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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 04:01:28 2024 UTC