r/CodingHelp • u/LazaMand • Mar 10 '26
Dart Erm, what is happening here in this code?
Hey I was just coding something since I wanted to learn dart. But while I was just messing around, I found something a bit weird. I am pretty sure that 3*2.2 isn't 6.600000000005. Can someone tell me why this is happening?
31
Upvotes
•
u/AutoModerator Mar 10 '26
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
29
u/mrtnrn Mar 10 '26
https://0.30000000000000004.com/
12
u/MysticClimber1496 Professional Coder Mar 10 '26
Look up how floating point arithmetic works, it happens due to rounding and https://en.wikipedia.org/wiki/IEEE_754
6
u/ShortSynapse Professional Coder Mar 10 '26
Ah, I still remember my first encounter with floating point math... not great that it was for tax software...
Anyway, what you are running into are limitations of the way floating point numbers are represented and operated on in the computer. Specifically IEEE 754:
https://en.wikipedia.org/wiki/IEEE_754
4
u/lollysticky Mar 10 '26
floats are not accurate. Use the decimal package to mitigate :)
3
u/phylter99 Mar 10 '26
Why floating point is sometimes not accurate...
https://www.youtube.com/watch?v=PZRI1IfStY0
3
u/AlexMTBDude Mar 10 '26
Google "How do floating point values work in programming?"
3
u/birdspider Mar 10 '26 edited Mar 10 '26
but
3 *2.20000000000000017764 is6.6000000000000005:)(you're missing a few
0s, it's6.6000000000000005)EDIT: you can ask dart to create a string representing the "exact" float:
2
u/ExtraTNT Mar 10 '26
IEEE754
2
u/CranberryDistinct941 Mar 11 '26
Today is the day that yet another poor soul learns about floating point arithmetic.
1
u/ummaycoc Mar 11 '26
So you can Google things but a quick comment explaining it: 1/3 can’t be accurately represented with a finite number of digits in decimal notation. 0.33333…3333 however many you want to put you always leave something off the true value.
Modern computers use binary to represent floating point numbers and so just like decimal can represent 1/5 easily as 0.2 but 1/3 causes some problems, likewise with some values for binary.
1
u/i_design_computers Mar 11 '26
To give a simple answer, 2.2 is a repeating decimal number in binary (10.0011... where the 0011 repeats) but the computer can only store a finite number of decimals so it rounds. This is just like 1/3 in decimal which is 0.333...
When you multiply by 3, the rounding error compounds, and then when you convert back to decimal, you end up off by a small amount.
1
u/Material-Aioli-8539 Mar 11 '26
Floating-point errors.
1
u/Former-Director5820 Mar 11 '26
What’s that one Mr.Incredible meme where he’s at the super computer looking shocked??
1
u/Personal-Search-2314 Mar 11 '26
Is that the dartpad? Im surprised that the solution is so off by nearly .067. That’s a lot.
1
u/AccomplishedSugar490 Mar 11 '26
No language’s default floating point (single or double precision) should be so wide as to exceed the precision limits of that it is printing. That 5 at the end is actually undefined behaviour, and not really present in the number.
1
u/BuyHistorical5789 Mar 11 '26
lol this is valid java code nowadays
1
u/Square-Fix3700 Mar 15 '26
I’ll give a different answer here in an effort to share some knowledge. Floating point numbers are accurate, but need to be rounded to the number of expected digits before evaluating.
Usually, we short cut this and round to FLT_EPSILON which is the smallest number we can add to 1 to make a number bigger than 1.
1
u/LetterheadFit6213 Mar 22 '26
Djske