The RIGHT Way To Compare Floats in Python

preview_player
Показать описание
If you're comparing floating-point values for equality, then you're doing it all wrong! I'll show you the RIGHT way to compare floats in Python.

⏱️⏱️VIDEO CHAPTERS⏱️⏱️
0:00 - Float Comparison Failures
0:23 - What You'll Learn
0:43 - What Are Floating-Point Numbers?
3:18 - What Numbers Get Rounded When Stored as Floats?
5:42 - How To Compare Floats in Python

👨🏼‍🎓 WANT HELP LEARNING PYTHON?

ABOUT ME
Hi! I'm David Amos, a Python educator and author of Python Basics: A Practical Introduction to Python 3. I've worked as a professional programmer for over 8 years and have spent the last 3 years helping thousands of people learn Python and become better Python programmers. I'm also an avid Julia programmer and co-host of the @Talk Julia podcast.

#Python #PythonFloats #PythonMath #NumbersInPython
Рекомендации по теме
Комментарии
Автор

The more I watch your videos, the more it dawns on me that I don't know anything. Brilliant!

alexanderstohle
Автор

3:16 Which is a bit of an over-generalization. This is why IEEE754 defines the “inexact” condition; when you see this raised, that is when you know that exact equality comparisons aren’t going to work.

People assume that “inexact” is always going to be the case. But there are situations where exact floating-point computations can be done.

Does Python give you access to the “inexact” state? Not with built-in functionality, but I have a module (pyfenv) which does.

lawrencedoliveiro
Автор

really well explained and lots of new information for me, thanks a lot and keep up the good work! :)

ENMPM
Автор

You've really hit your stride David! I've been watching the videos on your channel over the past few months, and this one, I have to say is very well done, and you've managed to explain things in such a nice way.

Kudos!

NafiulIslam
Автор

Haven’t even watched the video but I like where this is going

Mark-wqwd
Автор

Just got it recommended, great content! Shame that you stopped making it

kac
Автор

The other common way to deal with this is to not use floats, and instead use a type that basically avoids the base translation (accepting that it uses a bit more memory).

Imagine using four bits per decimal digit (16 possible values, but only 10 used in practice). The actual implementation is no doubt more efficient, but this shows that it's at least possible.

emurphy
Автор

Nice video David, thanks for producing such a good content

gabriellovate
Автор

i find that i've actually never had to check equality of floats, except 0.f or 1.f when i know i just assigned it
i try to keep as few floats in my code as possible for consistency

Temulgeh
Автор

If I found this on my own I would have indeed smashed my keyboard into my computer
last time I compared something that I used .lower() on to something with a capital letter, took me an hour to find ='D

vinnieg
Автор

How are ints and floats stored differently?

ChitralPatil
Автор

I really can't believe that there's such a function and it is standard.

iharobalasimi
Автор

I understand all except example, not math.isclose(num1, num2) and num1> num2.
Please could you explain, please?

RuslanSkiraUkraine
Автор

Try comparing 0.1 + 0.2 == 0.3 in C -> it returns true. :O So why C, which uses the very same IEEE 754 standard for floating point numbers, returns the correct answer, where Python, Javascript and other interpreted programming languages gossip?

CZghost
Автор

Actually comparison of floating point defaults linguistically to exact comparisons without tolerance. Comparing those is also interesting as NaN and even +/-zero must be checked first. Not to mention inequalities. What you are showing is inexact comparison which practically useful, it's way too misleading to say this is how to compare floats. In some contexts this method would cause a system failure. NASA won't be hiring you if you disregard exact comparison, needless to say

gregorymorse
Автор

im literally too stupid to understand any of what you just said

imveryhungry
Автор

Hello. In case you're wondering, your face looks a lot like mine)

igorgrischenko
Автор

What about just using a real language ?

ChronicTHX