Skip to content

ext/reflection: Fix float default values losing precision in reflection output - #25735

Open
jorgsowa wants to merge 2 commits into
php:masterfrom
jorgsowa:reflection-float-default-precision
Open

jorgsowa wants to merge 2 commits into
php:masterfrom
jorgsowa:reflection-float-default-precision

Conversation

@jorgsowa

@jorgsowa jorgsowa commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Reflection's __toString() printed floats using the precision INI setting. Constant expressions go through zend_ast_export(), which since #25461 prints the shortest exact representation. So $a = 0.30000000000000004 printed as 0.3, but $b = FOO + 0.30000000000000004 printed in full.

Default values, property defaults and attribute arguments now print the same way as constant expressions. So do class constants and global constants, which also now keep the trailing .0 like default values (const W = 1.0 prints as 1.0, not 1).

Follow-up #25461

I went bit further and improved overall consistency of float handling. I point it at master, let me know whether this should be fixed on 8.4 as well.

format_default_value() printed floats using the precision INI setting,
while constant expressions go through zend_ast_export(), which uses the
shortest round-trip representation. So $a = 0.30000000000000004 printed
as 0.3, but $b = FOO + 0.30000000000000004 printed in full.
@jorgsowa
jorgsowa changed the base branch from PHP-8.6 to master November 25, 2026 11:44
Class constants and global constants were printed using the precision
INI setting and without a trailing ".0", unlike default values.
@jorgsowa
jorgsowa force-pushed the reflection-float-default-precision branch from 903d0dd to c1409cc Compare November 25, 2026 15:09
@jorgsowa
jorgsowa marked this pull request as ready for review November 25, 2026 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant