php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33009 floor does not work with some integers as argument
Submitted: 2005-05-11 20:12 UTC Modified: 2005-05-11 21:03 UTC
From: edelmar at ditech dot com dot br Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 4.3.11 OS: Win 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: edelmar at ditech dot com dot br
New email:
PHP Version: OS:

 

 [2005-05-11 20:12 UTC] edelmar at ditech dot com dot br
Description:
------------
Using PHP Version 4.3.11 on Windows xp (my development machine).

I believe this behavior of floor function is a bug. The number 7560 passed to it is an integer and would not be changed by floor.

I am using 100 times number because PHP has no function like 'truncate'. I want 2 decimals and there is no way to do it with regular functions.

Its like 7560 has some special properties because this error does not happens with a lot of other numbers.

Thank you.

Reproduce code:
---------------
echo (75.6 * 100).'<br>'; // prints 7560 - OK
echo intval(75.6 * 100).'<br>'; // prints 7559 - ?
echo (int)(75.6 * 100).'<br>'; // prints 7559 - ?
echo floor(75.6 * 100).'<br>'; // prints 7559 - ?
echo ceil(75.6 * 100).'<br>'; // prints 7560 - OK
echo round(75.6 * 100).'<br>'; // prints 7560 - OK



Expected result:
----------------
7560
7560
7560
7560
7560
7560

Actual result:
--------------
7560
7559
7559
7559
7560
7560

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-05-11 20:41 UTC] edelmar at ditech dot com dot br
$d = 75.6 * 100;
$d = (string)$d;
echo floor($d).'<br>'; // prints 7560, wich is expected... how embarassing.
 [2005-05-11 21:03 UTC] tony2001@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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC