Bug #64307 [Opn->Nab]: Floating point error
| From: | pajoye@php.net | Date: | Wed, 27 Feb 2013 09:42:45 +0000 |
| Subject: | Bug #64307 [Opn->Nab]: Floating point error | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-177434@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64307&edit=1
ID: 64307
Updated by: pajoye@php.net
Reported by: sokkis at gmail dot com
Summary: Floating point error
-Status: Open
+Status: Not a bug
Type: Bug
Package: Variables related
Operating System: Gentoo linux
PHP Version: 5.4.12
Block user comment: N
Private report: N
New Comment:
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://www.floating-point-gui.de/
Thank you for your interest in PHP.
Previous Comments:
------------------------------------------------------------------------
[2013-02-27 09:31:27] sokkis at gmail dot com
Description:
------------
Floating point rounded when print, but won't on subtraction.
Test script:
---------------
<?php
$n1 = 905000;
$n2 = 0.27;
$ertek =$n1*$n2;
$egesz = round( $ertek );
$eredmeny = $ertek-$egesz;
echo "$ertek - $egesz = $eredmeny\n";
$n1 = 905000;
$n2 = 0.27;
$ertek =$n1*$n2;
$egesz = (int) $ertek;
$eredmeny = $ertek-$egesz;
echo "$ertek - $egesz = $eredmeny";
Expected result:
----------------
244350 - 244350 = 0
244350 - 244350 = 0
Actual result:
--------------
244350 - 244350 = 2.9103830456734E-11
244350 - 244350 = 2.9103830456734E-11
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64307&edit=1