Why Every Computer Fails Basic Math

Sdílet
Vložit
  • čas přidán 7. 07. 2024
  • It is a meme at this point that JavaScript sucks since it cannot even add 0.1 and 0.2 correctly. This is true, but it is not a problem with JavaScript. This is a problem with every single programming language and every single computer ever developed since they all use binary for their calculations which is why computers fail to add this correctly. In this video I will show you why this problem exists, then show you why it is important to you, before finally wrapping up with a nerdy deep dive into how computers handle floating point numbers.
    📚 Materials/References:
    IEEE 754 Calculator: www.h-schmidt.net/FloatConver...
    🌎 Find Me Here:
    My Blog: blog.webdevsimplified.com
    My Courses: courses.webdevsimplified.com
    Patreon: / webdevsimplified
    Twitter: / devsimplified
    Discord: / discord
    GitHub: github.com/WebDevSimplified
    CodePen: codepen.io/WebDevSimplified
    ⏱️ Timestamps:
    00:00 - Introduction
    00:58 - Binary Integers
    04:04 - Binary Decimals
    05:17 - The Problem With 0.1 + 0.2
    07:56 - Why This Is Important
    10:26 - Nerdy Deep Dive
    #Binary #WDS #JavaScript

Komentáře • 74

  • @-taz-
    @-taz- Před měsícem +40

    When I was your age (old man here!), we didn't even have a floating point unit. We had to emulate it (super slow) or buy a math co-processor. But since customers never had those, we just did integer math and then did a binary shift right to divide by 64 before drawing on the screen buffer in pixels. Quake was the first major game or product that depended on having a math unit built-in.

    • @catchwest.combypuneetgrover
      @catchwest.combypuneetgrover Před měsícem

      As far my guess goes such emulations are still very useful in creating processors or robotics or even gaming consoles where programming languages don't work.. however now overall it's a diversified sector with very few people capable of understanding the concepts, and if they didn't get aware about this fact, they will be fooled and pressurised resulting in failure to earn huge money, so be super over confident if you had solved complex programming, only rarest of rare can achieve that, and all this rumours of there are lots of programmers in competition, ai will replace programmers, only non programmers can believe these lies.

    • @abhishekpratap8784
      @abhishekpratap8784 Před měsícem

      😱🤯

    • @takethat6927
      @takethat6927 Před měsícem

      👌.. yay I won ! See I commented and reported other user, proves I am the programmer of youtube...

    • @funreviews1436
      @funreviews1436 Před měsícem

      ​@@takethat6927ha ha yeah India have programmers and nothing is outsourced from West, and reason there is rarely any website mentioning programmer details or link on their website.. haha children here can only play law sports or laziest cricket and want to claim education..

  • @BeCurieUs
    @BeCurieUs Před měsícem +8

    This is a solved problem for javascript programs...just use BigDecimal or a similar package. Never actually divide or multiply things if you can help it. We ran into this problem in production where the backend would store values as a decimal, but we wanted to display them to the user as a percentage. Even just the trivial * / 100 will introduce rounding errors. I would strenuously implore you to use a math package that handles this for you.

  • @devopsjuan
    @devopsjuan Před měsícem +4

    The actual calculation from binary (110) to decimal (6) representation is (2^2)*1 + (2^1)*1 + (2^0)*0 = 4 + 2 + 0 = 6. Otherwise, great video 👍.

  • @burtyful1
    @burtyful1 Před měsícem +6

    Excellent explanation of a complex topic !!

  • @metalbroga
    @metalbroga Před měsícem +1

    I've read about this once, but seeing that visual representation on the calculator was really good. Thanks for sharing.

  • @BooksWeCanRead
    @BooksWeCanRead Před měsícem +1

    This was awesome! Would love more like this! 😊✨

  • @husseinsalim2398
    @husseinsalim2398 Před měsícem +1

    I love and love your free courses for Next js and prisma

  • @Alcaatraz01
    @Alcaatraz01 Před měsícem +2

    Great video and perfect length

  • @ZacharyDuvallT
    @ZacharyDuvallT Před měsícem

    Thanks, please do more like this!

  • @user-vd3ph6zh8q
    @user-vd3ph6zh8q Před měsícem +1

    One trick I learned to count in binary, is you can use your fingers, think of each finger as a power of 2. So your first finger is 1, your second finger is 2, your first and second finger is 3, etc. Essentially you can count up to 31 on one hand and 1023 with two hands

  • @VincentYang024
    @VincentYang024 Před měsícem

    Good explanation. I was refreshing the same thing yesterday as a very old interview question came to my head suddenly “ why do you use decimal for money?” I could not give an in-depth answer, if I can tell the precision issue in binary floating-point to interviewer , I might get that job many years ago😂

  • @alexeyshaykov
    @alexeyshaykov Před 28 dny

    To be honest, I didn't understand much, but it was exciting. Yes, keep recording such video on deep dive topics. Thanks man!

  • @markmaples7215
    @markmaples7215 Před měsícem

    Very helpful. Interesting to see how these problems were solved, to enable encoding into a binary format.

  • @belkocik
    @belkocik Před měsícem

    Would love to see more videos like this where you explain how computer works.

  • @MFSln
    @MFSln Před měsícem +5

    2 the power of 0, or generally n to the power of 0 always gives 1.

  • @kennethmortensen6990
    @kennethmortensen6990 Před měsícem

    Good one, stuff like this is important.
    You could maybe add, that the larger the numbers you represent, the less precision you have. A good analogy is an elastic ruler: when you stretch it, the gaps between the marks get longer. So, for small numbers, it's more precise, but measuring a big number would create larger gaps between the marks, causing you to lose precision.

  • @okaniyoshiii9212
    @okaniyoshiii9212 Před měsícem

    Super interesting ! I want more videos like this :D

  • @medamine44100
    @medamine44100 Před měsícem +2

    Hi Kyle i'm gonna try out nerd you 😅,15:03 the largest number that can be represented in 32 bits is not 2 to the power of 32 ( because it needs 33 bits),. The largest number is 2 to the power of 32 minus 1.

  • @jamiesajdak7147
    @jamiesajdak7147 Před měsícem

    Great video! More like this for sure!

  • @SirMeowMeow
    @SirMeowMeow Před měsícem +1

    I'd argue that the base representation problem is not the right way to frame why software math is a problem. In languages like Racket adding 0.1 + 0.2 is fine. All number specs make a compromise between speed, accuracy, and space efficiency and floating point numbers will come with wonky behaviors like non-commutative addition. JS just happens to choose the IEEE 754 floating point as their default number type. As do many languages, but again, not all.
    There's no clean way to protect yourself from floating point wonkiness except to actually analyze the math you do in your software. Even OpenAI can become surprised by floating point behavior.

  • @stanislavdomanskyi3613
    @stanislavdomanskyi3613 Před měsícem +21

    2 pow 0 is 1, isn't it?

    • @oskarkottom8365
      @oskarkottom8365 Před měsícem

      Mistakes happen lol, but yeah it is 1

    • @oskarkottom8365
      @oskarkottom8365 Před měsícem

      In the second example he says it right tho lol

    • @cheens5349
      @cheens5349 Před měsícem +1

      literally my brain right now

    • @WebDevSimplified
      @WebDevSimplified  Před měsícem +5

      It is 1. I think in the first example I was trying to say that in our particular case we have a 0 in the 2^0 place which gives us 0 since 0 * 1 is 0.

    • @oskarkottom8365
      @oskarkottom8365 Před měsícem

      @@WebDevSimplified You got it right in the second example tho

  • @ahmadbenchakhtir5787
    @ahmadbenchakhtir5787 Před měsícem

    I love this so much honestly...

  • @cherubin7th
    @cherubin7th Před měsícem +1

    This is just the problem with floating point number. Other data structures like "Decimal" (in some languages) don't have this issue. The reason why floating point as the default is only because it is supported by the cpu directly and culture.

    • @fabriziocapolini4349
      @fabriziocapolini4349 Před měsícem

      Not exactly: repeating decimal numbers exist in decimal notation as well. The result of 1/3 cannot be fully represented in decimal just as 1/10 cannot be fully represented in binary. The cultural problem is actually that languages cater for our habit of using a base 10 notation instead of their native base 2 representation: if instead of adding tenths in that example we were adding sixteenth, we wouldn't have a rounding problem :-)

  • @williamdrum9899
    @williamdrum9899 Před 17 dny

    Here's a better question: Why even have equality comparisons for floating point in an interpreted language that don't auto-correct for epsilon?

  • @corscheid
    @corscheid Před měsícem

    I remember learning about this in college. Binary is interesting. It's also good for web devs to think about this low level stuff at least sometimes. It's not just for C++ devs only ;D

  • @jerryknows7765
    @jerryknows7765 Před měsícem

    More like this!

  • @gelzintvidaure
    @gelzintvidaure Před měsícem +1

    awesome !

  • @Srabon444
    @Srabon444 Před měsícem

    @WebDevSimplified Ah..., my university's second trimester kicked in. 🤭 Hey Kyle, make a video about React Compiler. I'm really looking forward to your in-depth video.

  • @anonym6132
    @anonym6132 Před měsícem

    well that's on point, just yesterday I added a line to my JavaScript to multiply sth with (140/173) .... mayyybe I should check if the calculation isn't too far off

  • @jarbyvid
    @jarbyvid Před měsícem +1

    The first bit is either 0 or 1 - not 2^0 which would always be 1

  • @AlexDanilin
    @AlexDanilin Před měsícem +6

    2^0 = 1

  • @jonathancarnos4816
    @jonathancarnos4816 Před 22 hodinami

    did you calculate 2 to the power of 0, first to be 0, then to be 1?

  • @Denis_Victorious
    @Denis_Victorious Před měsícem +2

    Thanks to computer science, I got to know such nitty-gritty ^_^

  • @singh.aadarsh
    @singh.aadarsh Před měsícem +1

    Amazing 🎉

  • @mohitmohlia8420
    @mohitmohlia8420 Před měsícem

    amazing ❤

  • @premsagar4438
    @premsagar4438 Před měsícem

    What keyboard you use?

  • @sy_filis
    @sy_filis Před měsícem

    You could mention `Number.EPSILON`.

  • @krateskim4169
    @krateskim4169 Před měsícem

    i would like more low level concepts

  • @nufSaid_
    @nufSaid_ Před měsícem

    Hi guys. Are you guys still getting tech roles? Been applying for jobs but nothing yet. Any idea on how or where to get jobs?

  • @aysommer
    @aysommer Před měsícem

    Legendary meme, I guess

  • @ChucklesMcGee780
    @ChucklesMcGee780 Před měsícem

    iEEE754 - we meet again

  • @sruthikrishnakumar7606
    @sruthikrishnakumar7606 Před měsícem +1

    I wondered why this video had very less likes and realised I'm early.

  • @cheens5349
    @cheens5349 Před měsícem

    2^0 = 0?

  • @rishiraj2548
    @rishiraj2548 Před měsícem

    👍

  • @johnnymnemonic1199
    @johnnymnemonic1199 Před 21 dnem

    I ask for some more 👍

  • @KnightYoshi
    @KnightYoshi Před měsícem

    "Computers get this wrong" - no the IEEE spec gets it wrong. Many languages that don't use the IEEE don't have this rounding error.

  • @RedblocktheII
    @RedblocktheII Před 21 dnem

    Fun fact:
    This is also why calculator lie to you and tell you the digits of pi are 3.141592654

  • @jasonjasonjasonjasonjason
    @jasonjasonjasonjasonjason Před měsícem

    1x1 = 2 there are no straight lines

  • @risitas5874
    @risitas5874 Před měsícem

    first!!!

  • @suelingsusu1339
    @suelingsusu1339 Před měsícem

    🙏🙏🙏🙏👏👏👏👏👏🌹🌹🌹🌹🌹🖖🖖🖖🖖🖖🖖

  • @Brettm501
    @Brettm501 Před měsícem +1

    1x1=2 lol

  • @kirandey95
    @kirandey95 Před měsícem +1

    How to become web developer and get high pay?

    • @Oncopoda
      @Oncopoda Před měsícem +1

      Start by playing Candy Crush for 8 hrs a day.

  • @ghotifish1838
    @ghotifish1838 Před měsícem

    Computers can add 0.25+0.5 better lol

  • @_OKEANOUS_
    @_OKEANOUS_ Před měsícem +3

    AI has significantly made coding ridiculously easy

    • @BDKing-hh7pk
      @BDKing-hh7pk Před měsícem

      Only those which solved by someone else already. If you give a unique problem like nextjs 14 or a super brand new tech it will struggle and sometime cant solve at all.

  • @amixengineer
    @amixengineer Před měsícem

    thanks, nice explanation but czcams.com/video/qTXwRSksJPg/video.html would be 7 since 2^0 equals 1.

  • @AdolfRizzler41
    @AdolfRizzler41 Před měsícem +4

    Javascript is just like a women you cant trust those