4 ms·
Not sure about "no surprises" - just saw this on Twitter yesterday: https://twitter.com/LupinityLabs/status/1554217944179249153 https://twitter.com/LupinityLabs
by timack 4y ago
Not sure about "no surprises" - just saw this on Twitter yesterday: https://twitter.com/LupinityLabs/status/1554217944179249153 https://twitter.com/LupinityLabs/status/1554217944179249153
- nyanpasu64 4y agoApparently exact minimal float-to-string conversion is more recent than I thought, and many languages used to print more (Python?) or less (PHP) decimal digits than necessary to uniquely identify the bit pattern. Python correctly prints 46000.80 + 553.04 as 46553.840000000004, but I don't know if it ever prints more digits than needed. One recent algorithm for printing floats exactly is https://github.com/ulfjack/ryu https://github.com/ulfjack/ryu, though I'm unaware what's the state-of-the-art (https://github.com/jk-jeon/dragonbox https://github.com/jk-jeon/dragonbox claims to be a benchmark and the best algorithm).
- deleted 4y ago[deleted]
- zeroonetwothree 4y agoThis is hardly specific to PHP. You should never compare IEEE floats using equality.
- unethical_ban 4y agoIsn't it the fault of the interpreter that it shows a rounded value without being promoted to?
- IshKebab 4y agoIt is absolutely the fault of PHP. You absolutely can compare IEEE floats using equality. Often it isn't what you want but this totally should work. In this case PHP is unexpectedly rounding floats for display. ~ python3 Python 3.9.13 (main, May 24 2022, 21:28:44) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> b = 46333.48000000001 >>> b 46333.48000000001 ~ php -a Interactive shell php > $b = 46333.48000000001; php > echo $b; 46333.48
- redml 4y agoI mean, its PHP - I'm not surprised ;)
- RedShift1 4y agoThis just in: Developer discovers quirks of floating point numbers, declares outrage.
- wpearse 4y agoI did a 100-level paper at University that explained floating point arithmetic and how to solve for equality. Without being snarky, how do developers miss this sort of stuff?
- GrumpyNl 4y agoNot every developer need floats for the backend of a website.
- omnimus 4y agoYet when you go through the thread exactly same issue - float rounding happens in many other languages. There are better examples of PHP being bad.
- conradfr 4y agoNot exactly true: https://onlinephp.io/c/38d42 https://onlinephp.io/c/38d42 According to the Twitter thread: > First value was retrieved from a db, second was calculated, which has introduced rounding errors.