In PHPmanual, the rational number that can be accurately expressed in decimal format, such as 0.1 or 0.7, cannot be accurately expressed by any number of tails in binary format, therefore, the binary format cannot be converted without a slight loss of precision. {Code...} in example 2 and Example 3... PHP manual:
Rational numbers that can be accurately expressed in decimal form, such as 0.1 or 0.7. No matter how many tails are there, they cannot be accurately expressed by the binary used internally, therefore, the binary format cannot be converted without a slight loss of precision.
// example1$float = (0.1 + 0.7) * 10;echo (integer) $float; // 7echo floor($float); // 7// example2echo (integer) (1.5+1.5); // 3echo floor(1.5+1.5); // 3// example3echo (integer) (0.5*10); // 5echo floor(0.5*10); // 5
In Example 2 and Example 3, the floating pointAdd
AndMultiplication
Can operations preserve accuracy?
Reply content:
PHP manual says:
Rational numbers that can be accurately expressed in decimal form, such as 0.1 or 0.7. No matter how many tails are there, they cannot be accurately expressed by the binary used internally, therefore, the binary format cannot be converted without a slight loss of precision.
// example1$float = (0.1 + 0.7) * 10;echo (integer) $float; // 7echo floor($float); // 7// example2echo (integer) (1.5+1.5); // 3echo floor(1.5+1.5); // 3// example3echo (integer) (0.5*10); // 5echo floor(0.5*10); // 5
In Example 2 and Example 3, the floating pointAdd
AndMultiplication
Can operations preserve accuracy?
It's time to offer my blog again:
- Code mystery (4)-floating point number (from surprise to thinking)
- Code mystery (5)-floating point number (who stole your precision ?)
0.1 + 0.7
The result is0.7999999999999999
0.5
1.5
It can be exactly represented by a floating point number.
0.1
Binary:
Symbol bit 0 index 01111011 (-4) digits 1.10011001100110011001101 (1.60000002384185791015625)
Convert the number to decimal:0.10000000149011612
0.7
Binary:
Symbol bit 0 index 01111110 (-1) digits 1.01100110011001100110011 (1.39999997615814208984375)
Convert the number to decimal:0.699999988079071