python-discord / meta

Issue tracker for suggestions and other questions relating to our community
https://pythondiscord.com
31 stars 5 forks source link

Resource and/or tag recommendation: guide to floating point numbers. #73

Closed lxnn closed 3 years ago

lxnn commented 3 years ago

I think this website would be a useful place to point people who are confused by the results they get when they compare floating point numbers in their code: https://floating-point-gui.de

I propose we introduce a tag for this. Something along the lines of:

Some fractional numbers cannot be represented exactly as binary floating point numbers. So when comparing floating point numbers it's usually best to include a margin of error: test that they are similar rather than exactly equal.

Instead of:

if x == y:
    ...

Try:

if abs(x - y) < epsilon:
    ...

Where epsilon is a small number.

See this website for more information: floating-point-guide.de

lxnn commented 3 years ago

Remembered that there's also math.isclose.

laundmo commented 3 years ago

in staff meeting the fractions and decimal stdlibs were also mentioned as possible things that should be mentioned in the tag

lxnn commented 3 years ago

Correct link is https://floating-point-gui.de

jb3 commented 3 years ago

This is added in python-discord/bot#1395