Skip to content

Commit 4107b54

Browse files
committed
Check that doubles are IEEE-754 in Randomizer::nextFloat()
1 parent be6f4ad commit 4107b54

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

ext/random/randomizer.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ PHP_METHOD(Random_Randomizer, nextFloat)
118118

119119
ZEND_PARSE_PARAMETERS_NONE();
120120

121+
#ifndef __STDC_IEC_559__
122+
zend_throw_exception(random_ce_Random_RandomException, "The nextFloat() method requires the underlying 'double' representation to be IEEE-754.", 0);
123+
RETURN_THROWS();
124+
#endif
125+
121126
result = 0;
122127
total_size = 0;
123128
do {
@@ -133,6 +138,7 @@ PHP_METHOD(Random_Randomizer, nextFloat)
133138
* use the full 64 bits of the uint64_t, because we would
134139
* introduce a bias / rounding error.
135140
*/
141+
ZEND_ASSERT(DBL_MANT_DIG == 53);
136142
const double step_size = 1.0 / (1ULL << 53);
137143

138144
/* Use the upper 53 bits, because some engine's lower bits

0 commit comments

Comments
 (0)