mirror of
https://github.com/php/php-src.git
synced 2025-08-16 05:58:45 +02:00
Cast NaN and Infinity to zero
This commit is contained in:
parent
6a58f270d8
commit
cb770cdc03
2 changed files with 55 additions and 1 deletions
37
Zend/tests/int_special_values.phpt
Normal file
37
Zend/tests/int_special_values.phpt
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
--TEST--
|
||||||
|
Conversion of special float values to int
|
||||||
|
--FILE--
|
||||||
|
<?php
|
||||||
|
$values = [
|
||||||
|
0.0,
|
||||||
|
INF,
|
||||||
|
-INF,
|
||||||
|
1 / INF,
|
||||||
|
-1 / INF, // Negative zero,
|
||||||
|
NAN
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach($values as $value) {
|
||||||
|
var_dump($value);
|
||||||
|
var_dump((int)$value);
|
||||||
|
echo PHP_EOL;
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
--EXPECT--
|
||||||
|
float(0)
|
||||||
|
int(0)
|
||||||
|
|
||||||
|
float(INF)
|
||||||
|
int(0)
|
||||||
|
|
||||||
|
float(-INF)
|
||||||
|
int(0)
|
||||||
|
|
||||||
|
float(0)
|
||||||
|
int(0)
|
||||||
|
|
||||||
|
float(-0)
|
||||||
|
int(0)
|
||||||
|
|
||||||
|
float(NAN)
|
||||||
|
int(0)
|
|
@ -71,8 +71,21 @@ ZEND_API zend_bool instanceof_function_ex(const zend_class_entry *instance_ce, c
|
||||||
ZEND_API zend_bool instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce TSRMLS_DC);
|
ZEND_API zend_bool instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce TSRMLS_DC);
|
||||||
END_EXTERN_C()
|
END_EXTERN_C()
|
||||||
|
|
||||||
|
/* isnan() might not be available (<C99), so we'll define it if so */
|
||||||
|
#ifndef isnan
|
||||||
|
/* NaN is never equal to itself */
|
||||||
|
# define isnan(n) ((n) != (n))
|
||||||
|
#endif
|
||||||
|
|
||||||
#if ZEND_DVAL_TO_LVAL_CAST_OK
|
#if ZEND_DVAL_TO_LVAL_CAST_OK
|
||||||
# define zend_dval_to_lval(d) ((long) (d))
|
static zend_always_inline long zend_dval_to_lval(double d)
|
||||||
|
{
|
||||||
|
if (EXPECTED(!isnan(d))) {
|
||||||
|
return (long)d;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
#elif SIZEOF_LONG == 4
|
#elif SIZEOF_LONG == 4
|
||||||
static zend_always_inline long zend_dval_to_lval(double d)
|
static zend_always_inline long zend_dval_to_lval(double d)
|
||||||
{
|
{
|
||||||
|
@ -87,6 +100,8 @@ static zend_always_inline long zend_dval_to_lval(double d)
|
||||||
dmod = ceil(dmod) + two_pow_32;
|
dmod = ceil(dmod) + two_pow_32;
|
||||||
}
|
}
|
||||||
return (long)(unsigned long)dmod;
|
return (long)(unsigned long)dmod;
|
||||||
|
} else if (UNEXPECTED(isnan(d))) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
return (long)d;
|
return (long)d;
|
||||||
}
|
}
|
||||||
|
@ -105,6 +120,8 @@ static zend_always_inline long zend_dval_to_lval(double d)
|
||||||
dmod += two_pow_64;
|
dmod += two_pow_64;
|
||||||
}
|
}
|
||||||
return (long)(unsigned long)dmod;
|
return (long)(unsigned long)dmod;
|
||||||
|
} else if (UNEXPECTED(isnan(d))) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
return (long)d;
|
return (long)d;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue