2
Sep 02 '15
Did you actually read the documentation of ceil()? Do you also wonder why 0 === 0.0 evaluates to false? Welcome in the land of floating point arithmetic.
2
u/00Davo Sep 02 '15
Floating point arithmetic isn't the problem - an IEEE 754 float 0.0 compares equal to an int 0 in any other language allowing that comparison, without running into float imprecision issues.
PHP's type system is the problem. It's atrocious.
5
u/diarrhea_on_rye Sep 07 '15
I don't see the problem here.
0 == 0.0returns true.0 === 0.0returns false. The first is a value comparison. The second is a value and type comparison. Int is a different type than float.-1
u/00Davo Sep 07 '15
The problem is that
==is far, far too loose in terms of what type conversions it's willing to perform. Coercing the int 0 to the float 0.0 is "safe" and reasonable. Coercing the string "0aa9214251874b244f1ff7f8fb58aad69e8fa576" to the Boolean false is less so.
===is significantly more reliable. Unfortunately this is an example of it being a little too strict, with the end result that there isn't actually a genuine equality operator in PHP.1
u/richardwhiuk Dec 05 '15
No just add a cast.
1
u/00Davo Dec 06 '15
Casts in PHP always succeed, so they essentially just enforce the dangerous type coercions the language supports. (e.g.,
(int) "0aa9214251874b244f1ff7f8fb58aad69e8fa576" === 0) Not really a solution.1
u/richardwhiuk Dec 31 '15
Surely you know where the argument is coming from and therefore which type it is? Anyway if you want the check you are trying to do then do 0 === $x || 0.0 === $x
37
u/SnowdogU77 Aug 20 '15
That would be because ceil returns a float. 0.0 === ceil(0)
10
u/Carlos_Sagan Aug 20 '15
But why?
12
u/madsohm Aug 20 '15
It's just more convenient to keep on working on floats when you do stuff with floats.
Why should it change data type, just because you ask it to round it off?
Ninja edit: This is of course when using it with
ceil(0.0)or similar, not in the above use case.18
u/the_biz Aug 20 '15
the entire point of ceil / floor / round is to turn fractions into whole numbers, and integer is the default representation for whole numbers
it's like you write something that divides an unknown quantity by 2 and want to handle the case where the unkonwn quantity is odd, so you use 1 of ceil/floor/round. you can't be expected to type-check the result...
7
u/josefx Aug 28 '15
They also return a float in most other languages - the result can be bigger than PHP_INT_MAX.
2
u/coredumperror Aug 24 '15
I think this is because integers use integer division, meaning 3/2 == 1. While 3.0/2 == 1.5. You don't really want a variable you know is a float to get silently converted to a data type with different arithmetic rules.
Holy shit, did I just defend PHP? I feel dirty...
9
u/the_biz Aug 24 '15
actually 3/2 returns a double but 4/2 returns an int
9
u/coredumperror Aug 25 '15
Oh right, this is PHP. I forgot that it pretends to be C, but mostly fails.
And now I feel even dirtier, because I defended PHP and was wrong. This is all your fault! ;)
3
u/SnowdogU77 Aug 20 '15
Now that's a damn good question.
23
u/smog_alado Aug 20 '15
The most likely reason PHP's ceil returns floats is because that is what the ceil function in the C library does.
The most likely reason the C library returns floats is because this way you avoid a costly round trip floating point to integer and then back to floating point if the output of ceil is used in a floating point operation.
-1
u/stralytic Aug 20 '15
But why PHP? Half of it is global functions based on C, and then half of it is objects loosely based on Java which do the same things. It's mostly dynamic typed but then it does stupid static typed things like this.
7
u/smog_alado Aug 20 '15
It makes perfect sense for 0 to not be equal to 0.0 when using ===. The problem is that PHP's == is so shitty and loose that you can't ever trust it and therefore have to resort to the overly-strict ===
0
4
u/stralytic Aug 20 '15
00Davo makes a compelling argument as to the absurdity of this behaviour, beyond the simple 'understand data types better' hypothesis.
1
u/webdeverper Aug 22 '15
Always use epsilon compare method for float equality, unless you're designing rocket systems. Then don't use php, lol
24
u/BilgeXA Aug 20 '15
The only lol here is your lack of understanding of basic programming fundamentals, namely: data types.