Bug #61855 [Opn->Nab]: Wrong division of float numbers
Edit report at https://bugs.php.net/bug.php?id=61855&edit=1
ID: 61855
Updated by: pajoye@php.net
Reported by: martin dot edlman at gmail dot com
Summary: Wrong division of float numbers
-Status: Open
+Status: Not a bug
Type: Bug
Package: Math related
Operating System: Linux
PHP Version: 5.3.10
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:
------------------------------------------------------------------------
[2012-04-26 06:53:50] martin dot edlman at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.floor#refsect1-function.floor-description
---
I encountered problem when dividing float numbers. It's simple formula which can be solved by
10 year old child. But not by PHP! See example.
The problem is that displayed value (3) doesn't correspond with internal value
2.999999999999999 and floor() then returns 2 which is obviously incorrect result! And that's
serious problem.
The same code in C returns correct numbers.
Test script:
---------------
<?php
$a = 1.2;
$b = 0.4;
$v = $a / $b;
printf("v(15) = %.15f\n", $v);
printf("v(16) = %.16f\n", $v);
print "v = $v\n";
print "floor(v) = ".floor($v)."\n";
?>
Expected result:
----------------
v(15) = 3.000000000000000
v(16) = 3.0000000000000000
v = 3
floor(v) = 3
Actual result:
--------------
v(15) = 3.000000000000000
v(16) = 2.9999999999999996
v = 3
floor(v) = 2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61855&edit=1
Thread (4 messages)