Bug #54056 [Opn->Fbk]: number_format returns an incorrect answer for certain parameters
| From: | rasmus@php.net | Date: | Sun, 20 Feb 2011 15:37:26 +0000 |
| Subject: | Bug #54056 [Opn->Fbk]: number_format returns an incorrect answer for certain parameters | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-158904@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=54056&edit=1
ID: 54056
Updated by: rasmus@php.net
Reported by: mr_platelet+jin6vr at fastmail dot fm
Summary: number_format returns an incorrect answer for
certain parameters
-Status: Open
+Status: Feedback
Type: Bug
Package: Strings related
Operating System: Linux
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
I'm not sure what you are expecting here. number_format() is not an
arbitrary
precision function. You are limited by the double precision of your
operating
system.
Previous Comments:
------------------------------------------------------------------------
[2011-02-20 14:33:45] mr_platelet+jin6vr at fastmail dot fm
The following script shows further problems with
number_format.
------------------
<?php
$count = 0;
$wrong = 0;
for($i = 319; $i <= 1074; $i++) {
$s1 = number_format(pow(2, -$i), $i, '.', '');
$s2 = bcpow(2, -$i, $i);
$s1 = trim($s1, '0');
$s2 = trim($s2, '0');
$count++;
$wrong += ($s1 !== $s2);
}
print "Number of calculations: $count\n";
print "Number of wrong answers: $wrong\n";
------------------
When I run the script, it prints:
Number of calculations: 756
Number of wrong answers: 756
Some extra information. (1) When running
each of the 2 scripts I've supplied, I
used PHP's "-n" switch. (2) Here is the
"configure" command I used when building
PHP:
--prefix=/usr/local/php/5.3.5 --disable-all --enable-bcmath
------------------------------------------------------------------------
[2011-02-20 14:17:59] mr_platelet+jin6vr at fastmail dot fm
Description:
------------
In the script below, number_format returns a string
representing zero, which is quite wrong.
Test script:
---------------
<?php
$x = pow(2, -1074);
$s = number_format($x, 1074, '.', '');
if(trim($s, '0') === '.') {
print "\$s represents zero, which is wrong.\n";
}
Expected result:
----------------
I expect to see no output.
Actual result:
--------------
The script outputs the following message:
$s represents zero, which is wrong.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54056&edit=1