this post was submitted on 25 Sep 2024
-53 points (9.2% liked)
Programming
17087 readers
187 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities !webdev@programming.dev
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
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.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.
Perhaps the following rewording of your last sentence would be easier for readers to follow along:
Now try opening js interpreter and doing the same.
Also, try
0.1+0.2
in python interpreter.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
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. https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node18.html#SECTION00612000000000000000