Why is 0.1 + 0.2 returns 0.30000000000000004 in JavaScript? (medium.com)
from gyanendraknojiya@programming.dev to programming@programming.dev on 25 Sep 2024 14:04
https://programming.dev/post/19843161

Have you performed simple arithmetic operations like 0.1 + 0.2? You might have gotten something strange: 0.1 + 0.2 = 0.30000000000000004.

#programming

threaded - newest

RagingHungryPanda@lemm.ee on 25 Sep 2024 14:22 next collapse

It’s how CPUs do floating point calculations. It’s not just javascript. Long story short, a float is stored in the format of one bit for the +/-, some bits for a base value (mantissa), and some bits for the exponent. As a result, some numbers aren’t quite representable exactly.

luciole@beehaw.org on 25 Sep 2024 14:26 next collapse

Floating-point arithmetic is important to understand at least vaguely since it’s a pretty leaky abstraction. Fortunately, we don’t need a “✨Member-only story” on Medium to get acquainted with the underlying concepts.

gyanendraknojiya@programming.dev on 25 Sep 2024 14:39 collapse

It also includes a non member link

ericjmorey@programming.dev on 25 Sep 2024 17:27 collapse

That link didn’t work for me.

aubeynarf@lemmynsfw.com on 25 Sep 2024 14:34 next collapse

JavaScript is truly a bizarre language - we don’t need to go as far as arbitrary-precision decimal, it does not even feature integers.

I have to wonder why it ever makes the cut as a backend language.

thingsiplay@beehaw.org on 25 Sep 2024 15:00 next collapse

Popularity and ease of use I guess.

luciole@beehaw.org on 25 Sep 2024 17:16 collapse

The JavaScript Number type is implemented as an IEEE 754 double and as such any integer between -2^53^ and 2^53^ are represented without loss of precision. I can’t say I’ve ever missed explicitly declaring a value as an integer in JS. It’s dynamically typed anyways. There’s the languages people complain about and the ones nobody uses.

aubeynarf@lemmynsfw.com on 25 Sep 2024 18:23 collapse

And then JSON doesn’t restrict numbers to any range or precision; and at least when I deal with JSON values, I feel the need to represent them as a BigDecimal or similar arbitrary precision type to ensure I am not losing information.

luciole@beehaw.org on 25 Sep 2024 18:46 collapse

I hope you work in a field where worrying about your integers hitting larger values than 9 quadrillion is justified.

aubeynarf@lemmynsfw.com on 25 Sep 2024 18:57 collapse

Could be a crypto key, or a randomly distributed 64-bit database row ID, or a memory offset in a stack dump of a 64 bit program

Zagorath@aussie.zone on 25 Sep 2024 14:36 next collapse

A good way to think of it is to compare something similar in decimal. .1 and .2 are precise values in decimal, but can’t be represented as perfectly in binary. 1/3 might be a pretty good similar-enough example. With a lack of precision, that might become 0.33333333, which when added in the expression 1/3 + 1/3 + 1/3 will give you 0.99999999, instead of the correct answer of 1.

toasteecup@lemmy.world on 25 Sep 2024 14:50 collapse

Python has no issues representing

1/3 + 1/3 + 1/3

as 1. I just opened a python interpreter, imported absolutely no libraries and typed

1/3 + 1/3 + 1/3 enter and got 1 as the result. Seems like if python could do that, JavaScript should be able to as well.

andnekon@programming.dev on 25 Sep 2024 14:59 next collapse

Now try opening js interpreter and doing the same.

Also, try 0.1+0.2 in python interpreter.

toasteecup@lemmy.world on 25 Sep 2024 16:36 collapse

I’ll pass on the js interpreter. I don’t feel like learning the arcane runes.

To your point, Python handles those by giving you 0.300000004 might have missed a zero but valid point nonetheless

Zagorath@aussie.zone on 25 Sep 2024 15:20 next collapse

I thought it was a rather simple analogue, but I guess it was too complicated for some?

I said nothing about JavaScript or Python or any other language with my 1/3 example. I wasn’t even talking about binary. It was an example of something that might be problematic if you added numbers in an imprecise way in decimal, the same way binary floating point fails to accurately represent 1/10 + 1/5 from the OP.

ericjmorey@programming.dev on 25 Sep 2024 17:34 collapse

Perhaps the following rewording of your last sentence would be easier for readers to follow along:

With a lack of precision, 1/3 might become 0.33333333. When evaluating the expression 1/3 + 1/3 + 1/3, using 0.33333333 as an approximate representation of 1/3 will return a result of 0.99999999, instead of the correct answer of 1.

aubeynarf@lemmynsfw.com on 25 Sep 2024 15:23 next collapse

That’s because the nearest representable float to 0.99999999999999 is 1.0 - not because Python is handling rationals correctly.

This is a float imprecision issue that just happens to work out in this case.

It’s worth wondering why, if Python is OK with “/“ producing a result of a different type than its arguments, don’t they implement a ratio type. e.g. www.cs.cmu.edu/Groups/AI/html/cltl/…/node18.html#…

[deleted] on 25 Sep 2024 18:14 collapse

.

karlhungus@lemmy.ca on 25 Sep 2024 14:39 next collapse

Ugh, i thought this was a question, not a link. So i spent time googling for a good tutorial on floats (because I didn’t click the link)…

Now i hate myself, and this post.

thingsiplay@beehaw.org on 25 Sep 2024 15:02 collapse

Don’t hate yourself. At least you searched it properly. See it this way, you learned from a failure more than anyone who did not fail. You are now stronger!

karlhungus@lemmy.ca on 25 Sep 2024 19:17 collapse

assuming i learned, lol :D

thingsiplay@beehaw.org on 25 Sep 2024 15:00 next collapse

If you are adding 0.1 + 0.2, then it means you can cut off anything after the first digit (after the dot off course). Because the rest of the 0.1 is only 0 and the rest of 0.2 is 0. That can help with rounding errors on floating point calculations. I don’t program JavaScript, so no idea what the best way to go about it would be.

aubeynarf@lemmynsfw.com on 25 Sep 2024 15:20 collapse

How would you implement this in code?

thingsiplay@beehaw.org on 25 Sep 2024 15:43 collapse

I don’t have much JavaScript experience, but maybe .toFixed() will help here. Playground (copy the below code to the playground to test): playcode.io/javascript

const number = 0.1 + 0.2
const fixed = number.toFixed(3)

// Update header text
document.querySelector('#header').innerHTML = message

// Log to console
console.log(number)
console.log(fixed)

outputs:

0.30000000000000004
0.300
mox@lemmy.sdf.org on 25 Sep 2024 17:01 next collapse

en.wikipedia.org/wiki/IEEE_754

eric@lemmy.ca on 25 Sep 2024 20:07 next collapse

0.30000000000000004.com

[deleted] on 25 Sep 2024 22:01 collapse

.