Problems with parseInt method [duplicate] - javascript

This question already has answers here:
parseInt rounds incorrectly
(2 answers)
Closed 7 years ago.
The parseInt function made my number loose precision: the last two digits changed from 18 to 20:
console.log(parseInt('76561198236425518', 10));
76561198236425520
Why did that happen and how to fix it?

Numbers are stored as floating point with a 53-bit manttissa. This limits the precision you can have to less than what you have in that number of yours, hence it has to round to the nearest floating point number it can represent.
The actual number of bits needed to represent a number N can be calculated as about log2N or, if you're working on a calculator that can't calculate logarithms to base two, logxN/logx2.
The value of log276561198236425518, roughly 56.1, shows that it requires about 56 bits, which is why it's not exact near the end.

Related

Javascript "toLocaleString()" function chnages decimals to zero when number is very large [duplicate]

This question already has answers here:
Node.js Maximum Safe Floating-point Number
(4 answers)
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I am developing a react app and i need to format some numbers using toLocaleString function and i realize that the decimal part of the number changes to zero when the number is very
(10000.25).toLocaleString('en',{maximumFractionDigits: 2, minimumFractionDigits: 2});
//Output: 10,000.25
(10000000000000000000.25).toLocaleString('en',{maximumFractionDigits: 2, minimumFractionDigits: 2});
//Output: 10,000,000,000,000,000,000.00
why is this so? is it a bug or am I missing something?
Javascript compiler cannot handle very large numbers. Use Number.MAX_SAFE_INTEGER to check the last number it can handle. Try to consider using string type from the beginning if these numbers are not calculated (Maybe if you get it from an API kind of), or try using BigInt type.
For floating point, the maximum number it can handle to a precision of 1 decimal digit is 562949953421311 and it differs for other digits. This has been referred here. Try using the BigNumber class in your case.

Limit precision of the float number without rounding [duplicate]

This question already has answers here:
Truncate number to two decimal places without rounding
(43 answers)
Closed 2 years ago.
is there a way to have 2 numbers after comma without rounding the value. I want the exact value. Math.round() and toFixed() give the value rounded.
You can do the workaround with help of Math.ceil() and Math.floor() functions.
Another way, is treat is as an string and use .slice()
i.e:
number = number.slice(0, number.indexOf(".")+3); //this should give you 2 decimals
Number(number); //Convert it to "Number" again, so you can operate with it
Solution without type conversions
While solving the issue, you should bear in mind that bouncing back and forth between data types may cost you some of app performance wasted
Instead, I'd suggest to modify input number directly:
shift the dot n positions to the right by multiplying your number by 10 in power of n (10**n)
cut off what's left after dot, using bitwise OR (|) that implicitly turns the float into integer
divide the result by 10 in power of n to shift the dot n positions back to the left
Following is a quick live-demo:
const num = 3.14159265,
precision = 4,
limitPrecision = (n,p) => (0|n*10**p)/10**p
console.log(limitPrecision(num, precision))
.as-console-wrapper{min-height:100%;}

Javascipt decimal multiplication and division output [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Why in the output, the length of decimals differ in these two scenarios?
33 * .1 = 3.3000000000000003
where as
33 * .01 = 0.33
Any idea why its like that?
NB: 33 in the above calculation can be any integer
When you convert .1 or 1/10 to base 2 (binary) you get a repeating
pattern after the decimal point, just like trying to represent 1/3 in
base 10. The value is not exact, and therefore you can't do exact math
with it using normal floating point methods.
This is the reason why you end up answer like above.

decimal places limited to 15 in javascript [duplicate]

This question already has answers here:
What is the standard solution in JavaScript for handling big numbers (BigNum)?
(1 answer)
Is there a limit in Javascript to decimal places on big floats?
(1 answer)
Closed 5 years ago.
I'm trying to get a decimal number without any decimal places limit in javascript. I have two numbers in my code, 610 and 987. If I divide them in the default windows calculator, I get the number 1.6180339850173579389731408733784. This number has 31 decimal places. If I divide them using javascript, it automatically rounds this number and limits it to 15 decimal places. Since I need this number to calculate further, it has to be the full decimal number, otherwise the final result is wrong.
Currently I'm using this code:
var multiplicator = 987/610; // returns 1.618032786885246
var maxloop = input - 15;
for(var i = 0; i < maxloop; i++){
value *= multiplicator;
}
Since the variable multiplicator doesn't contain the whole decimal number, the value will be wrong for higher values, because of the rounding.
I already tried toFixed() function, but then I only get zeroes at the end of the decimal number instead of the correct decimal value and toFixed() allows only digits between 0 and 20.
var multiplicator = (987/610).toFixed(31); // wont work because toFixed() allows only digits between 0 and 20
Is there a way to get the full decimal number with all the 31 decimal places?

why does javascript Number function return wrong value for this number? [duplicate]

This question already has answers here:
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 6 years ago.
so I ran into this issue with javascript Number() function, it seems to return the wrong value:
the following code:
Number("10153396863200835")
returns 10153396863200836, not 10153396863200835
but if I do Number("10153396863200836") it returns 10153396863200836
and Number("10153396863200834") returns 10153396863200834
so I'm confused what's going on? thanks
Because JavaScript's numbers are IEEE-754 double-precision binary floating point (frequently known as "double"). They have only about 15 digits of decimal precision. Your number, 10153396863200835, is much larger than that.
In doubles, the maximum whole number before we stop being able to represent the next whole number is 9,007,199,254,740,9921. 9,007,199,254,740,993 cannot be represented by doubles; 9,007,199,254,740,994 can. That's because at that scale, the least significant bit is worth 2 decimal. The further you go, the bigger those gaps get, as the least significant bit starts being worth 4 decimal, then 8, then...
Modern JavaScript has a handy constant for the value right before this value, 9,007,199,254,740,991: Number.MAX_SAFE_INTEGER. It's defined as the last whole number in a double where adding 1 gives you the next consequtive whole number. The name is slightly misleading, as 9,007,199,254,740,994 or indeed 10,153,396,863,200,836 aren't going to suddenly stop having those values, it's just that math with them starts getting...interesting.

Categories

Resources