Bug #73732 [Opn->Nab]: Sprintf bug for 1.15*100*2^n values
| From: | requinix@php.net | Date: | Tue, 13 Dec 2016 10:35:49 +0000 |
| Subject: | Bug #73732 [Opn->Nab]: Sprintf bug for 1.15*100*2^n values | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-205955@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73732&edit=1
ID: 73732
Updated by: requinix@php.net
Reported by: teodor dot danciu at gmail dot com
Summary: Sprintf bug for 1.15*100*2^n values
-Status: Open
+Status: Not a bug
Type: Bug
Package: Strings related
Operating System: Irrelevant
PHP Version: Irrelevant
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:
------------------------------------------------------------------------
[2016-12-13 10:25:01] teodor dot danciu at gmail dot com
Description:
------------
sprintf("%03d ",(1.15*100)); and values multiplied by 2 return wrong values
ex 114 instead of 115
Test script:
---------------
$price=1.15*100;
echo sprintf("%03d ",$price); // prints 114 instead of 115
$price=1.15*100*2;
echo sprintf("%03d ",$price); // prints 229 instead of 230
$price=1.15*100*4;
echo sprintf("%03d ",$price); // prints 459 instead of 460
$price=1.15*100*8;
echo sprintf("%03d ",$price); // prints 919 instead of 920
$price=1.15*100*10;
echo sprintf("%03d ",$price); // prints 1149 instead of 1150
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73732&edit=1