Issue working with numbers in Javascript [duplicate] - javascript

This question already has answers here:
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 5 years ago.
I'm new in developing javascript apps, i have a doubt about a behaviour that i'm going to try to explain.
If i execute:
Number(5555555555555555);
Result: 5555555555555555
But if i execute:
Number(55555555555555555);
Result: 55555555555555550
Anybody can explain to me what is the reason of this? Thanks!!

If you need to work with such big numbers I would suggest you use some of the big integer libraries such as this. The reason this happens as far as I know is the way processors and memory work. It's no related to some "bug" in JS.

Integers (numbers without a period or exponent notation) are accurate up to 15 digits. Javacript simply adds zeros to keep the number accurate in terms of its digit length.
Documentation

Related

Remainder in Node.js vs Python [duplicate]

This question already has answers here:
JavaScript % (modulo) gives a negative result for negative numbers
(13 answers)
Closed 4 years ago.
I am a thirteen-year-old who is learning programming. I know Javascript and Python pretty well, but I mostly program Javascript using Node.js as my compiler.
I have a pretty naïve question. I am writing an implementation of a circular buffer, and I need to know the remainder when -7 is divided by 3. I went to a Node.js console(version 11.1.0) and a Python console(Python 3.7) and typed in -7 % 3. In the Node.js console, it printed -1, but in the Python console, it printed 2. My question is partially mathematical: Which answer is correct, -1 or 2?
Both are correct, but they are different things. Python uses actual modulus, which is positive. Javascript does a remainder, which can be negative.
If you always need a positive value you can add (a multiple of) the divisor into the value and make sure it’s always larger than zero and will always get the same answer. Or check afterwards for a negative value and add the divisor to get the modulus.

Why is this false? Math.ceil(5.0000000000000009) === Math.ceil(5.00000000000000009) // false [duplicate]

This question already has an answer here:
What is the standard solution in JavaScript for handling big numbers (BigNum)?
(1 answer)
Closed 8 years ago.
I'm looking for a Mathematical solution that deals with really (long, big, huge, storms) numbers. I haven't found anything yet, But I don't wanna think that this problem hasn't be solve at this time. I'm looking for an easy Number solution, like Microsoft Excel Precision (30 decimals), or a BigInteger (Java) solution. in Javascript of course.
While looking for an big integer library for an ElGamal crypto implementation I tested several libraries with the following results:
I recommend this one: Tom Wu's jsbn.js (http://www-cs-students.stanford.edu/~tjw/jsbn/)
Comprehensive set of functions and fast
Leemon Baird's big integer library (http://www.leemon.com/crypto/BigInt.js)
Comprehensive set of functions and pretty fast
BUT: Negative number representation is buggy!
bignumber.js (https://github.com/MikeMcl/bignumber.js)
Pretty complete set of functions
BUT: Converting really big numbers from strings into BigNumber objects result in INFINITY
Scheme arithmetic library for JavaScript (https://github.com/jtobey/javascript-bignum)
JS-Implementation of Scheme arithmetic functions
BUT: No function for y= x^e mod n
I haven't tested this by myself: BigNumber (http://jsfromhell.com/classes/bignumber)
Functions for high precision claculations
BUT: It's said to be slow due to internal representation of numbers as strings
There's a BigInteger library for JavaScript available here:
jsbn.js
(Note that I haven't used this myself. Try it and see what you think.)
There is also Silent Matt's library for Big Integers. It does not handle decimals.

Why is .075 * 100 coming out as 7.499999999999996 in Javascript? [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 8 years ago.
I'm learning Javascript at the moment and the particular lesson that I'm on right now is showing how to turn 1.075 into 7.5% for display purposes.
the math looks as follows:
(1.075 - 1) * 100
and this is displaying in the results as 7.499999999999996. Why in the world is it calculating like this in Javascript? Every calculator that I used to do the exact same math came up as 7.5, as it should. I didn't even need a calculator to realize something was odd, but I wanted to run the same numbers on as many calculators as I could just to reassure myself. I'm actually shocked that the guy doing the tutorials didn't say a single thing about that, other than how to fix it to display only 2 decimal places, because this just seems so odd. So what be going on? I'm crazy curious about what's going on here.
Thanks!
I want to apologize to everyone that was obviously very upset by my asking a duplicate question, and decided to downvote me for it. I would have been just as well searching for an answer, only I don't even know how I could have began to query for such a question.
To the people that answered, and linked me to another article answering my question; thank you very much, and I apologize for the duplicate :)
Some decimal numbers cannot be accurately stored as floating point numbers since they are periodic in binary. 1075/1000 is such a number.
http://floating-point-gui.de/

Javascript doesn't like 16.1 value? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is JavaScript’s Math broken?
Funny question, but why at 16.1 javascript become "crazy"? :)
Code :
var value1=16.1;
var value2=16.2;
console.log(value1 * 1000);
console.log(value2 * 1000);
Output :
16100.000000000002
16200
Why?
That's because javascript casts everything to a double internally. As a result, all calculations pick up some noise due to floating point inaccuracy: Floating point inaccuracy examples
One way to fix this issue, is to just round down to the nearest int after all intermediate calculations.
Answer Copy From Here
It's not a javascript problem, it's a problem related to any programming language using floating point numbers, see
Is floating point math broken?
for explanation of the root problem and for some useful workarounds too.

javascript round problems [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is JavaScript's Math broken?
I've some problems with javascript.
If I do this:
alert(6.000 * 1.050);
I expect 6.3, but I get 6.30000001
can anybody help me ? or explain why this happens?
Here you can simple use the method toFixed() in java-script
alert(parseFloat(6.000 * 1.050).toFixed(1));
They're called floats, and sometimes have a little bit of inaccuracy.
http://en.wikipedia.org/wiki/Floating_point#Accuracy_proble
Standard problem; decimals can't be stored with infinite precision in general, so most programming languages have data types that approximates them and show a rounded version. Problem is that multiplication or subtraction can cause the inaccuracies to show.
In the end, you'll just have to round probably.

Categories

Resources