typeof 17;//number typeof 98.6;//number typeof -21.3;//number
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
8 | 1;//9
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:
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:
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.)
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.
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- xcode - pod install [!] no `podfile 'found in the project directory
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file