#27744 [Bgs]: 141.23 - 141.00 = 0.22999999999999 ?

From: Date: Wed, 31 Mar 2004 00:47:47 +0000
Subject: #27744 [Bgs]: 141.23 - 141.00 = 0.22999999999999 ?
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-57165@lists.php.net to get a copy of this message
 ID:               27744
 Updated by:       alan_k@php.net
 Reported By:      t dot steve at ariadne-quatra dot com
 Status:           Bogus
 Bug Type:         Math related
 Operating System: *
 PHP Version:      *
 New Comment:

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)




Previous Comments:
------------------------------------------------------------------------

[2004-03-30 13:45:03] helly@php.net

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)

------------------------------------------------------------------------

[2004-03-29 01:06:46] alan_k@php.net

Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.
 
Thank you for your interest in PHP.

.

------------------------------------------------------------------------

[2004-03-29 01:05:32] t dot steve at ariadne-quatra dot com

Description:
------------
Subtraction does not work as expected.

Windows 2000 Server
SP4
IIS5
PHP5RC1

Reproduce code:
---------------
$result=141.23-141.00;
echo $result;

(or 
$result=141.23-141;
echo $result;
 - same result)

Expected result:
----------------
0.23

Actual result:
--------------
0.22999999999999


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=27744&edit=1


Thread (14 messages)

« previous php.bugs (#57165) next »