php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #10002 sprintf() and floating point
Submitted: 2001-03-26 14:13 UTC Modified: 2002-10-14 22:48 UTC
From: bmr at comtime dot com Assigned: hholzgra (profile)
Status: Closed Package: Strings related
PHP Version: 4.1.1 OS: Debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bmr at comtime dot com
New email:
PHP Version: OS:

 

 [2001-03-26 14:13 UTC] bmr at comtime dot com
<?php
printf('%.2f',0.09999999999999999167332731531132594682276248931884765625);
?>
produces '0.0:' instead of '0.10'

I'm really curious as to why PHP just doesn't use libc's sprintf().

./configure --with-oracle=/usr/local/oracle/product/8.1.7 \
  --with-oci8=/usr/local/oracle/product/8.1.7 --enable-force-cgi-redirect \
  --enable-track-vars --with-posix --enable-sockets --enable-sigchild \
  --with-gd=/usr/local

gcc version 2.95.2 19991024 (release)


This is not exactly a fix...
--- ext/standard/formatted_print.c      Mon Mar 26 14:01:31 2001
+++ ext/standard/formatted_print.c-fixed        Mon Mar 26 14:02:26 2001
@@ -92,7 +92,7 @@
                while (p1 < &cvt_buf[NDIG])
                        *p++ = *p1++;
        } else if (arg > 0) {
-               while ((fj = arg * 10) < 1) {
+               while ((fj = arg * 10.0) < 0.9999999) {
                        arg = fj;
                        r2--;
                }


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-26 17:19 UTC] derick@php.net
This bug is in 4.0.5RC2 too, just confirmed it
 [2001-03-27 08:32 UTC] zeev@php.net
I can't reproduce it under RH 6.2.  Generally the fix isn't supposed to do anything (since arg is a double, 10 should be converted to a double automatically), but the fix can't hurt anything so if it works around a compiler bug, it's fine.

PHP cannot use sprintf() because sprintf() works with C types with predefined types for the arguments, whether PHP has its own types, and autoconverts arguments to match the format string.
 [2001-03-27 12:30 UTC] derick@php.net
Patch applied, but it's still a strange thing.
 [2002-01-09 16:36 UTC] pgerzson at freestart dot hu
I'm sorry to say that I experienced similar misbehaviour
with  PHP 4.0.6 and 4.1.1, too. 

I'm pulling aggregated statistics from database and display
it. I'm currently working on minimize a test case, please wait. Till then, visit this link:

http://213.134.22.60/~gerzson/sprintf.php

this is a very simple test script with a show_source() and 
phpinfo(). I think this bug should be reopened.
 [2002-01-09 16:43 UTC] gerzson@php.net
I'd like to reopen it. Visit the page: 

http://213.134.22.60/~gerzson/sprintf.php

 [2002-01-15 12:33 UTC] pgerzson at freestart dot hu
I played with ini settings (to be frank remove the
php.ini), and now examples work fine.
 [2002-01-22 07:46 UTC] martin at humany dot com
I cant reproduce this bug under PHP 4.1.1 neither in Windows XP or Linux 2.4.
I tried the original example aswell as the additional examples from http://213.134.22.60/~gerzson/sprintf.php

(abstract from phpinfo()):

System Linux www3 2.4.17 #4 SMP Sat Dec 22 19:40:48 CET 2001 i686 unknown 
Build Date Jan 13 2002 
Configure Command  './configure' '--with-apache=../apache_1.3.22' '--with-mysql=/usr/local' '--with-gd=/usr/local' '--with-jpeg-dir=/usr' '--enable-ftp' '--enable-bcmath' '--with-ming' '--with-mcrypt' '--with-mhash' '--with-xml' '--with-imap' '--with-pdflib' '--with-curl' '--with-png-dir=/usr' '--with-zlib' '--with-freetype-dir=/usr/local/include/freetype2' '--enable-gd-native-ttf' '--disable-posix-threads' '--enable-memory-limit=yes' '--enable-track-vars' '--enable-trans-sid' '--enable-debug=no' '--enable-inline-optimization'
 [2002-06-17 06:59 UTC] hholzgra@php.net
locale-dependant, does not work for locales where decimal_point is != '.' right now


 [2002-10-14 22:48 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 03:01:28 2024 UTC