THIS Is How You SHOULD Be Comparing FLOATS (Accurately) In Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ธ.ค. 2024

ความคิดเห็น • 38

  • @wyndhamwynne2480
    @wyndhamwynne2480 2 ปีที่แล้ว +13

    One way I have dealt with this is with the decimal library. I reccomend giving it a go if you've not come across it before.

  • @56z41
    @56z41 2 ปีที่แล้ว +15

    What you implemented in the video was actually abs_tol and not rel_tol

  • @SomeStrangeMan
    @SomeStrangeMan 2 ปีที่แล้ว +5

    Floating piont numbers come in two typical flavours. 32 bit, which has a range of 1e-36 to 1e36-ish, and 7/8-ish significant figures, and 64 bit, which has a range of 1e-306 to 1e306-ish and 15/16 significant figures. This means most numbers don't have a completely precise floating point representation, and that errors down at the 15th significant figure are extremely common.
    The epsilon function is part of the solution to this. - It returns the smallest possible error on a value, eg. abs(a-b)/eps(a) = an expression of the error as a multiple of the minimum possible error. This can then be compared to a suitable number.

  • @erichstocker8358
    @erichstocker8358 5 หลายเดือนก่อน +1

    This is why business languages provided a BCD representation that stored real numbers in the way that "business" expected them. Accounts really didn't care for the fuziness of real number. By definition a real number is one where between any two real numbers there is an infinite number of real numbers. Computers have never been good at infinity. So, this actually less a computer issue than a math issue. Computers just developed a convention for dealing with it.

  • @coolcodingcat
    @coolcodingcat 2 ปีที่แล้ว +3

    Python has a Decimal class.
    Java has a BigDecimal class.

  • @axlrose010101
    @axlrose010101 2 ปีที่แล้ว +2

    Ran into the same issue few days ago, i was so upset because i couldnt find the bug in my code. After found the issue I used the round() function, that solved it for me. But this is also a very good idea. Thank you!

    • @bFix
      @bFix 2 หลายเดือนก่อน

      round probably doesn't solve this for floating point numbers. Only if integer precision is enough

  • @foxypiratecove37350
    @foxypiratecove37350 5 หลายเดือนก่อน

    If you really want accuracy, you can use the Decimal classe from the decimal built-in module, with a string parameter for more accuracy.

  • @bFix
    @bFix 2 หลายเดือนก่อน

    just a quick correction, please don't take this personal
    8:40 actually the tolerance you implemented earlier is abs_tol not rel_tol.
    Relative tolerance is a percentage.
    The larger value will be divided by the smaller value. Your implementation calculated the absolute tolerance by subtracting and then calculating abs.

  • @Andrumen01
    @Andrumen01 ปีที่แล้ว

    In science you don't need precision beyond 4 or 5 significant figures, so anything else is overkill....unless you are working in the ISO and you need an extremely precise value for updates on exact values...

  • @judithlee7989
    @judithlee7989 2 ปีที่แล้ว

    Thanks for posting this, I really need it for the project at hand to work

  • @S3424868
    @S3424868 2 ปีที่แล้ว +1

    I learned that at school. I watched a few of your videos, then signed up. :)

  • @BersekViking
    @BersekViking ปีที่แล้ว

    Thank you for a great explanation!

  • @MACAYCZ
    @MACAYCZ 2 ปีที่แล้ว +1

    You are awesome! I love your videos:)

    • @Indently
      @Indently  2 ปีที่แล้ว

      I appreciate it mate!

  • @G-33k
    @G-33k 2 ปีที่แล้ว +1

    Man, i'm addicted to Python so finding someone talks about it means i'm gonna subscribe right now

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 ปีที่แล้ว

    Excellent ❤

  • @cenkedits1777
    @cenkedits1777 2 ปีที่แล้ว

    in fact we should make extend float class
    Extend float class has numper and diver

  • @ironmonkey1990
    @ironmonkey1990 2 หลายเดือนก่อน

    thank you!

  • @Migoyan
    @Migoyan 2 ปีที่แล้ว +2

    Real numbers are infinite, floats aren't and are what we use to represent real numbers.
    This problem concerns all languages implementing floats.
    Designing algorithms that keep the error low is a big field of applied mathematics.
    Also you didn't implemented relative tolerence, you implemented absolute tolerence.

  • @gemilangrahmandhika7509
    @gemilangrahmandhika7509 2 หลายเดือนก่อน

    I don't even know this was an issue 😅

  • @random-user-s
    @random-user-s ปีที่แล้ว

    We need quantum computers...

  • @senex_0372
    @senex_0372 ปีที่แล้ว

    Madoi se ti ho sentito arrabbiato dopo aver detto che la somma ti ritornava false 😂😂

  • @howl2339
    @howl2339 2 ปีที่แล้ว +1

    Di la verità!
    Sei Toscano anche te?

    • @Indently
      @Indently  2 ปีที่แล้ว +1

      Ahahah, no ho solo vissuto a Milano per un po’, però sono cresciuto all’estero per la maggior parte de la mia vita :)

  • @GooogleGoglee
    @GooogleGoglee 2 ปีที่แล้ว +2

    Wow this is a big Python flaw I didn't expect or know about. Is it happening only with the float type? It is like using a faulty calculator.
    I bet C or Java do not have this flaw

    • @EpicNoobx
      @EpicNoobx 2 ปีที่แล้ว +1

      I tried it with java, and guess what; java has the same flaw:)
      output: 0.30000000000000004
      im not sure if c has it too, though.

    • @GooogleGoglee
      @GooogleGoglee 2 ปีที่แล้ว

      @@EpicNoobx must be a specific design for float and the "mantissa" due to precision left to the programmer

    • @i_forgot1477
      @i_forgot1477 2 ปีที่แล้ว +3

      All languages conforming to the IEEE 754 standard for floats, which is the vast majority of languages, will have this problem

    • @GooogleGoglee
      @GooogleGoglee 2 ปีที่แล้ว +1

      @@i_forgot1477 correct, I was also digging into it the other day, and it is a design constraint. It is left to the programmer to deal with this based on what he needs to achive.

    • @Gruuvin1
      @Gruuvin1 2 ปีที่แล้ว +3

      It's not a language flaw. This is a limitation for the storage of floats, which has been around far longer than Python, and is acceptable. The only flaw is not understanding these limits and writing code that doesn't work within those limits.

  • @glitchedpixelscriticaldamage
    @glitchedpixelscriticaldamage 2 ปีที่แล้ว +1

    .... overcomplicating stuff for the sake of it...

    • @coolcodingcat
      @coolcodingcat 2 ปีที่แล้ว +3

      No. Numbers are stored in binary. Imagine a number like 1/3 in decimal. That is 0.333333...... Well in binary, 0.1 has an infinite expansion. The cpu has a aritmetic logic unit (alu) to quickly add, subtact, multiply or divide any 2 numbers using a full adder logic gate, and full multiplier logic gate. (Those are compound logic gates).
      There are some classes such as Decimal in Python or BigDecimal that abstract this away for you and compute these more accurately, to take care of it, but the trade offs are that they take more memory, and are slower, as it have to compute 1 digit at a time, instead of just sending it all through the cpu's alu and calculating it all at once.

    • @coolcodingcat
      @coolcodingcat 2 ปีที่แล้ว +1

      Or in other words, it is actually a lot more complicated to get a computer to do this in the simpler more expected way and a lot simpler to make it work in this more complicated unexpected way.

    • @Gruuvin1
      @Gruuvin1 2 ปีที่แล้ว +2

      Commenting negatively on a video you don't even understand.

    • @coolcodingcat
      @coolcodingcat 2 ปีที่แล้ว

      @@Gruuvin1 Maybe 1 or both of my previous replies helped them to understand better.

  • @hunterx405
    @hunterx405 2 ปีที่แล้ว

    I Cee.....