php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22887 sprintf( "%.2f", 10.75 ); returns "0.00"
Submitted: 2003-03-25 23:36 UTC Modified: 2003-03-28 11:34 UTC
From: josh at chatgris dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.1 OS: Gentoo Linux /w apache2
Private report: No CVE-ID: None
 [2003-03-25 23:36 UTC] josh at chatgris dot com
Sample script 
 
<?php 
  echo sprintf( "%.2f", 10.75 ); 
?> 
 
returns 0.00 
 
this is NOT the expected behaviour. 
 
Using apache2, not tested on apache1. 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-26 00:28 UTC] rasmus@php.net
Works just fine from in CLI and Apache1.  Don't have an
Apache2 server handy for testing this.
 [2003-03-26 09:25 UTC] iliaa@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-STABLE-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-STABLE-latest.zip

Cannot verify using Apache 2 either, if latest snapshot still has this problem, please include your configure line for PHP in your reply.
 [2003-03-27 21:44 UTC] josh at chatgris dot com
I built it with the lastest snapshot (phpinfo said it was  
4.3.2-RC) and the exact same problem is occuring.  
  
I have also made a sample C program to test the output of  
printf and it works in the C program.  So it's definately  
something to do with php.  
 
Anything else I can do to help you guys figure this out? 
  
Josh.
 [2003-03-27 21:55 UTC] rasmus@php.net
It cannot be a general php problem as nobody here can reproduce it.  It has to something related to either Apache2 or some other factor unique to your machine.
 [2003-03-28 00:08 UTC] josh at chatgris dot com
I am not the only one experiencing this problem..  plus it 
seems to round the number randomly for example 
 
 
echo sprintf('%.2f', '8.7586206896551724');  
  
returns 10.00 
 
the person who had this problem's post is here 
 
http://forums.gentoo.org/viewtopic.php?t=38470&highlight=sprintf
 [2003-03-28 02:43 UTC] helly@php.net
Just tried PHP4.3.2/5 with CLI/Apache1 on mdk/winxp. All
work fine and the code that might cause the problems is not sapi dependant.

The external message you pointed to is different because he used a string to be printed with %f. That means it must first be converted to a number and then printed.
 [2003-03-28 04:41 UTC] tal@php.net
Works fine here (Gentoo with glibc 2.3.2), using php5 cvs from two days ago (cli).

-tal
 [2003-03-28 10:27 UTC] josh at chatgris dot com
I believe the problem is related..  For example, we are 
both using Pentium$ CPU's.. 
 
sprintf works fine on my Athlon, but not my pentium4... 
 
In addition, his example produced the same output on my 
machine.  and I've tried putting the float in a variable, 
float as a string it alwasy returns the same so I am 
pretty sure that the problem is within sprintf.. 
 
Could anyone tell me where the sprintf code is in php?  I 
don't mind messing with it and trying to figure out what 
is wrong... 
 
Josh.
 [2003-03-28 11:16 UTC] jay@php.net
This is most likely being caused by over-optimization when 
compiling. I had a similar problem on my gentoo box, and 
the problem was traced back to over-optimizing when 
compiling glibc. (var_dump() was printing some weird 
floats, and I believe this problem is similar, as both use 
modf() at some point.) 
 
Basically, tone down the optimizations a bit. If you 
compile glibc with CFLAGS along the lines of 
"-march=pentium4 -O3...", modf() starts dying. The 
solution (for gentoo, at least) is to modify your USE flag 
in make.conf to use -march=pentium3 if you're using 
pentium4 and re-emerge glibc, then recompile PHP.  
 
gcc has problems with march pentium4 spitting out bad 
instructions, so you should stay away from it, even if 
you're actually on a Pentium 4. Use march=pentium3 or 
mcpu=i686 instead. There are threads on the gentoo forums 
about this, and newer versions of portage mention it in 
the make.conf comments. 
 
If this is indeed the problem, it affects python, too, 
fwiw. 
 
J 
 [2003-03-28 11:23 UTC] josh at chatgris dot com
Thank you for the suggestion!! 
 
I am going to try and compile my entire gentoo system from 
scratch using mcpu=i686 (I feel kinda iffy using 
march=pentium3 on a pentium4). 
 
I'll respond as soon as I have new information. 
 
Thanks again!!!! 
 
Josh.
 [2003-03-28 11:26 UTC] jay@php.net
I haven't had any problems on my system using pentium3. 
When I came across the problem, I was using pentium4 and I 
switched it over to pentium3, so my system is slowly being 
converted. (I have so much on this system, I can't 
re-emerge everything at once as I need it for work, so I'm 
doing it bit by bit.) 
 
Fwiw, my CFLAGS is "-march=pentium3 -O3 -pipe 
-fomit-frame-pointer", on a P4 Dell laptop.  
 
Slight correction to previous post: I meant CFLAGS, not 
the USE flag in make.conf. 
 
J 
 [2003-03-28 11:34 UTC] iliaa@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

This is a known bug in gcc 3.2.X with pentium4 optimized code. Not a PHP issue.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 12:01:31 2024 UTC