Edit report at http://bugs.php.net/bug.php?id=54056&edit=1

 ID:                 54056
 User updated by:    mr_platelet+jin6vr at fastmail dot fm
 Reported by:        mr_platelet+jin6vr at fastmail dot fm
 Summary:            number_format returns an incorrect answer for
                     certain parameters
 Status:             Open
 Type:               Bug
 Package:            Strings related
 Operating System:   Linux
 PHP Version:        5.3.5
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[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

Reply via email to