Why is the result of PHP and JavaScript rounding ((0.1+0.7) *10) not 8?
Source: Internet
Author: User
Keywordsintval0.10.7php10
PHP code
Reply content:
This is related to the fractional representation of the computer,
Typically, decimals are used as floating-point numbers
Indicated by:
floating-point numbers in a computer
Floating point refers to a number with decimals, floating point arithmetic is a decimal arithmetic, commonly used to measure the speed of computer operations. Most computers are represented by binary (b=2). Bit
is the unit that measures the amount of storage space required for a floating-point number, usually 32-bit or 64-bit, and
is called single
-precision and double-precision, respectively.
A single-precision floating-point number, for example, consists of 32 bit bits. According to IEEE 754
Standard, available in 32-bit
1-bit is sign bit
8 bits is the digit (exponent)
23 bits is numeric (fraction)
As shown in the following:
So this number is the value of
So for example, for 0.5, it can be represented as Sign = 0, exponent =-1, fraction = 1
But actually IEEE 754
Some optimizations are also made to the presentation method, such as fraction must be a decimal between 1-2 so that fraction only represents the decimal place, and the actual value of exponent is exponent + offset.
The actual calculation formula is:
For
example, a float of 0.5 means:
0 01111110
00000000
00000000
0000 Where 0 is the sign bit 01111110 is the digit, the decimal is 126, so the actual exponent is 126-127 =-1,
and
00000000
00000000
0000 is fraction, the decimal is 0, so 0.5f =
The problem with this representation is that many floating-point numbers cannot be accurately represented, such as 0.1 floating-point numbers:
0 0111101 110011001100110011001101
The actual value is
What do you see as the result of ' 0.1 + 0.7 '? This is related to the characteristics of floating-point arithmetic. When the decimal floating-point number is converted to a binary floating-point number, the precision can be lost, and the result must be error.
--
If you do not understand why decimal floating-point numbers are converted to binary floating-point numbers, first you can choose to go back to the basic book ... If you're reading a computer, and you haven't thrown the book away, or you remember how to search. Otherwise, this is probably the case: floating-point numbers are saved as the mantissa multiplied by the exponent, for example, 12345 will be saved as 1.2345 * 10 ^ 4 (Actual save 1.2345 and 10). The problem is that floating-point numbers do not hold the mantissa in decimal, so 1.2345 must be converted to the corresponding binary form, which is something like 1.01010011. You can consider that this form of binary number can only represent 1 +/-1/8 +/-... Therefore, it is possible that the decimal number you want to express is not shown in the binary number of the finite number of digits, and then the accuracy is lost. Because in both languages, the result of 0.7 + 0.1 is less than 0.8, so the result of rounding is 7.
Why 0.7 + 0.1 is less than 0.8, you can see me in JS, why floating-point value 0.1+0.2=0.30000000000000004, and 0.15+0.15=0.25+0.05=0.3? The answer in
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.