Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down
62 changes: 62 additions & 0 deletions ext/reflection/tests/default_value_float_precision.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--TEST--
Reflection prints floats with round-trip precision
--INI--
precision=14
--FILE--
<?php

const FOO = 1;
const G = 0.30000000000000004;

#[Attribute]
class Attr {
public function __construct(public $x) {}
}

class C {
const K = 0.30000000000000004;
const L = FOO / 3;
const W = 1.0;

public $p = 0.30000000000000004;

#[Attr(0.30000000000000004)]
public function m(
$a = 0.30000000000000004,
$b = FOO + 0.30000000000000004,
$c = [0.1, 1.0, 1e100],
$d = -0.0,
$e = INF,
$f = M_PI,
) {}
}

$m = new ReflectionMethod(C::class, 'm');
foreach ($m->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 [ <optional> $a = 0.30000000000000004 ]
Parameter #1 [ <optional> $b = FOO + 0.30000000000000004 ]
Parameter #2 [ <optional> $c = [0.1, 1.0, 1.0E+100] ]
Parameter #3 [ <optional> $d = -0.0 ]
Parameter #4 [ <optional> $e = INF ]
Parameter #5 [ <optional> $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 }
2 changes: 1 addition & 1 deletion ext/reflection/tests/gh22683.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ Constant [ <persistent> 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 }
Loading