float number range in php. -0 is float?

2019-02-21 06:18发布

问题:

when I'm converting string '-0'in float it returns float type -0

example

$x = '-0';
$y = (float)$x;

result => float -0

why -0 is float number ?

回答1:

The IEEE 754 standard, which is how almost all computer languages implement floating point numbers, has both a +0 and a -0 value.

For most operations, +0 and -0 can be used interchangeably.

However, operations that error out to infinity, use +0 or -0 to go to +Infinity and -Infinity.

Because -0 is a valid floating point number, it makes perfect sense that casting -0 to a float would do exactly as you asked --- returning a floating point number with a value of exactly -0.



回答2:

We all know that all integers are floats. And in PHP from here

Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +)

So -0 is an int which is normally a float