Home > Web Front-end > Front-end Q&A > How many types of javascript numerical types are there?

How many types of javascript numerical types are there?

青灯夜游
Release: 2022-10-14 17:11:26
Original
2241 people have browsed it

Javascript has only one numerical type: floating point type. JavaScript internally stores numbers as 64-bit floating point types, so there is actually no integer type in JavaScript. According to the number format in JavaScript, the range of integers that can be represented is "[-2^53 ~ 2^53]", including boundary values; but it should be noted that array indexes, bit operators, etc. use 32-bit integers. .

How many types of javascript numerical types are there?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, there is only one numerical type, which is internally represented as a 64-bit floating point number. So there is actually no integer type in JavaScript.

print(3/2); // 1.5
Copy after login

It can be seen that the values ​​​​are processed as floating point numbers.

Because JavaScript internally stores numbers in 64-bit floating point types, regardless of integers or decimals. What is confusing is that some bit operations must require integers to run, so 64-bit floating point numbers are automatically converted into 32-bit integers. Then perform bit operations. In JavaScript, 1 and 1.0 are the same

1 == 1.0 true
0.1+0.2 == 0.3 false
0.3/0.1 = 2.99999999996
(0.3-0.2) === (0.2-0.1) false
Copy after login

In short, be careful when performing operations involving decimals

According to the number format in JavaScript (64-bit floating point format defined by IEEE-754), The range of integers that can be represented is [-2^53 ~ 2^53], including boundary values. However, it should be noted that array indexing, bitwise operators, etc. use 32-bit integers.

The highest precision of floating point values ​​is 17 decimal places, but its accuracy is far less than that of integers when performing arithmetic calculations. For example, the result of adding 0.1 to 0.2 is not 0.3, but 0.30000000000000004. This small rounding error makes it impossible to test specific floating point values.

Because binary floating point numbers can accurately represent 1/2, 1/8, etc., but cannot accurately represent 1/10, 1/100, etc. So simple numbers like 0.1 cannot be represented accurately.

Due to precision issues with floating point numbers, unexpected results may be obtained during comparison:

print((0.1+0.2) == 0.3); // false
print(0.1+0.2);          // 0.30000000000000004
print(0.3);              // 0.3

print((10/3-3) == (1/3));// false
print(10/3-3);           // 0.3333333333333335
print(1/3);              // 0.3333333333333333
Copy after login

For integers, as long as integers within 53 bits are used, this will not occur. Accuracy issue, you can use it with confidence.

In addition to being represented in decimal, integers can also be represented by literals in octal (base 8) or hexadecimal (base 16). Among them, the first digit of the octal literal must be zero (0), followed by a sequence of octal digits (0 to 7). If the value in the literal exceeds the range, leading zeros are ignored and the following value is interpreted as a decimal value.

Octal literals are invalid in strict mode and will cause JavaScript engines that support this mode to throw an error.

The first two digits of a hexadecimal literal value must be 0x, followed by any hexadecimal digit (0~9 and A~F). Among them, the letters A to F can be uppercase or lowercase.

Since saving floating-point values ​​requires twice the memory space as saving integer values, ECMAScript will lose no time in converting floating-point values ​​into integer values. Obviously, if the decimal point is not followed by any digits, then the value can be saved as an integer value. Similarly, if the floating-point value itself represents an integer (such as 1.0), then the value will also be converted to an integer.

Numeric object

#Just like a string value corresponding to a string object, a numerical value also has a corresponding numerical object, namely Number .

Numbers can also directly call the properties corresponding to the value:

print((6).toString()); // 6
Copy after login

Note that in this example, the value needs to be added in parentheses, otherwise the period will be considered a decimal point.

The use of Number is similar to the use of String. Type conversion, creation of numerical objects, etc. can be performed.

When performing type conversion, if the conversion is unsuccessful, Number returns NaN, and the same is true when using numerical objects.

var a = Number('x');
print(typeof a, a);       // number NaN
var b = new Number('x'); 
print(typeof b, b);       // object [Number: NaN]
Copy after login

Number has 5 special attributes (read-only), namely:

  • MAX_VALUE : The maximum value of a positive number, and if it is larger, it will become Infinity

  • MIN_VALUE :The minimum value of a positive number, any smaller it will become 0

  • NaN :Not a Number

  • NEGATIVE_INFINITY: Negative infinity, that is -Infinity

  • POSITIVE_INFINITY: Positive infinity, that is, Infinity

