News for hackers. Stuff that matters.
Re:I still love PHP and JavaScript by timack on Tuesday August 02, 2022 @08:54PM (#32326361)
Not sure about "no surprises" - just saw this on Twitter yesterday: https://twitter.com/LupinityLabs/status/1554217944179249153
Re:I still love PHP and JavaScript by zeroonetwothree on Tuesday August 02, 2022 @10:13PM (#32326843)
This is hardly specific to PHP. You should never compare IEEE floats using equality.
Re:I still love PHP and JavaScript by IshKebab on Wednesday August 03, 2022 @08:36AM (#32330863)
It 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
Re:I still love PHP and JavaScript by unethical_ban on Wednesday August 03, 2022 @12:55AM (#32327806)
Isn't it the fault of the interpreter that it shows a rounded value without being promoted to?
Re:I still love PHP and JavaScript by nyanpasu64 on Tuesday August 02, 2022 @09:00PM (#32326401)
Apparently 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, though I'm unaware what's the state-of-the-art (https://github.com/jk-jeon/dragonbox claims to be a benchmark and the best algorithm).
Re:I still love PHP and JavaScript by conradfr on Wednesday August 03, 2022 @04:51AM (#32329239)
Not exactly true: 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.

Re:I still love PHP and JavaScript by RedShift1 on Wednesday August 03, 2022 @02:35AM (#32328346)
This just in: Developer discovers quirks of floating point numbers, declares outrage.
Re:I still love PHP and JavaScript by wpearse on Wednesday August 03, 2022 @02:52AM (#32328457)
I 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?
Re:I still love PHP and JavaScript by GrumpyNl on Wednesday August 03, 2022 @09:33AM (#32331470)
Not every developer need floats for the backend of a website.
Re:I still love PHP and JavaScript by omnimus on Wednesday August 03, 2022 @02:57AM (#32328488)
Yet when you go through the thread exactly same issue - float rounding happens in many other languages.

There are better examples of PHP being bad.

Re:I still love PHP and JavaScript by redml on Wednesday August 03, 2022 @01:19AM (#32327919)
I mean, its PHP - I'm not surprised ;)