Skip to content
Draft
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ PHP NEWS
(Nicolas Grekas)
. Don't call destructor when constructor is not public in newInstance(Args)().
(ndossche)
. Fixed float default values losing precision in __toString() output.
(Jorg Sowa)

- Session:
. Fixed session upload progress to respect session.use_only_cookies.
Expand Down
4 changes: 3 additions & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
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
47 changes: 47 additions & 0 deletions ext/reflection/tests/default_value_float_precision.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
Reflection prints float default values with round-trip precision
--INI--
precision=14
--FILE--
<?php

const FOO = 1;

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

class C {
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,
) {}
}

$m = new ReflectionMethod(C::class, 'm');
foreach ($m->getParameters() as $p) {
echo $p, "\n";
}
echo new ReflectionProperty(C::class, 'p');
echo $m->getAttributes()[0];

?>
--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 ]
Property [ public $p = 0.30000000000000004 ]
Attribute [ Attr ] {
- Arguments [1] {
Argument #0 [ 0.30000000000000004 ]
}
}
Loading