ID: 27744 Updated by: [EMAIL PROTECTED] Reported By: t dot steve at ariadne-quatra dot com Status: Bogus Bug Type: Math related Operating System: * PHP Version: * New Comment:
Floating point values in computers are never exact. It's a fact of life, and an issue that is not only encountered with PHP but other languages as well. Previous Comments: ------------------------------------------------------------------------ [2004-03-30 21:35:54] t dot steve at ariadne-quatra dot com Hi! I am sure I am something, apologies for that. :( "Guys, how in the world is PHP supposed to magically guess what precision you want results displayed in." 141.23 - 141 _is_ precisely 0.23. If I was asking for 1/3, then I would understand the decimal places. But how come 141.23-141 turns out to have so many decimal places in the end instead of just being 0.23 - the mathematically correct and precise result? ------------------------------------------------------------------------ [2004-03-30 19:59:05] [EMAIL PROTECTED] Guys, how in the world is PHP supposed to magically guess what precision you want results displayed in. If you know you always want lower precision, set that in your php.ini file. Or if you just want it temporarily simply do: $old = ini_set('precision',2); echo (string)(750 - 749.99); ini_set('precision',$old); ------------------------------------------------------------------------ [2004-03-30 19:47:47] [EMAIL PROTECTED] create table a ( b float,c float ); Query OK, 0 rows affected (0.11 sec) mysql> insert into a (b,c) values (141.23,141); Query OK, 1 row affected (0.07 sec) mysql> select b-c from a; +------------------+ | b-c | +------------------+ | 0.22999572753906 | +------------------+ 1 row in set (0.00 sec) ------------------------------------------------------------------------ [2004-03-30 13:45:03] [EMAIL PROTECTED] That is the whole point of the answer. Floating point values are not accurate and are not nice. And we do not do a bunch of work just to make them look better in certain circumstances. ------------------------------------------------------------------------ [2004-03-30 12:07:45] garbo_doe at hotmail dot com IMHO I think this is a bug. Of course there are problems with floatingpoint values in binary form, especially when rounded many times. But in an operation like <?php echo (string)(750 - 749.99) ?> it shouldn't return "0.00999999999999" but "0.01". I did a quick test in Delphi: showmessage(floattostr(750 - 749.99)); returns "0.01, not "0.00999999999999". I had to solve it in PHP but multiplying with 100, then subtract and then divide the result by 100 again. It's not pretty :-D (0.00999[infinite 9's] IS exactly the same as 0.01, but it should remember the "infinite" with a bit or something, so (1/3)*3 = 1 and not 0.999999999) (this is similar as bug #8164) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/27744 -- Edit this bug report at http://bugs.php.net/?id=27744&edit=1