print(Number.MAX_VALUE);         // 1.7976931348623157e+308
print(Number.MIN_VALUE);         // 5e-324
print(Number.NaN);               // NaN
print(Number.NEGATIVE_INFINITY); // -Infinity
print(Number.POSITIVE_INFINITY); // Infinity
Copy after login

Infinity

When the numerical operation result exceeds the upper limit of numbers that JavaScript can represent, the result is a special infinity value (Infinity). If the negative value exceeds the range of negative numbers that JavaScript can represent, The result is -Infinity.

When the operation result is infinitely close to zero and smaller than the minimum value that JavaScript can express (underflow), the result is 0. When a negative number underflows, the result is -0, and a positive number underflows. overflow, the result is 0.

JavaScript predefines the global variables Infinity and NaN, which are both read-only variables.

由于内存的限制,ECMAScript并不能保存世界上所有的数值。ECMAScript能够表示的最小数值保存在Number.MIN_VALUE中——在大多数浏览器中,这个值是5e-324;能够表示的最大数值保存在Number.MAX_VALUE中——在大多数浏览器中,这个值是1.7976931348623157e+308。如果某次计算的结果得到了一个超出JavaScript数值范围的值,那么这个数值将被自动转换成特殊的Infinity值。具体来说,如果这个数值是负数,则会被转换成-Infinity(负无穷),如果这个数值是正数,则会被转换成Infinity(正无穷)。 

如果某次计算返回了正或负的Infinity值,那么该值将无法继续参与下一次的计算,因为Infinity不是能够参与计算的数值。要想确定一个数值是不是有穷的(换句话说,是不是位于最小和最大的数值之间),可以使用isFinite()函数。

NaN

NaN是一个特殊的数值,这个数值用于表示一个本来要返回数值的操作数未返回数值的情况(这样就不会抛出错误了)。

对于NaN,要记住的一点是,只要运算中出现NaN,结果就一定是NaN,就算是"NaN*0"这样的运算,也一样是NaN。只要对NaN运行比较运行,结果就一定是false,就算"NaN==NaN"/"NaN!=NaN"等,都是false。

要判断一个值是否为NaN,可以使用isNaN()函数:

print(isNaN(NaN));  // true
print(isNaN(0/0));  // true
Copy after login

在基于对象调用isNaN()函数时,会首先调用对象的valueOf()方法,然后确定该方法返回的值是否可以转换为数值。如果不能,则基于这个返回值再调用toString()方法,再测试返回值。

也可以使用x!==x来判断x是否为NaN,只有在x为NaN的时候,这个表达式的值才为true。

inFinite()

isFinite函数用于判断一个数是否为“正常”的数值:

print(isFinite(Number.NaN));               // false
print(isFinite(Number.NEGATIVE_INFINITY)); // false
print(isFinite(Number.POSITIVE_INFINITY)); // false
Copy after login

除了以上3个特殊值外,其他值的结果都为true

假如x是一个普通数值,则有:

x/0 = Infinity
x%0 = NaN
x/Infinity = 0
x%Infinity = x
0/0 = NaN
0%0 = NaN
Infinity/x = Infinity
Infinity%x = NaN
Infinity/Infinity = NaN
Infinity%Infinity = NaN
Copy after login

完整输出如下:

print(0 / 0);           // NaN
print(3 / 0);           // Infinity
print(Infinity / 0);    // Infinity
print(0 % 0);           // NaN
print(3 % 0);           // NaN
print(Infinity % 0);    // NaN
----------
print(0 / 4);           // 0
print(3 / 4);           // 0.75
print(Infinity / 4);    // Infinity
print(0 % 4);           // 0
print(3 % 4);           // 3
print(Infinity % 4);    // NaN
----------
print(0 / Infinity);           // 0
print(3 / Infinity);           // 0
print(Infinity / Infinity);    // NaN
print(0 % Infinity);           // 0
print(3 % Infinity);           // 3
print(Infinity % Infinity);    // NaN
Copy after login

负零与正零

负零与正零的值相等,不过毕竟带有符号,在一些运算中会有符号方面的差别,比如:

var zero = 0; 
var negz = -0;
Copy after login

此时,zero 等于 negz , 但1/zero 却不等于 1/negz。

【相关推荐:javascript视频教程编程基础视频

The above is the detailed content of How many types of javascript numerical types are there?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template