A programmer must know the principle of floating-point arithmetic detailed
Guide: floating-point arithmetic is a very technical content of the topic, not easy to grasp. Many programmers are not sure what is wrong with the = = operator when comparing float/double types. Many people often make mistakes when using float/double for currency calculations. This article is the essence of this series, and all software developers should read it.
As your experience grows, you definitely want to get to know the details of some common things, and floating-point arithmetic is one of them.
1. What is a floating point number?
During the development of computer system, many methods have been proposed to express real numbers.
"1" is a typical example of a fixed point number relative to a floating point . In this expression, the decimal point is fixed somewhere in the middle of all the numbers in the real number. The expression of currency can be used in this way, such as 99.00 or 00.99 can be used to express a four-bit precision (Precision), after the decimal point has a two-bit currency value . Because the decimal position is fixed, the corresponding value can be expressed directly using a four-bit value. The number data type in SQL is defined using fixed-point numbers.
"2" There is also a proposed expression in the form of rational numbers, that is, the ratio of two integers to express real numbers.
The disadvantage of the fixed-point number expression method is that its form is too stiff, and the fixed decimal point position determines the integer part and fractional part of the fixed digit, which is not conducive to the expression of special large number or special small number. In the end, most modern computer systems adopt the so-called floating-point representation .
"3" floating-point expression , which uses scientific notation to express real numbers, that is, a mantissa (mantissa), a cardinality (base), an exponent (Exponent) , and a symbol representing positive and negative to express real numbers. For example, 123.45 using the decimal Science notation can be expressed as 1.2345x102, of which 1.2345 is the mantissa, 10 is the base, and 2 is the exponent. Floating point numbers use an exponent to achieve the effect of a floating decimal point, allowing the flexibility to express a larger range of real numbers.
Tip: The mantissa is sometimes called a valid number (Significand). The mantissa is actually an informal statement of valid numbers.
The same values can be expressed in a number of floating-point numbers, such as 123.45 in the above example can be expressed as 12.345x101,0.12345x103 or 1.2345x102. Because of this diversity, it is necessary to standardize it to achieve the goal of unified expression. The canonical (normalized) floating-point representation has the following form:
D.dd...dxβe, (0≤d i <β)
A programmer must know the principle of floating-point arithmetic detailed