我有以下代码:
var str = "0x4000000000000000"; //4611686018427387904 decimal
var val = parseInt(str);
alert(val);
我得到了这个值:“4611686018427388000
”,即0x4000000000000060
我想知道JavaScript是否错误处理64位整数还是我做错了什么?
答案 0 :(得分:79)
JavaScript使用IEEE-754双精度(64位)格式表示数字。据我所知,这给你53位精度,或十五到十六位小数。您的号码比JavaScript可以处理的数字更多,因此您最终会得到近似值。
这并不是真的“错误处理”,但显然如果你需要对大数字进行全面精确,它就没有用。有一些JS库可以处理更大的数字,例如BigNumber和Int64。
答案 1 :(得分:8)
Chromium 57及更高版本本身支持任意精度整数。这被称为BigInt,对于其他浏览器也是being worked on。它比JavaScript实现dramatically faster。
答案 2 :(得分:1)
即,V8 JavaScript 是 Smalltalk 派生引擎。 (1980 年代至今)Lisp 和 Smalltalk 引擎支持使用
这些类型的数字具有无限的精度,通常用作构建块以提供
注意:我是 Smalltalk、JS 和其他语言及其引擎和框架的长期实施者和开发者。
如果将多精度算术作为 JavaScript 的标准特性适当地 例如,在我 1998 年的 smalltalk 引擎之一中,我刚刚在 2.3GHz cpu 上运行: 定义为:(说明 来自 Lars Bak(我的当代作品)的 V8 引擎源自于 David Ungar 的来自 Smalltalk-80 的 SELF 作品的 Animorphic Smalltalk,随后演变为 JVM,并由 Lars for Mobile 重做,后来成为 V8引擎基础。 我提到这一点是因为 Animorphic Smalltalk 和 QKS Smalltalk 都支持类型注释,这使引擎和工具能够以类似于 TypeScript 为 JavaScript 尝试的方式来推理代码。 注释提示及其在语言、工具和运行时引擎中的使用提供了支持正确支持多精度算术类型提升和强制规则所需的多方法(而不是双分派)的能力。 这反过来又是在一个连贯的框架中支持 8/16/32/64 int/uint 和许多其他数字类型的关键。 来自 QKS Smalltalk 1998 的多方法 我预计随着 [10000 factorial] millisecondsToRun => 59ms
10000 factorial asString size => 35660 digits
[20000 factorial] millisecondsToRun => 271ms
20000 factorial asString size => 77338 digits
<BigInt>
多精度操作)factorial
"Return the factorial of <self>."
| factorial n |
(n := self truncate) < 0 ifTrue: [^'negative factorial' throw].
factorial := 1.
2 to: n do:
[:i |
factorial := factorial * i.
].
^factorial
<Magnitude|Number|UInt64>
示例Integer + <Integer> anObject
"Handle any integer combined with any integer which should normalize
away any combination of <Boolean|nil>."
^self asInteger + anObject asInteger
-- multi-method examples --
Integer + <Number> anObject
"In our generic form, we normalize the receiver in case we are a
<Boolean> or <nil>."
^self asInteger + anObject
-- FFI JIT and Marshaling to/from <UInt64>
UInt64 ffiMarshallFromFFV
|flags| := __ffiFlags().
|stackRetrieveLoc| := __ffiVoidRef().
""stdout.printf('`n%s [%x]@[%x] <%s>',thisMethod,flags,stackRetrieveLoc, __ffiIndirections()).
if (flags & kFFI_isOutArg) [
"" We should handle [Out],*,DIM[] cases here
"" -----------------------------------------
"" Is this a callout-ret-val or a callback-arg-val
"" Is this a UInt64-by-ref or a UInt64-by-val
"" Is this an [Out] or [InOut] callback-arg-val that needs
"" to be updated when the callback returns, if so allocate callback
"" block to invoke for doing this on return, register it as a cleanup hook.
].
^(stackRetrieveLoc.uint32At(4) << 32) | stackRetrieveLoc.uint32At(0).
-- <Fraction> --
Fraction compareWith: <Real> aRealValue
"Compare the receiver with the argument and return a result of 0
if the received <self> is equal, -1 if less than, or 1 if
greater than the argument <anObject>."
^(numerator * aRealValue denominator) compareWith:
(denominator * aRealValue numerator)
Fraction compareWith: <Float> aRealValue
"Compare the receiver with the argument and return a result of 0
if the received <self> is equal, -1 if less than, or 1 if
greater than the argument <anObject>."
^self asFloat compareWith: aRealValue
-- <Float> --
Float GetIntegralExpAndMantissaForBase(<[out]> mantissa, <const> radix, <const> mantissa_precision)
|exp2| := GetRadix2ExpAndMantissa(&mantissa).
if(radix = 2) ^exp2.
|exp_scale| := 2.0.log(radix).
|exp_radix| := exp2 * exp_scale.
|exponent| := exp_radix".truncate".asInteger.
if ((|exp_delta| := exp_radix - exponent) != 0) [
|radix_exp_scale_factor| := (radix.asFloat ^^ exp_delta).asFraction.
"" Limit it to the approximate precision of a floating point number
if ((|scale_limit| := 53 - mantissa.highBit - radix.highBit) > 0) [
"" Compute the scaling factor required to preserve a reasonable
"" number of precision digits affected by the exponent scaling
"" roundoff losses. I.e., force mantissa to roughly 52 bits
"" minus one radix decimal place.
|mantissa_scale| := (scale_limit * exp_scale).ceiling.asInteger.
mantissa_scale timesRepeat: [mantissa :*= radix].
exponent :-= mantissa_scale.
] else [
"" If at the precision limit of a float, then check the
"" last decimal place and follow a rounding up rule
if(exp2 <= -52 and: [(mantissa % radix) >= (radix//2)]) [
mantissa := (mantissa // radix)+1.
exponent :+= 1.
].
].
"" Scale the mantissa by the exp-delta factor using fractions
mantissa := (mantissa * radix_exp_scale_factor).asInteger.
].
"" Normalize to remove trailing zeroes as appropriate
while(mantissa != 0 and: [(mantissa % radix) = 0]) [
exponent :+= 1.
mantissa ://= radix.
].
^exponent.