#๐Ÿ”’ why the difference?

47 messages ยท Page 1 of 1 (latest)

wet elm
#

it doesnt make sense in the implementation of floating point

plain mantleBOT
#

@wet elm

Python help channel opened

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.

wet elm
#

ending of 2, 3, 2.0

#

it's like the +1 for the last one added 0 and not 1

dreamy cargo
#

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.

wet elm
dreamy cargo
#

Well, both of them are equally close to the true result (..993). I think float addition rounds towards zero in this case.

wet elm
dreamy cargo
#

That's true, yes.

#

It also means that (x + 1) + 1 isn't the same as x + (1 + 1), so addition stops being associative.

wet elm
dreamy cargo
#

Yes.

wet elm
#

this seems like something that has to be fixed

wet elm
analog ledge
#

I think this has to do with how computers fundamentally work and represent numbers

rustic panther
#

This is more of a flaw in how computers see numbers, not python

dreamy cargo
analog ledge
#

If you want to compare this number to another one, you can use math.isclose()

dreamy cargo
rustic panther
#

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

wet elm
dreamy cargo
dreamy cargo
wet elm
#

i mean the base10 doesnt really make sense to me

dreamy cargo
# wet elm isnt that a flaw in the system?

(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...

wet elm
#

since it still is a base2 in reality

dreamy cargo
#

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.

wet elm
#

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)

dreamy cargo
# wet elm could you explain this to me?

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.

dreamy cargo
wet elm
dreamy cargo
#

sure, that's done sometimes

wet elm
#

do you know if Kahan's algorithim is the best way to solve this issue?

wet elm
#

do you know if it has a name?

#

well thats alright

#

ty anyway mate!

#

!close

plain mantleBOT
#
Python help channel closed

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.