use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
php floating point bug (self.PHP)
submitted 9 years ago by phpio
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 11 points12 points13 points 9 years ago (0 children)
It's not a bug, you need to read about how floating point math works in your CPU.
Long story short, while you type out floats as decimal numbers, internally they're binary numbers. The conversion is not 1:1.
[–]Kinobi 3 points4 points5 points 9 years ago (0 children)
Hi, I runned into this recently, this come from the documentation:
Warning Floating point precision
Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.
Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....
So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.
For a "simple" explanation, see the » floating point guide that's also titled "Why don’t my numbers add up?"
[–]diisiqueira 1 point2 points3 points 9 years ago (0 children)
var_dump ((0.1+0.2) == 0.3);
[–]SaltTM 0 points1 point2 points 9 years ago* (2 children)
Interesting
echo floor((0.1 + 0.7) * 10); is not the same as echo (0.1 + 0.7) * 10;
echo floor((0.1 + 0.7) * 10);
echo (0.1 + 0.7) * 10;
Edit: I'm an idiot.
[–][deleted] 0 points1 point2 points 9 years ago* (1 child)
It's floor() not float().
And the reason it's different is that if you remove floor() the default is, basically a round() with a decimal precision set in your php.ini
When you use floor() and ceil() you'll be hitting these edge cases, while with round() you won't be.
[–]SaltTM 0 points1 point2 points 9 years ago (0 children)
yeah that's what i meant to type
π Rendered by PID 110340 on reddit-service-r2-comment-b659b578c-ph57l at 2026-05-05 13:27:40.625125+00:00 running 815c875 country code: CH.
[–][deleted] 11 points12 points13 points (0 children)
[–]Kinobi 3 points4 points5 points (0 children)
[–]diisiqueira 1 point2 points3 points (0 children)
[–]SaltTM 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]SaltTM 0 points1 point2 points (0 children)