php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48462 sprintf error when multiplying float and displaying digit
Submitted: 2009-06-03 17:03 UTC Modified: 2009-06-04 14:25 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:1 (33.3%)
From: courtois at nouvo dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.9 OS: XP
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: courtois at nouvo dot com
New email:
PHP Version: OS:

 

 [2009-06-03 17:03 UTC] courtois at nouvo dot com
Description:
------------
This a followup to bug #48455 which has been closed.

I observed same bug with different values.

I'm affraid rasmus' answer is incorrect.

3498 is represented with 12 bits. A float holds 24 bits which is far enough to contain 3498 without any single bit of precision loss.

Anyway, the same program in C works fine:

#include <stdio.h>

main()
{
   float f;

   f=34.98*100;
   printf("%6d %f\n",(int)f,f);
}

thus the problem is NOT with the float representation.

Moreover the following script should return twice the same thing:

$x=34.98*100;

print "x:".$x." sprintf:".sprintf('%d',$x);

but the unexpected result is:

x:3498 sprintf:3497

If the first conversion from float to string works fine, why doesn't the second conversion work ?



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-04 14:25 UTC] jani@php.net
Your C program is not same as what PHP does, try change the float to 
double..
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun May 11 17:01:27 2025 UTC