Why Every Computer Fails Basic Math
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
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.
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.
😱🤯
👌.. yay I won ! See I commented and reported other user, proves I am the programmer of youtube...
@@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..
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.
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 👍.
Excellent explanation of a complex topic !!
I've read about this once, but seeing that visual representation on the calculator was really good. Thanks for sharing.
This was awesome! Would love more like this! 😊✨
I love and love your free courses for Next js and prisma
Great video and perfect length
Thanks, please do more like this!
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
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😂
To be honest, I didn't understand much, but it was exciting. Yes, keep recording such video on deep dive topics. Thanks man!
Very helpful. Interesting to see how these problems were solved, to enable encoding into a binary format.
Would love to see more videos like this where you explain how computer works.
2 the power of 0, or generally n to the power of 0 always gives 1.
Yes, what a mistake!
Yes, what a mistake!
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.
Super interesting ! I want more videos like this :D
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.
Great video! More like this for sure!
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.
2 pow 0 is 1, isn't it?
Mistakes happen lol, but yeah it is 1
In the second example he says it right tho lol
literally my brain right now
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.
@@WebDevSimplified You got it right in the second example tho
I love this so much honestly...
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.
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 :-)
Here's a better question: Why even have equality comparisons for floating point in an interpreted language that don't auto-correct for epsilon?
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
More like this!
awesome !
@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.
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
The first bit is either 0 or 1 - not 2^0 which would always be 1
2^0 = 1
and then 0*(2^0)=0
did you calculate 2 to the power of 0, first to be 0, then to be 1?
Thanks to computer science, I got to know such nitty-gritty ^_^
Amazing 🎉
amazing ❤
What keyboard you use?
You could mention `Number.EPSILON`.
i would like more low level concepts
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?
Legendary meme, I guess
iEEE754 - we meet again
I wondered why this video had very less likes and realised I'm early.
2^0 = 0?
👍
I ask for some more 👍
"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.
Fun fact:
This is also why calculator lie to you and tell you the digits of pi are 3.141592654
(the ninth digit is wrong)
1x1 = 2 there are no straight lines
first!!!
🙏🙏🙏🙏👏👏👏👏👏🌹🌹🌹🌹🌹🖖🖖🖖🖖🖖🖖
1x1=2 lol
How to become web developer and get high pay?
Start by playing Candy Crush for 8 hrs a day.
Computers can add 0.25+0.5 better lol
AI has significantly made coding ridiculously easy
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.
thanks, nice explanation but czcams.com/video/qTXwRSksJPg/video.html would be 7 since 2^0 equals 1.
Javascript is just like a women you cant trust those