Fix rounding of zend_dval_to_lval

Now rounding is always towards zero; the rounding can be though as if
occurring on the original double. Only relevant for the 32-bit long
variant.
This commit is contained in:
Gustavo Lopes 2013-02-23 00:41:39 +01:00
parent 77566edbaf
commit a86fcfbc1d
2 changed files with 8 additions and 1 deletions

View file

@ -15,6 +15,8 @@ if (PHP_INT_SIZE != 4)
-3999999999999999475712.,
-3999999999999998951424.,
];
/* see if we're rounding negative numbers right */
$values[] = -2147483649.8;
foreach ($values as $v) {
var_dump((int)$v);
@ -27,3 +29,4 @@ int(-2055733248)
int(-2055208960)
int(-2054684672)
int(-2054160384)
int(2147483647)

View file

@ -77,7 +77,9 @@ static zend_always_inline long zend_dval_to_lval(double d)
dmod = fmod(d, two_pow_32);
if (dmod < 0) {
dmod += two_pow_32;
/* we're going to make this number positive; call ceil()
* to simulate rounding towards 0 of the negative number */
dmod = ceil(dmod) + two_pow_32;
}
return (long)(unsigned long)dmod;
}
@ -93,6 +95,8 @@ static zend_always_inline long zend_dval_to_lval(double d)
dmod = fmod(d, two_pow_64);
if (dmod < 0) {
/* no need to call ceil; original double must have had no
* fractional part, hence dmod does not have one either */
dmod += two_pow_64;
}
return (long)(unsigned long)dmod;