Trying to understand this code behavior
I was working on a PHP project today and came across this code behavior
<?php
$x = 5.5;
$y = 0;
echo $z = floor($x * $y * -1);
?>
This worked -0
. Can anyone shed some light on why this is repeating -0
. But I expected that 0
Only when adding a floor does this seem to happen. I have tried the same in java.
class Sample {
public static void main(String[] args) {
float x =5.5f;
int y = 0;
System.out.println(Math.floor(x*y*-1));
}
}
This also prints -0.0
.
source to share
float
and double
have both positive 0s and negative 0s. When you multiple 0 * -1, you get -0, as specified in the IEEE 754 standard.
Note: 1/0 is positive infinity, but 1 / -0 is negative infinity.
You can see that http://ideone.com/tBd41l
System.out.println(0f * -1);
prints
-0.0
Math.floor is not required.
source to share