php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33576 19.6*100 != 19.6*10*10
Submitted: 2005-07-05 20:11 UTC Modified: 2005-07-06 15:39 UTC
From: rick at ninjafoo dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: * OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rick at ninjafoo dot com
New email:
PHP Version: OS:

 

 [2005-07-05 20:11 UTC] 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, 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-05 20:13 UTC] 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:56 UTC] 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 21:34 UTC] 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-06 10:42 UTC] rick at ninjafoo dot com
Your right, this isnt a bug 
 
(double)(19.6*100) !== (double)1960 
 
This is just plain broken.
 [2005-07-06 15:39 UTC] rasmus@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 18:01:29 2024 UTC