Unexpected ParseFloat result [duplicate] - javascript

This question already has answers here:
Floating point inaccuracy examples
(7 answers)
Closed 7 years ago.
Today, I have seen strange issue with parseFloat in Javascript. When I an doing addition operation of the value I have seen some strange values, below are the examples:
parseFloat("31.099")+1;
Output:
32.099000000000004
Why do I have only problem with 31.099?

Floating numbers are always strange. Check out some descriptions in PHP Manual as well. There is a warning about precision.

Most of the programming languages do this, when you add an int to a floating number, it will make an approximation of the result. One work around would be to create your floating type and separate the floating value from the integer value.

Yes, floating point numbers are not always precise. It's because fractions cannot be precisely represented in binary.
To fix it, use toFixed() to round floating point numbers to the amount of precision you want. For example, for two decimal places:
num.toFixed(2)

Related

JavaScript - Incorrect automatic rounding decimal numbers [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I have big numbers coming from the backend service in my JS application. I don't want to lose any of the digits after the decimal place.
However, when a number like 999999999999999.99 is assigned to a variable in JS, it rounds it to 1000000000000000 and 99999999999999.99 turns to 99999999999999.98.
Both numbers are within the limits. i.e. less than Number.MAX_SAFE_INTEGER
See the above in action in the screenshot below:
Can anybody shed some light on it, please?
MAX_SAFE_INTEGER only guarantees precision for integers below it, not decimals.
In fact there are no decimals in Javascript only floating point numbers. When you try to represent a number with too much precision it'll just be rounded to the nearest 64 bit floating point number.
If you actually want decimals you will need to use a decimal library like decimal.js or you could write your own.

Solution to the infamous 64-bit floating point math error in JavaScript [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
Is there a solution to the infamous floating point math error in JavaScript due to the 64-bit floating point representation?
Is floating point math broken?
Im trying to make a math intensive application based on JavaScript, however, due to this error in JavaScript, it always results in inaccurate output.
For ex.:
0.1 + 0.2 = 0.30000000000000004
whereas the expected is
0.1 + 0.2 = 0.3
I wonder how the financial organizations like (PayPal) are still up with there javascript applications.
The "solution" to the (non-)error is to ignore it, and ensure that you use an appropriate function, i.e. toFixed(n), to present the number with the desired number of decimal places.
It's important to know the difference between the internal representation of a value, and how to present that value to the end user.
You can use https://github.com/ekg/fraction.js library for the intermediate values and convert the final result back to decimal value to minimize error.

why does JavaScript mess up 0.1 + 0.2 when C++ doesn't? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
I understand that with the IEEE representation (or any binary representation) for double one can't represent 0.1 with a finite amount of bits.
I have two questions with this in mind:
When C++ also uses the same standard for double why doesn't it mess up 0.1 + 0.2 like JavaScript does?
Why does JavaScript print console.log(0.1) correctly when it can't accurately hold it in memory?
There are at least three reasonable choices for conversion of floating point numbers to strings:
Print the exact value. This seems like an obvious choice, but has downsides. The exact decimal value of a finite floating point number always exists, but may have hundreds of significant digits, most of which are of no practical use. java.util.BigDecimal's toString does this.
Print just enough digits to uniquely identify the floating point number. This is the choice made, for example, in Java for default conversion of double or float.
Print few enough digits to ensure that most output digits will be unaffected by rounding error on most simple calculations. That was the choice made in C.
Each of these has advantages and disadvantages. A choice 3 conversion will get "0.3", the "right" answer, for the result of adding 0.1 and 0.2. On the other hand, reading in a value printed this way cannot be depended on to recover the original float, because multiple floating point values map to the same string.
I don't think any of these options is "right" or "wrong". Languages typically have ways of forcing one of the non-default options, and that should be done if the default is not the best choice for a particular output.
Because it prints x to n decimal places. That happens to be correct

Why this calculation doesn't return a whole number? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 9 years ago.
console.log((57 / 100) * 100);
When I did this math in Javascript it always return 56.99999999. Why its not 57.00000?
That has to do with the way JavaScript handles floating point numbers. Check out this topic: How to deal with floating point number precision in JavaScript?
In order to get an exact answer, you will need to work in integers, or use a decimal library. There are some decimal libraries for Javascript which compute the values using integers, then convert the values back to decimal. For example, there is the BigNumber library for Javascript.
The reason for this is that the section (57 / 100) returns a Float, or floating point number in Javascript. JavaScript uses 64-bit floating point numbers, which on most systems, gives 53 bits of precision to represent the mantissa, or numeric part of the float (the 3.2 in 3.2e4). Most systems use the IEEE-754 floating point standard, which allows for any error of the floating point operations as long as it's less than one unit in the last place (which in the case of IEEE-754 64-bit is the 54th bit). Therefore, all floats are cut off, rounded and/or truncated, which is why the result returns a fractional number.
For more information on why this happens, you can also see: Is floating point math broken?

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).

Categories

Resources