string - javascript Number constructor strange behaviour -


this question has answer here:

converting string number produces incremented value:

var n = '9999999999999999'; console.log(n); // -> 9999999999999999 var nn = number(n) console.log(nn); // -> 10000000000000000 

how avoid this?

9999999999999999 treated internally in javascript floating-point number. cannot accurately represented in ieee 754 double precision require 54 bits of precision (the number of bits log2(9999999999999999) = 53.150849512 , since fractional bits not exist, result must rouned up) while ieee 754 provides 53 bits (1 implict bit + 52 explicitly stored bits of mantissa) - 1 bit less. hence number gets rounded.

since 1 bit lost in case, 54-bit numbers representable, since nevertheless contain 0 in bit, gets lost. odd 54-bit numbers rounded nearest value happens doubled 53-bit number given default unbiased rounding mode of ieee 754.

[source]


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -