php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64675 Warning about floats for ceil and floor
Submitted: 2013-04-19 11:16 UTC Modified: 2016-06-14 15:11 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: phpmpan at mpan dot pl Assigned: cmb (profile)
Status: Wont fix Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phpmpan at mpan dot pl
New email:
PHP Version: OS:

 

 [2013-04-19 11:16 UTC] phpmpan at mpan dot pl
Description:
------------
As per bug 64674, I request including warning box about floating point numbers for `ceil` and `floor`. I know that floating point numbers data type already warns about that, but it seems that people are still hit by this issue.

The warning would say something along these lines:
This function operates on floating point numbers. Floating point numbers are only an approximation of the actual value and may differ a bit from the expected result. It may happen that floating point values are not integers even if they're expected to be integers if the calculation would be carried in real numbers domain. See: http://php.net/manual/en/language.types.float.php

If examples are required to depict the issue:
floor:
    $input = (29 / 100.0) * 100.0;
    echo floor($input); // Outputs: 28, expected: 29
ceil:
    $input = (7 / 100.0) * 100.0;
    echo ceil($input); // Outputs: 8, expected: 7
    

Reason
======
Behaviour of these functions is obvious for anyone, who has computer science background, but countless web developers never touched the theory and know nothing about floating point numbers. They can be easily affected by misunderstanding of what FP calculations are and they may expect a result different from what these functions actually return.

Also bug 23498 is still open. I do realize that the idea makes no sense, because for most floating point arguments both `ceil` and `floor` make no sense, but it would be nice to close the related bug from 2003.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-14 15:11 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2016-06-14 15:11 UTC] cmb@php.net
In my opinion, the floating point precision limitation is not
particularly related to ceil() and floor(), and adding a warning
to *all* functions that accept or return a float value appears to
be distracting. There are already links to the float man page
which has a highly visible (I'd even call it intrusive) warning
regarding precision limits, so I don't see the need to further
document the issue.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 10:01:28 2024 UTC