php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47208 Converting Float/Double to Integer bug.
Submitted: 2009-01-24 13:44 UTC Modified: 2009-01-24 16:23 UTC
From: superdude at hotmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.8 OS: win32/64 possibly others.
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
35 + 19 = ?
Subscribe to this entry?

 
 [2009-01-24 13:44 UTC] superdude at hotmail dot com
Description:
------------
When multiplying a float/double that the has value of 1.003, converting it to a integer, will always return the expected value, minus 1.

Reproduce code:
---------------
<?php
$x = 1.003;
echo (int)($x*1000)."<br>\n";
echo (int)($x*10000)."<br>\n";
echo (int)($x*100000)."<br>\n";
?>

Expected result:
----------------
1003
10030
100300

Actual result:
--------------
1002
10029
100299

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-01-24 16:23 UTC] mattwil@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.

1.003 is stored as slightly less than that (close as possible), which can be seen when you show enough digits:

printf('%.20f', 1.003);

Gives 1.00299999999999989164 for me...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC