diff --git a/NEWS b/NEWS index aabe78ad2a7c..9781419c15a2 100644 --- a/NEWS +++ b/NEWS @@ -151,6 +151,10 @@ PHP NEWS . Fixed a heap over-read in the interactive shell prompt when cli.prompt is set to an empty string. (Ilia Alshanetsky) +- Reflection: + . Fixed floats losing precision in __toString() output. + (Jorg Sowa) + - Session: . Fixed session_start() continuing after a failed create_sid() when session.use_strict_mode rejects the supplied ID. (Ilia Alshanetsky) diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index bbe805a81479..e27dd2ebe111 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -593,7 +593,7 @@ static void _const_string(smart_str *str, const zend_string *name, const zval *v } else if (Z_TYPE_P(value) == IS_STRING) { smart_str_append(str, Z_STR_P(value)); } else if (Z_TYPE_P(value) == IS_DOUBLE) { - smart_str_append_double(str, Z_DVAL_P(value), (int) EG(precision), false); + smart_str_append_double(str, Z_DVAL_P(value), /* precision */ -1, /* zero_fraction */ true); } else if (Z_TYPE_P(value) == IS_FALSE) { smart_str_append(str, ZSTR_KNOWN(ZEND_STR_FALSE)); } else if (Z_TYPE_P(value) == IS_TRUE) { @@ -633,7 +633,7 @@ static void _class_const_string(smart_str *str, const zend_string *name, zend_cl } else if (Z_TYPE(c->value) == IS_OBJECT) { smart_str_appends(str, "Object"); } else if (Z_TYPE(c->value) == IS_DOUBLE) { - smart_str_append_double(str, Z_DVAL(c->value), (int) EG(precision), false); + smart_str_append_double(str, Z_DVAL(c->value), /* precision */ -1, /* zero_fraction */ true); } else if (Z_TYPE(c->value) == IS_FALSE) { smart_str_append(str, ZSTR_KNOWN(ZEND_STR_FALSE)); } else if (Z_TYPE(c->value) == IS_TRUE) { @@ -710,7 +710,9 @@ static zval *get_default_from_recv(const zend_op_array *op_array, uint32_t offse } static void format_default_value(smart_str *str, const zval *value) { - if (smart_str_append_zval(str, value, SIZE_MAX) == SUCCESS) { + if (Z_TYPE_P(value) == IS_DOUBLE) { + smart_str_append_double(str, Z_DVAL_P(value), /* precision */ -1, /* zero_fraction */ true); + } else if (smart_str_append_zval(str, value, SIZE_MAX) == SUCCESS) { /* Nothing to do. */ } else if (Z_TYPE_P(value) == IS_ARRAY) { bool is_list = zend_array_is_list(Z_ARRVAL_P(value)); diff --git a/ext/reflection/tests/default_value_float_precision.phpt b/ext/reflection/tests/default_value_float_precision.phpt new file mode 100644 index 000000000000..fc936d9569ae --- /dev/null +++ b/ext/reflection/tests/default_value_float_precision.phpt @@ -0,0 +1,62 @@ +--TEST-- +Reflection prints floats with round-trip precision +--INI-- +precision=14 +--FILE-- +getParameters() as $p) { + echo $p, "\n"; +} +echo new ReflectionProperty(C::class, 'p'); +echo $m->getAttributes()[0]; +echo new ReflectionClassConstant(C::class, 'K'); +echo new ReflectionClassConstant(C::class, 'L'); +echo new ReflectionClassConstant(C::class, 'W'); +echo new ReflectionConstant('G'); + +?> +--EXPECT-- +Parameter #0 [ $a = 0.30000000000000004 ] +Parameter #1 [ $b = FOO + 0.30000000000000004 ] +Parameter #2 [ $c = [0.1, 1.0, 1.0E+100] ] +Parameter #3 [ $d = -0.0 ] +Parameter #4 [ $e = INF ] +Parameter #5 [ $f = M_PI ] +Property [ public $p = 0.30000000000000004 ] +Attribute [ Attr ] { + - Arguments [1] { + Argument #0 [ 0.30000000000000004 ] + } +} +Constant [ public float K ] { 0.30000000000000004 } +Constant [ public float L ] { 0.3333333333333333 } +Constant [ public float W ] { 1.0 } +Constant [ float G ] { 0.30000000000000004 } diff --git a/ext/reflection/tests/gh22683.phpt b/ext/reflection/tests/gh22683.phpt index 9dfe8eba25c2..9c6fe7565938 100644 --- a/ext/reflection/tests/gh22683.phpt +++ b/ext/reflection/tests/gh22683.phpt @@ -25,4 +25,4 @@ Constant [ float INF ] { INF } Constant [ public float MY_NAN ] { NAN } Constant [ public float MY_INF ] { INF } Constant [ public float MY_FLOAT ] { 1.5 } -Constant [ public float MY_WHOLE ] { 2 } +Constant [ public float MY_WHOLE ] { 2.0 }