Why is 0.1 + 0.2 Not 0.3 in Python?

Ғылым және технология

In this video we talk about why 0.1 plus 0.2 is not equal to 0.3 in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: www.neuralnine.com/books/
💻 The Algorithm Bible Book: www.neuralnine.com/books/
👕 Programming Merch: www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine.com/
📷 Instagram: / neuralnine
🐦 Twitter: / neuralnine
🤵 LinkedIn: / neuralnine
📁 GitHub: github.com/NeuralNine
🎙 Discord: / discord
🎵 Outro Music From: www.bensound.com/
Timestamps:
(0:00) Intro
(1:08) IEEE-754 Explained
(9:22) Binary Representation
(15:57) How To Solve This Issue
(19:30) Outro

Пікірлер: 42

  • @mathophile441
    @mathophile4412 жыл бұрын

    I never knew something like this existed. Thanks a lot for making great in-depth Python content like these and please keep making it in the future too. Thanks:D

  • @ko-Daegu

    @ko-Daegu

    2 жыл бұрын

    that's like first year Computer Sci I highly recommend reading some books that uni students go thru

  • @krishnanagpal807
    @krishnanagpal8072 жыл бұрын

    Floats='confusing'

  • @Rain99891h
    @Rain99891h2 жыл бұрын

    Just discovered your channel, liked and subbed :)

  • @ke30_
    @ke30_2 жыл бұрын

    Thank you ! Does this mean libraries such as scipy, numpy, pandas already convert numbers into Decimal instances to prevent mathematical errors ?

  • @complexnumbers64

    @complexnumbers64

    2 жыл бұрын

    Nope. Numpy & friends suffer from the same floating point shenanigans as in vanilla python. For example, when working with financial (dollars & cents) data with pandas, it's something you'll commonly run into when say summing a column of transactions. You'll get a result that you'll need to round to 2 places

  • @benjamindreyer9884
    @benjamindreyer98842 жыл бұрын

    These videos are awesome, really awesome neovim theme:)

  • @SomeCrazyVids
    @SomeCrazyVids Жыл бұрын

    How would it work if your exponent is much larger, lets say 101010001011 = 2699? Would you then still subtract 1023 from that? Because that leaves you with an Exponent of 2^1676? As there are only 52-bits in the Mantissa, 1676 far exceeds that, so how would you move the decimal place? Thanks

  • @somerandomuserfromootooob
    @somerandomuserfromootooob2 жыл бұрын

    @NeuralNine is that 'IEE' Used in The Programming language c

  • @bagadavid9477
    @bagadavid9477 Жыл бұрын

    Thank u for your class~

  • @mohsenhaddadi1117
    @mohsenhaddadi11172 жыл бұрын

    According to IEEE-754, is Zero equal to 2 to the power of -123 ? If so, for 64 bit system while we don't have 123 bits to shift mantissa to the left, Zero is equal to exactly Zero. but for 128 bit system Zero won't be equal to exact Zero

  • @ghost-ud3bm

    @ghost-ud3bm

    2 жыл бұрын

    It's 1023 not 123. So, you'd need a 1kbit number to make a difference.

  • @ilgar.roshen
    @ilgar.roshen2 жыл бұрын

    Thank You !

  • @patrickslomian7423
    @patrickslomian7423 Жыл бұрын

    Thank you ;)!

  • @TheJimmar12
    @TheJimmar122 жыл бұрын

    Just tried this out using the console. The same result in Javascript as well.

  • @logothetis4771
    @logothetis47712 жыл бұрын

    It might sound stupid, but why are floats not just represented as 2 integers, one for the whole and another for the decimal part?

  • @NeuralNine

    @NeuralNine

    2 жыл бұрын

    How would you represent 1.000000000008231 then?

  • @logothetis4771

    @logothetis4771

    2 жыл бұрын

    ok you got me lol

  • @somerandomuserfromootooob

    @somerandomuserfromootooob

    2 жыл бұрын

    @@NeuralNine i think we could do by dividing it!

  • @somerandomuserfromootooob

    @somerandomuserfromootooob

    2 жыл бұрын

    @@logothetis4771 but anyway it think it is not a good idea

  • @somerandomuserfromootooob

    @somerandomuserfromootooob

    2 жыл бұрын

    @@NeuralNine i mean: Take 6 We make it two int by splitting it like this 3+3 into two integers and in that way satisfies

  • @not-alot-of-options
    @not-alot-of-options Жыл бұрын

    7:16 for anyone getting confused at this whole "shifting 1.1 to the right" business like I was, you are shifting the *_decimal point_* of 1.1 to the right. 1.1 11 move [point] right once 110 move [point] right twice 1100 move [point] right thrice 11000 "shift 1.1 to the right four times"

  • @HrupekHrupencjusz

    @HrupekHrupencjusz

    9 ай бұрын

    i am confused about it. We are shifting on binary, and then calculate binary to dig, so why you call it "decimal" shift? shift is shift. I think @neuralnine can be so nice and make good explanation for this situation?

  • @justacoder_
    @justacoder_ Жыл бұрын

    Probably best thumbnail in the Universe😂🤣

  • @mohsenhaddadi1117
    @mohsenhaddadi11172 жыл бұрын

    you are one of the bests

  • @aditya_asundi
    @aditya_asundi2 жыл бұрын

    What country are you from?

  • @NeuralNine

    @NeuralNine

    2 жыл бұрын

    Austria

  • @jasimchouhan4784

    @jasimchouhan4784

    2 жыл бұрын

    @@NeuralNine I thought, Du bist Deutsch.

  • @NeuralNine

    @NeuralNine

    2 жыл бұрын

    @@jasimchouhan4784 ich spreche Deutsch, bin aber Österreicher

  • @marco.nascimento
    @marco.nascimento2 жыл бұрын

    Quite interesting. This is a source of various bugs if you're a beginner at programming and has no idea about the problem heh

  • @jhawar-ji
    @jhawar-ji2 жыл бұрын

    The motivation behind this video is one of the 64 bit floating point Kata from Code Wars.

  • @NeuralNine

    @NeuralNine

    2 жыл бұрын

    Not really

  • @jhawar-ji

    @jhawar-ji

    2 жыл бұрын

    @@NeuralNine ohh okay, while watching this video I thought of that Kata only😅

  • @ko-Daegu

    @ko-Daegu

    2 жыл бұрын

    that's something with a lot of history (read about the f-22 Raptor or Patriot failure on desert war) floating point representation is a big topic in Computer Science

  • @jhawar-ji

    @jhawar-ji

    2 жыл бұрын

    @@ko-Daegu Yeah I realised that after I started diving in scientific computing.

  • @atomicsoham4864
    @atomicsoham48642 жыл бұрын

    Let's just pretend its an easter egg in python 😉.

  • @SparePlayss
    @SparePlayss2 жыл бұрын

    floats == weird

  • @emperorj4783
    @emperorj47832 жыл бұрын

    Well it makes sense for the kids that bring 0 in math

  • @thomasgoodwin2648
    @thomasgoodwin26482 жыл бұрын

    I still believe binary scales much better than decimal. Logs need to be brought in before any effective scaling can be achieved in decimal where binary comes with it built in. Add a bit and you have doubled the solution space. It grows geometrically rather than linearly (x10).

  • @kenichimori8533
    @kenichimori85332 жыл бұрын

    Download

Келесі