Skip to content

Commit 46ea478

Browse files
committed
Add remaining GetFloatBounds variants
1 parent 1b9eb78 commit 46ea478

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

ext/random/random.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public function generate(): string {}
116116
enum GetFloatBounds {
117117
case ClosedOpen;
118118
case ClosedClosed;
119+
case OpenClosed;
120+
case OpenOpen;
119121
}
120122
}
121123

ext/random/random_arginfo.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/random/randomizer.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,32 @@ static double getFloat_closed_closed(const php_random_algo *algo, php_random_sta
187187
}
188188
}
189189

190+
static double getFloat_open_closed(const php_random_algo *algo, php_random_status *status, double min, double max)
191+
{
192+
double g = getFloat_gamma(min, max);
193+
uint64_t hi = getFloat_ceilint(min, max, g);
194+
uint64_t k = algo->range(status, 0, hi - 1);
195+
196+
if (fabs(min) <= fabs(max)) {
197+
return max - k * g;
198+
} else {
199+
return k == (hi - 1) ? max : min + (k + 1) * g;
200+
}
201+
}
202+
203+
static double getFloat_open_open(const php_random_algo *algo, php_random_status *status, double min, double max)
204+
{
205+
double g = getFloat_gamma(min, max);
206+
uint64_t hi = getFloat_ceilint(min, max, g);
207+
uint64_t k = algo->range(status, 1, hi - 1);
208+
209+
if (fabs(min) <= fabs(max)) {
210+
return max - k * g;
211+
} else {
212+
return min + k * g;
213+
}
214+
}
215+
190216
/* {{{ Generates a random float within [min, max).
191217
*
192218
* The algorithm used is the γ-section algorithm as published in:
@@ -230,6 +256,12 @@ PHP_METHOD(Random_Randomizer, getFloat)
230256
if (zend_string_equals_literal(bounds_name, "ClosedClosed")) {
231257
RETURN_DOUBLE(getFloat_closed_closed(randomizer->algo, randomizer->status, min, max));
232258
}
259+
if (zend_string_equals_literal(bounds_name, "OpenClosed")) {
260+
RETURN_DOUBLE(getFloat_open_closed(randomizer->algo, randomizer->status, min, max));
261+
}
262+
if (zend_string_equals_literal(bounds_name, "OpenOpen")) {
263+
RETURN_DOUBLE(getFloat_open_open(randomizer->algo, randomizer->status, min, max));
264+
}
233265
ZEND_UNREACHABLE();
234266
}
235267
/* }}} */

0 commit comments

Comments
 (0)