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

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/

Related

Issue working with numbers in Javascript [duplicate]

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

Javascript calculate with number over Number.MAX_VALUE [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.

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 adding extra precision when doing additions [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is JavaScript's Math broken?
Any ideas as to why:
(872.23 + 2315.66 + 4361.16) == 7549.05
return false in a Javascript console (e.g. Chrome Developer console)?
If I try it with my calculator, the left side does exact 7549.05... However, Javascript displays it as 7549.049999999999. I could "fix" it or round it, or... but WHY should I be doing that for simple additions?
Thanks
Marco Mariani answered a similar question a short time ago:
What Every Computer Scientist Should Know About Floating Point Arithmetic
and the shorter, more to the point:
http://floating-point-gui.de/
That is not Javascript adding extra precision, that is your computer's floating-point representation not being able to accurately represent your number. Get around it by using rational numbers or fixed-point (instead of floating-point) arithmetic.
By using decimals, you are using floating point numbers. You would have to know a bit about how floating point is represented in binary form, and how binary floating point addition works to understand why adding floating point numbers does not always resolve to what you want.
Here is a quick google result that you might want to glance at: The Complete Javascript Number Reference
Also, if you want to learn how floating point is represented in binary look at IEEE floating point on Wikipedia.
Your best bet in this case would be to round.
This is because of how floats are represented in the hardware (32 bits, probably, but it's the same in any number of bits). Basically the issue is you can't represent something like "7549.05" exactly (more on this issue in wikipedia).
So, for practical uses, if the numbers are currency, a good option is multiplying by 100 so they are always integers, and operating with ints (which will give good results when adding, substracting or multiplying).

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