Why is gender (0.9999999999999999999) = 1 and gender (0.9999999999999999) = 0?

The floor function in PHP behaves strangely. For 16 decimal values, it gives the value of the sexes, but increasing 1 decimal digit.

$int = 0.99999999999999999; 
echo floor($int); // returns 1 

$int = 0.9999999999999999; 
echo floor($int); // returns 0

$int = 0.99999999999999994; 
echo floor($int); // returns 0
  • Is it defined / explained somewhere, and at what point does it give a “round” meaning?
  • Is there any function that gives one 0way or another how many 9 decimal places?
+4
source share
2 answers

It is not floorthat rounds, it is the floating point math that does.

This line:

echo 0.99999999999999999;

1 (demo), 0.99999999999999999 , (64-?) , , 1.
0.99999999999999994 , , 0.9999999999999999.

  • / -, "" ?

, .
, " , ", , IEEE 754 standard.
, , .

  1. - , 0 , 9 ?

. , PHP 0.99999999999999999 1.
, .

, .
, :

PHP

, , , , .

, , 0.999... ( ) ( ) 1, .

+15
$float_14_digits = 0.99999999999999;
echo $float_14_digits; // prints 0.99999999999999
echo floor($float_14_digits); // prints 0

$float_15_digits = 0.999999999999999;
echo $float_15_digits; // prints 1
echo floor($float_15_digits); // prints 1
exit;

, "15", "17", . PHP . floor()

0

Source: https://habr.com/ru/post/1612899/


All Articles