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
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
2 жыл бұрын
that's like first year Computer Sci I highly recommend reading some books that uni students go thru
Floats='confusing'
Just discovered your channel, liked and subbed :)
Thank you ! Does this mean libraries such as scipy, numpy, pandas already convert numbers into Decimal instances to prevent mathematical errors ?
@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
These videos are awesome, really awesome neovim theme:)
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
@NeuralNine is that 'IEE' Used in The Programming language c
Thank u for your class~
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
2 жыл бұрын
It's 1023 not 123. So, you'd need a 1kbit number to make a difference.
Thank You !
Thank you ;)!
Just tried this out using the console. The same result in Javascript as well.
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
2 жыл бұрын
How would you represent 1.000000000008231 then?
@logothetis4771
2 жыл бұрын
ok you got me lol
@somerandomuserfromootooob
2 жыл бұрын
@@NeuralNine i think we could do by dividing it!
@somerandomuserfromootooob
2 жыл бұрын
@@logothetis4771 but anyway it think it is not a good idea
@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
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
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?
Probably best thumbnail in the Universe😂🤣
you are one of the bests
What country are you from?
@NeuralNine
2 жыл бұрын
Austria
@jasimchouhan4784
2 жыл бұрын
@@NeuralNine I thought, Du bist Deutsch.
@NeuralNine
2 жыл бұрын
@@jasimchouhan4784 ich spreche Deutsch, bin aber Österreicher
Quite interesting. This is a source of various bugs if you're a beginner at programming and has no idea about the problem heh
The motivation behind this video is one of the 64 bit floating point Kata from Code Wars.
@NeuralNine
2 жыл бұрын
Not really
@jhawar-ji
2 жыл бұрын
@@NeuralNine ohh okay, while watching this video I thought of that Kata only😅
@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
2 жыл бұрын
@@ko-Daegu Yeah I realised that after I started diving in scientific computing.
Let's just pretend its an easter egg in python 😉.
floats == weird
Well it makes sense for the kids that bring 0 in math
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).
Download