Javascript中的浮点数和整数的区别? [英] Difference between floats and ints in Javascript?
问题描述
var isNegative =数字< 0.0 ||数字== 0.0&& 1 /数字< 0.0;
我认为这样一个最初看起来奇怪的签名检查的原因是识别 -0
为负值,但有没有任何理由使用 0.0
而不是 0 $ c在JavaScript中只有一个数字类型 - wikipedia.org/wiki/IEEE_754-1985#Double-precision_64_bitrel =nofollow noreferrer> IEEE 754双精度浮点数。
请参阅对于这个事实的一些后果,这些问题:
2015更新
现在我们有: 不久之后,我们将拥有:http://www.youtube.com/watch?
> 键入数组
<这意味着JavaScript中可用的数字类型的数量将会从一个增长: b
$ b
至少在 WebAssembly :
- 8位整数(有符号和无符号)
- 16位整数(有符号和无符号)
- 32位整数(有符号和无符号)
- 64位整数(有符号和无符号)
- 32位浮点
- 64位浮点数
(技术上所有整数类型的内部表示在最低级别是无符号的,但是不同的运算符可以把它们看作有符号或无符号的,例如int32.sdiv
与int32.udiv
等)
<这些可在键入数组中找到:
- 8位二进制补码有符号整数
- 8位无符号整数
- 8位无符号整数(b)
- 16位二进制补码有符号整数
- 16位无符号整数
- 32位二进制补码有符号整数
- 32位无符号整数
- 32位IEEE浮点数
- 64位IEEE浮点数
- double
- 浮动
- 浮动
- floatis h
- 16位二进制补码有符号整数
I'm looking through some of the code from the Google Closure Library and I found this line:
var isNegative = number < 0.0 || number == 0.0 && 1 / number < 0.0;
I've figured that the reason for such an initially strange looking sign-check is to identify
-0
as negative, but is there any reason to use0.0
instead of0
?解决方案There is only one number type in JavaScript – the IEEE 754 double precision floating-point number.
See those questions for some consequences of that fact:
- Avoiding problems with JavaScript's weird decimal calculations
- Node giving strange output on sum of particular float digits
- Javascript Infinity Object
2015 Update
It's been over 4 years since I wrote this answer and the situation is much more complicated now.
Now we have:
Soon we'll have:
It means that the number of numeric types available in JavaScript will grow from just one:
- 64-bit floating point (the IEEE 754 double precision floating-point number - see: ECMA-262 Edition 5.1, Section 8.5 and ECMA-262 Edition 6.0, Section 6.1.6)
to at least the following in WebAssembly:
- 8-bit integer (signed and unsigned)
- 16-bit integer (signed and unsigned)
- 32-bit integer (signed and unsigned)
- 64-bit integer (signed and unsigned)
- 32-bit floating point
- 64-bit floating point
(Technically the internal representations of all integer types are unsigned at the lowest level but different operators can treat them as signed or unsigned, like e.g.
int32.sdiv
vs.int32.udiv
etc.)Those are available in typed arrays:
- 8-bit twos complement signed integer
- 8-bit unsigned integer
- 8-bit unsigned integer (clamped)
- 16-bit twos complement signed integer
- 16-bit unsigned integer
- 32-bit twos complement signed integer
- 32-bit unsigned integer
- 32-bit IEEE floating point number
- 64-bit IEEE floating point number
asm.js defines the following numeric types:
- int
- signed
- unsigned
- intish
- fixnum
- double
- double?
- float
- float?
- floatish
这篇关于Javascript中的浮点数和整数的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 8位二进制补码有符号整数