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.
Related
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
This question already has answers here:
Javascript float subtract
(6 answers)
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 6 years ago.
So I am having issues with numbers:
I have a item, with a price of 483.65. I want to multiply this by 7. Expected result should be:
3385.55
However, Javascript reds this as:
3385.5499999999997
The number in the database is stored as a Float.
Here are screenshots to explain the situation better:
What am I missing?
be careful with floating point math operations.
thats because of floating point standard used by js vm.
one trick I found to fix it without external libs is to multiple by 1k and than divid by 1k.
483.65 * 1000 * 7 / 1000
strange but always works.
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.
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.
This question already has answers here:
javascript large integer round because precision? (why?)
(2 answers)
Closed 8 years ago.
I've just run into a peculiar issue with Javascript.
An API call returns some JSON as it normally does. One of the ids returned is the long number "10151920335784069".
However, in Javascript world that becomes "10151920335784068" (one subtracted).
A quick test in the (Chrome) console demonstrates it:
x = 10151920335784069;
console.log(x);
10151920335784068
x==10151920335784069;
true
Further more:
x==10151920335784067;
true
x==10151920335784066;
false
What is going on here?
JavaScript (ECMA 262 5th Edition) uses double-precision 64bit numbers in IEEE 754 format. That representation cannot store your value in question exactly so it must round it to the nearest value per the IEEE 754 specification.
Authors and users of APIs that use JSON data should keep this limitation in mind. Many runtime environments (such as JavaScript) have potentially unexpected behavior regarding such numerical values even though the JSON format doesn't impose any such limitations.
All numerical variables in Javascript are stored as 64-bit floating point integers, so at high levels of precision, with numbers above 32 bits, it will round and give you slightly inaccurate numbers
If you want to check if two numbers are roughly even, you can use this
if(Math.abs(num-check)/check<1e-8)){
alert("For most practical intents and purposes, they are equal!");
}