RE: [PHP-DEV] Float comparison
| From: | Derick Rethans | Date: | Sun, 04 May 2008 20:35:55 +0000 |
| Subject: | RE: [PHP-DEV] Float comparison | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-37443@lists.php.net to get a copy of this message | ||
On Sun, 4 May 2008, Jonathan Bond-Caron wrote:
> Thanks, I made some tests after looking at
> http://lxr.php.net/source/php-src/main/spprintf.c#712
>
> At first glance, I am utterly confused,
>
> ini_set('precision', 100);
> setlocale(LC_ALL, 'fr_FR.UTF-8');
> echo 0.3; // 0,299999999999999988897769753748434595763683319091796875
> echo "\n";
> echo (string)0.3; // 0.3
> // expected: 0,3
> echo "\n";
> echo (float)(string)0.3; // 0,299999999999999988897769753748434595763683319091796875
> // expected: 0,3
Expected should be "0" here actually:
(string) 0.3 -> converts to "0,299999999999999988897769753748434595763683319091796875".
(float) "0,299999999999999988897769753748434595763683319091796875" -> converts to 0.
I get the following output with both PHP 5.2.6:
0,299999999999999988897769753748434595763683319091796875
0,299999999999999988897769753748434595763683319091796875
0
0,3
Which is what I would expect.
regards,
Derick
--
Derick Rethans
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org