Home>

Learn with me the precision of floating point numbers in javascript,Take everyone to really understand the floating point numbers of javascript,A reminder to watch out for precision traps in floating point arithmetic,

Most programming languages ​​have several numeric data types.But JavaScript has only one.You can use the typeof operator to see the type of a number.Whether it's an integer or a floating point number,JavaScript all simply classifies them as numbers.

typeof 17;//number
typeof 98.6;//number
typeof -21.3;//number

In fact, all numbers in JavaScript are double-precision floating-point numbers.This is a 64-bit encoded number developed by the ieee754 standard-"doubles". If this fact makes you wonder how javascript represents integers,Remember that double-precision floating-point numbers perfectly represent integers with up to 53 digits of precision.All integers from –9 007 199 254 740 992 (–253) to 9 007 199 254 740 992 (253) are valid double-precision floating-point numbers.So, despite the obvious lack of integer types in javascript,But it is perfectly possible to perform integer operations.

Most arithmetic operators can be calculated using integers, real numbers, or a combination of both.

0.1 * 0.9;//0.19
-99 + 100;//1
21- 12.3;//8.7
2.5/5;//0.5
21%8;//5

However, bit arithmetic operators are special.JavaScript does not directly operate on operands as floating point numbers,Instead, it is implicitly converted to a 32-bit integer and then operated on.(To be precise,They are converted to 32-bit big-endian 2's complement integers.) Take the bitwise OR operation expression as an example:

8 | 1;//9

Seemingly simple expressions actually require several steps to complete the operation.As mentioned before,The numbers 8 and 1 in JavaScript are both double-precision floating-point numbers.But they can also be expressed as 32-bit integers, that is, sequences of 32-bit 0s and 1s. The integer 8 is represented as a 32-bit binary sequence as follows:

00000000000000000000000000001000

You can also use the tostring method of the number type to see it yourself:

(8) .tostring (2) //"1000"

The parameter of the tostring method specifies its conversion base,This example is expressed in base 2 (that is, binary). The resulting values ​​omit the extra 0s (bits) at the left because they do not affect the final value.

The integer 1 is represented as a 32-bit binary as follows:

00000000000000000000000000000001

Bitwise OR expressions combine two sequences of bits.As long as any one of the two bits involved in the operation is 1, the bit of the operation result is 1. The results in bit mode are as follows:

00000000000000000000000000001001

This sequence represents the integer 9. You can verify using the standard library function parseint, again using 2 as the base:

parseint ("1000", 2);//9

(Similarly, leading zero bits are unnecessary.Because they do not affect the results of the operation.)

All bit operators work the same way.They convert operands to integers,Then use integer bit mode to perform the operation,Finally the result is converted to a standard javascript floating point number. In general,The javascript engine needs to do some extra work to make these conversions.Because numbers are stored as floating point numbers,It must be converted to an integer,Then convert back to a floating point number.However, in some cases,Arithmetic expressions and even variables can only participate in operations using integers,Optimizing compilers can sometimes infer these situations and store numbers internally as integers to avoid redundant conversions.

The final caveat about floating point numbers is,You should always be alert to them.Floating point numbers seem familiar,But they are notoriously imprecise.Even some of the simplest arithmetic operations seem to produce incorrect results.

0.1 + 0.2;0.300000000000004

Although 64-bit accuracy is already quite high,But double-precision floating-point numbers can only represent a limited set of numbers,It cannot represent all real number sets.Floating-point operations can only produce approximate results,Round to the nearest representable real number.When you perform a series of operations,As rounding errors accumulate,The calculation results will become increasingly inaccurate.Rounding can also cause some unexpected deviations from the laws of arithmetic we normally expect.For example, real numbers satisfy the associative law,this means,For any real number x, y, z, (x + y) + z=x + (y + z) is always satisfied.

However, for floating point numbers,Not always.

(0.1 + 0.2) +0.3;//0.60000000000000001
0.1+ (0.2+ 0.3);//0.6

Floating-point numbers weigh precision and performance.When we care about accuracy,Be careful of the limitations of floating point numbers.An effective solution is to use integer-valued operations whenever possible,Because integers do not need to be rounded.When doing currency related calculations,Programmers usually convert numerical values ​​to the smallest monetary unit proportionally before performing calculations.This allows calculations to be performed as integers.For example, if the above calculation is in US dollars,Then, we can convert it to an integer representation of cents for calculation.

(10 + 20) +30;//60
10+ (20 + 30);//60

For integer operations,You do n’t have to worry about rounding errors,But you still have to be careful that all calculations only work for integers from –253 to 253.

prompt

JavaScript numbers are double-precision floating-point numbers. Integers in JavaScript are just a subset of double-precision floating-point numbers,Instead of a separate data type Bit operators treat numbers as 32-bit signed integers.
  • Previous Detailed explanation of the most basic file and directory operation methods in Java programming
  • Next Jquery verify mobile phone number is correct