在PHP中,ceil()能否统一一个偶数分区?

I have read a bunch of questions and comments but haven't seen this mentioned, and since others may have the same question, I'm posting it here.

Considering floating point errors, is it ever possible to get a result one point higher than it should be from a ceil($a / $b) where the rest of $a / $b is 0?

If so, since I'm working with positive integers higher than 0, perhaps I should write my_ceil() where I check for $a % $b first and if it's not 0, add 0.1 to $a before calling the built-in function…

Looking at the ceil source code on github, it seems that it is dependent on your C library's ceil function.

If what you appear to be asking is how to use floating points including their correct non-absolute errors in deciding a ceil outcome, the following should guide you:

1) http://www.php.net/manual/en/language.types.float.php

Rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality [my emphasis]. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

Read up

Arbitrary precision math functions : http://uk1.php.net/manual/en/ref.bc.php

And gmp : http://uk1.php.net/manual/en/ref.gmp.php

2) You are caring about floats (as the single input value) when using ceil($float) when ceil will only ever round up to the nearest integer so whatever the floating value is, is irrelevant. You may be considering using the round() function instead. which has it's own ways of dealing with the above floating point inaccuracy issue.

http://php.net/manual/en/function.round.php


To answer the original question of Considering floating point errors, is it ever possible to get a result one point higher than it should be from a ceil($a / $b) where the rest of $a / $b is 0? the answer is YES because of two points:

  • Float is potentially significantly inaccurate in base10 numbers, and,

  • You are using the wrong function to get the output you want to the precision you need. In this situation using multiple float numbers you want a function that have built in precision required, such as gmp or maths functions .

Edit: To answer the question – YES, it (probably) can.

I guess this doesn't really have anything to do with ceil() but rather with if the division in question returns a float or integer type value.

According to http://php.net/manual/en/language.operators.arithmetic.php

The division operator ("/") returns a float value unless the two operands are integers (or strings that get converted to integers) and the numbers are evenly divisible, in which case an integer value will be returned.

So using at least one float value could produce a ceil() "error", but I should be fine with two integers.