#๐ why the difference?
47 messages ยท Page 1 of 1 (latest)
@wet elm
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
this integer is too big to be exactly representable as a float, so it gets rounded to the nearest float.
the next float after 900...992 is 900...994, so adding 1 to the former doesn't change it.
but it isnt ...994 though? why didnt it go to the next floating point then like you said? it didnt at all change after the +1 operation
Well, both of them are equally close to the true result (..993). I think float addition rounds towards zero in this case.
but that means if i do +1 again itll again round down to 992
That's true, yes.
It also means that (x + 1) + 1 isn't the same as x + (1 + 1), so addition stops being associative.
so i could do a 10^3 operations of adding +1 is what you're saying, and the variable wouldnt change ever?
Yes.
yes exactly
isnt that a flaw in the system?
this seems like something that has to be fixed
because this could ruin actual real world solutions to problems
I think this has to do with how computers fundamentally work and represent numbers
This is more of a flaw in how computers see numbers, not python
It's one of the problems of IEEE floats, yes.
If you want to compare this number to another one, you can use math.isclose()
There isn't really a fix, though - floats are good for most of their usecases, and fixing their problems requires giving up on some of the nice properties (and also on performance).
Afaik there is a more accurate standard to work with floats, but it'll probably be much slower and won't be supported by many things
there's other fraction implementations in python? i think python is famous for one them i cant remember which, one that could fix it with practically infinite number space allocation
For instance, decimals (fixed-point numbers) don't have most of these problems, but they have the problem of having a limited range of values rather than having a constant relative error like floats do.
You likely mean decimal. It's not really unique or anything - there's multiprecision math libraries for basically any language*, though Python is pretty cool for having an implementation in the standard library.
*E.g. the famous GMP comes to mind.
by decimal you mean real numbers? or just whole numbers? or rational ones too?
i mean the base10 doesnt really make sense to me
(Also, in practice it's possible to compensate for many of the problems, it just takes some specialized knowledge. E.g. in the scenario you mentioned, adding up tons of numbers of very different scales, you can avoid the loss of accuracy by using the https://en.wikipedia.org/wiki/Kahan_summation_algorithm )
In numerical analysis, the Kahan summation algorithm, also known as compensated summation, significantly reduces the numerical error in the total obtained by adding a sequence of finite-precision floating-point numbers, compared to the obvious approach. This is done by keeping a separate running compensation (a variable to accumulate small error...
since it still is a base2 in reality
decimal is the name of a builtin python library. It provides a fixed-point, base-10, variable-size representation for real numbers.
or rational ones
there's a different builtin for that,fractions. That one is pretty simple - python already has infinite-sized ints, so it's simple to represent rationals as a pair of those, and that's what that module does.
could you explain this to me?
i mean i thought too to make a seperate variable to add the 10^3 +1s everytime to instead of the big number above
but it could also eventually reach the SAME problem the original big number had (+1 to itself is identical to +0)
not sure I can. check the algorithm itself, basically. It's possible to predict the error that'll occur when adding a+b, and add the error only to a separate variable.
Yup. there's no free lunch here - no matter what you do it'll be possible to produce an example which has numerical errors. There's just no way to accurately model real numbers with a finite-size representation.
yeah thats true, but there could be an-ever-expanding represenation maybe? maybe when needing extreme precisions for exact calculations?
sure, that's done sometimes
do you know if Kahan's algorithim is the best way to solve this issue?
thats the implementation im looking for
do you know if it has a name?
well thats alright
ty anyway mate!
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.