#33576 [Bgs]: 19.6*100 != 19.6*10*10
| From: | rasmus@php.net | Date: | Wed, 06 Jul 2005 13:39:53 +0000 |
| Subject: | #33576 [Bgs]: 19.6*100 != 19.6*10*10 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-81482@lists.php.net to get a copy of this message | ||
ID: 33576
Updated by: rasmus@php.net
Reported By: rick at ninjafoo dot com
Status: Bogus
Bug Type: Math related
Operating System: *
PHP Version: *
New Comment:
I am not sure why you think casting 1960 to a float suddenly would make
19.6*100 equal to exactly 1960.0. Floating point math is not exact.
19.6*100 is never going to be exactly 1960.0. It will be
1959.999999999999999999999 or 1960.00000000000000000001 regardless of
any casting you do to it. You can round() it, or add a fuzz factor and
floor() it.
Previous Comments:
------------------------------------------------------------------------
[2005-07-06 10:42:38] rick at ninjafoo dot com
Your right, this isnt a bug
(double)(19.6*100) !== (double)1960
This is just plain broken.
------------------------------------------------------------------------
[2005-07-05 21:34:21] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
.
------------------------------------------------------------------------
[2005-07-05 20:56:41] rick at ninjafoo dot com
Yes, I understand that. My bug report was not to point out
the joys of floating point math.
My bug report was to illustrate a problem with php's
internal casting.
19.6*100 != 1960
19.6*100 != (int)1960
19.6*100 != (float)1960
19.6*100 != (string)1960
echo gettype(19.6*100) returns 'double', However .....
19.6*100 != (double)1960
19.6*100 cannot be compaired to anything without manually
casting it as something else first.
(string)(19.6*100) == (string)1960
------------------------------------------------------------------------
[2005-07-05 20:13:07] derick@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.
If you would like to know more about "floats" and what IEEE
754 is read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Thank you for your interest in PHP.
.
------------------------------------------------------------------------
[2005-07-05 20:11:18] rick at ninjafoo dot com
Description:
------------
100* certain values is not the same as 10*10* the same
value.
The following code should produce no output. I see output.
Reproduce code:
---------------
<?php
$value1 = 19.6*100;
$value2 = 19.6*10*10;
if ($value1 != $value2) {
echo 'funny, i thaught 100x was exactly the same as 10x10x....<br>';
echo 'value1='.$value1.', value2='.$value2.',';
}
?>
Expected result:
----------------
I expect nothing to happen.
Actual result:
--------------
funny, i thaught 100x was exactly the same as 10x10x....
value1=1960, value2=1960,
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=33576&edit=1