Why is the result of PHP and JavaScript rounded up (0.1 + 0.7) * 10) Not 8?

Source: Internet
Author: User
Php code intval (0.7 + 0.1) * 10) php code

intval((0.7+0.1)*10) 
Reply content: This is related to the decimal representation of the computer,

Generally, decimal places use floating point numbers. :
Floating Point in Computer
A floating point refers to a number with decimal places. A floating point operation is a four-digit decimal point operation. It is often used to measure the computation speed of a computer. Most computers use binary notation (B = 2. Bit (Bit) is the unit used to measure the storage space required by floating-point numbers, usually 32-bit or 64-bit. And Double Precision .

For example, a single-precision floating point number consists of 32 bits. According to IEEE 754 Standard, 32-bit
1-bit is a sign)
The 8-digit index (exponent)
The 23th digit is a numerical value (fraction)

As shown in:
The value of this number is

In this way, for example, 0.5 can be expressed as sign = 0, exponent =-1, fraction = 1
But in fact, IEEE 754 The representation method is also optimized. For example, fraction must be a decimal point between 1 and 2, so fraction only represents the decimal point, and the actual exponent value is exponent + offset.

The actual formula is as follows:
For example, float of 0.5 indicates:
0 01111110 000 00000000 00000000 0000
0 indicates the symbol bit.
01111110 is the index bit, And the decimal value is 126, so the actual exponent is 126-127 =-1,
And 000 00000000 00000000 0000 is fraction, and the decimal value is 0,
So 0.5f =

The problem with this representation method is that many floating point numbers cannot be accurately expressed. For example, 0.1 floating point numbers are represented:
0 0111101 110011001100110011001101
The actual value is

What are the results of '0. 1 + 0.7? This is related to the characteristics of floating point operations. When a decimal floating point number is converted to a binary floating point number, the precision may be lost, and the calculation result must also be incorrect.

--

If you do not understand why the decimal floating point number is converted to a binary floating point number, you can choose to go back to the basic book ...... If you are reading a computer, and you haven't threw away the book, or remember how to search. Otherwise, the floating point number is saved as the ending number multiplied by the index. For example, 12345 is saved as 1.2345*10 ^ 4 (actually saved as 1.2345 and 10 ). The problem is that floating point numbers do not use decimal numbers to store the ending number. Therefore, 1.2345 must be converted to a binary format, which is something similar to 1.01010011. You can consider that the binary number in this form can only represent 1 +/-1/2 +/-1/4 +/-1/8 + /-... therefore, it is very likely that the decimal number you want to express cannot be expressed in the binary number with a limited number of digits, and the accuracy will be lost. In these two languages, the result of 0.7 + 0.1 is less than 0.8. Therefore, the result of the round operation is 7.

Why is 0.7 + 0.1 less than 0.8? In JS, why is the floating point value 0.1 + 0.2 = 0.30000000000000004, while 0.15 + 0.15 = 0.25 + 0.05 = 0.3? Answer in

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.