social.tchncs.de is one of the many independent Mastodon servers you can use to participate in the fediverse.
A friendly server from Germany – which tends to attract techy people, but welcomes everybody. This is one of the oldest Mastodon instances.

Administered by:

Server stats:

3.8K
active users

Maltimore

This morning, out of nowhere, I decided enough is enough. Python existed for over 30 years, so the following issue just shouldn't exist anymore:

>>> 0.1 + 0.2 == 0.3
False

So I after people tried to convince me of julia for many years because it supposedly handles numbers much better, I finally tried it.

julia> 0.1 + 0.2 == 0.3
false

Okay then.

@maltimore So switched Language? What you use then?

@maltimore

"just shouldn't exist anymore"?!

But you can easily prove there is no way to prevent this problem in general, which is why all programming languages have this!

I strongly recommend reading some books on numerical analysis. The second volume of Knuth's The Art of Computer Programming has a chapter called "Arithmetic" that is still one of the best sources.

@TomSwirly
read numerical analysis, they said. No way to prevent this, they said.

@maltimore

I should have been more specific.

Yes, I know exact arithmetic is possible in certain limited cases, but it is highly undesirable as the main numeric type in a general purpose programming language, because there are no bounds on how long basic computations like addition or multiplication might take - and it also prevents doing basic things like logarithms or trig functions.

Even NASA uses floating point numbers to do their calculations.

1/

@maltimore The fact that Python uses floating point numbers to represent non-integral values is not a bug, so it won't ever get fixed!

There are certainly use-cases for limited and very expensive "exact" arithmetic, but the vast majority of programmers won't ever encounter them.

@maltimore (Do note that I said that there is no way to prevent this problem "in general" and that is exactly the case.)

@maltimore the stupidity of modern languages of continuing to insist in making numbers be weird instead of actually working as numbers is bonkers, ridiculous and annoys me to no end. Computers and languages should help people do stuff and numbers working properly should be a basic thing that should work. The day I create my language numbers will be numbers and not this.

@vascorsd
learn julia, they said. it handles numbers better, they said.

@vascorsd @maltimore
The problem (particularly in dynamically typed languages like Python) is that any change to type inference would be a major breaking change.
You could create YAL (Yet Another Language) that treats non-integer number literals as decimals instead of floating point, but then you would have other problems to contend with. The ability of a compiler/interpreter to mind-read is limited.

@aserraric @vascorsd
I mean there are programs that handle it just fine, like Mathematica. I assume SageMath too.
I assume handling it properly would be terrible for performance, but I'd rather have a slow, correct language than a fast incorrect one.

@maltimore @vascorsd @aserraric In Python the 'decimal' module can handle this, too:

import decimal
decimal.Decimal('0.1')+decimal.Decimal('0.2') == decimal.Decimal('0.3')
True


@diesch @vascorsd @maltimore

Interestingly (or maybe stupidly), decimal.Decimal(0.1)+decimal.Decimal(0.2) == decimal.Decimal(0.3) is False. Apparently, Python loves its floats. I could not find a way to declare a decimal literal, you have to parse it from a string...

@aserraric @diesch @vascorsd
this is a pretty cool resource about how languages handle it, though I wish they'd show the 0.1+0.2==0.3 output for each one too.
0.30000000000000004.com/

0.30000000000000004.comFloating Point Math