Transcript for:
Understanding Real Numbers in Binary

Alright, hello class. In this one we're going to talk about how real numbers can be represented in binary. That is, numbers that have fractional parts, right?

Or not even necessarily fractional, because they could be irrational, but right? Decimal parts, non-whole parts, right? How do you represent numbers like that? So, we're not going to go into super much detail here.

I just want to give you the general idea. First things first, is it even possible to represent partial numbers with binary? Well, obviously it must be, because I'm making this video, right? But let's just think about this with decimal for a second again.

We talked about the place value, kilometer place value system, right? Where we said, okay, you have some base, and then your columns represent that base to some power. So, like, let's just do, you know, 10 to the 3, 10 to the 2, 10 to the 1, and 10 to the 0, right?

And so we can have some number here like this. We'll put, like, I don't know, 3, 7. to five. So writing that in a little bit more condensed way, three, seven, two, five, right? That is 3,725, right? 3,700, two tens and five ones.

No problem. We saw that the same principle works for binary, right? It's two, excuse me, two to the three, two to the two, two. to the one and two to the zero. And we put a number in here like this zero one zero.

We've got well, two to the three is eight, right? So we've got one eight zero fours, one, two and zero ones, which in decimal makes ten, right? Alright, no problem.

Now, let's think about what happens when we just keep going, right? We throw down a decimal point and then we put like a 2 here, or a 3 here. What is this kilometer place? Well, we know it's tenths in English, right? That's 3725 and 3 tenths, or.3.

Well, what is that in our power scheme? Well, it turns out that, yeah, it is just this, right? It's what you'd expect.

Remember how negative exponents work. 10 to the negative 1, excuse me, to the negative 1 is equivalent to 1 divided by 10 to the 1. Gracious, if I could ever find that caret key. 1 divided by 10 to the 1. 10 to the 1 is 10, so that's 1 over 10, so that's tenths, right? Likewise, the next place is hundredths, right? So we could have, you know, 3. hundredths as well.

And that's going to be 10 to the negative two, right? Again, one over 10 to the two hundredths. So can we just continue this idea with binary?

You bet we can, right? So this is going to be two, two. the negative one.

So one over two to the one, so one over two. So that means this is the halves slot, right? So if we wanted to have 10 and a half, that's how we would do it, right? 10 and a half, just like that.

And then the next one is going to be two to the negative two, one over two to the two, which is one over four. So that means this is the quarters, right? So 10.75 would be like this, right? Because that's a half and a quarter added together.

together. So, and then it would look like this, 10.11 and 10.75, right, in decimal. Pretty nifty, not too hard to understand, right? It's just a continuation of that idea we were already at.

A little funky, I will admit, right, to think about not tenths and hundredths, but halves and quarters, and then eighths, and then sixteenths, and then thirty-seconds, you know? But hey, if you ever used, like, a ruler to do carpentry, that's exactly how the imperial measurement system works. And there's there's some wisdom behind that, right?

Because visually for a human to visually cut a space in half and say, okay, well there's the halfway point. And then to say, there's the halfway point. Like we're pretty good at doing that. We're not so great at eyeballing tents, right? So don't get me wrong.

I'm definitely pro metric, but I do think there is some wisdom that's not often highlighted in the Imperial system, at least as far as length goes, pints and ounces and, you know, bushels and all of that stuff and, you know, whatever, but you know, inches, half inches, quarter inches, eighth inches, 16th inches, 32nd, 13th inches. of an inch, 64th of an inch, those make sense. Okay, there you go, soapbox over.

So this is the idea, at least in some. way. However, this is not really how they're represented. So what you'll hear talked about is not this, which is what you might call fixed point representation, right? You just decide, okay, hey, we've got 8 bits, we're going to use the first 4 bits for the whole part, and the back 4 bits for the fractional part.

That would only give you a maximum resolution of halves, quarters, eighths, sixteenths, right? Like, if you wanted to represent a finer fraction than that, you simply couldn't, right? You simply couldn't. So, how would you represent, you know, like...

one and a 20th, well, you couldn't. The best you could do would be one and a 16th, right? So the level of granularity, whenever you've just got like a fixed number of digits on each side is completely fixed. And so that's an important thing to notice, right?

Is like when you... you have a fixed number of digits, you cannot represent every possible value in between. We do this on paper by cheating and using human conventions, right? Like how do you write a third, um, you know, as a decimal, you do 0.33 little hat, right? And you put a little hat on it.

And that means, Hey, that keeps going forever. Right? So just pretend there's infinite threes stretching off into space and we know that's a third, right?

Okay. Well, computers can't do that, right? Computers can't do that. So what do they do instead?

Uh, instead computers actually use basically a scientific. notation so they have you know some some whole part whatever that is uh well you know 37 and then they also record a uh an exponent, right? I believe this is called the mantissa, and this is just called the exponent.

So three times, you know, and usually in scientific notation in decimal, we say 10 to the, you know, whatever. 7, something like that. And you know, if we wanted more precision, we could say 3.732 times 10 to the 7 or whatever.

It's the exact same thing in a computer, except that this is a binary number and... it's two to the power and the power is also a binary number so like you know that's 10 times two to the two right uh and this is called floating point Notation. And the reason it's called floating point notation is because we can kind of slide our decimal point around. Right.

We can put it here. We can put it here. and we can get different degrees of precision by doing that. Still suffers from some of the same things as fixed point, right? How many numbers are between 0 and 1?

Well, infinite, right? But how many can you represent with 32 bits? not infinite, right?

And, you know, that's the key answer there is maybe you can represent a lot, quite a few, and, you know, using this clever, like, scientific notation trick, maybe even more than we could have otherwise, but you still can't represent all of them, right? There are still numbers that are too infinitesimally small to represent accurately. So in the world, there are two standards for this. Both of them were created by IEEE, which is a standards organization. There is a single precision.

floating point. These are often just referred to as floats in computer programming speak, and there is a double precision floating point standard, and those are often just referred to as doubles. These are 32-bit. These are 64-bit. Those are the standard types of floating point numbers.

Now, if we're representing it this way, where you have, you know, you have one chunk of 32-bits, and we just say, okay, this part is the mantissa, and this part is the exponent. But can you just run those through like the addition circuit that we saw? Can you just run those through our ripple carry adder to add them?

No. You need completely different circuitry and completely different machinery to handle real numbers, fractional numbers. And that's just basically unavoidable. Right. You could say to yourself, well, what if we just got rid of the integer circuitry and only had floating point circuitry?

Because then we could save on that. But the truth is, is we need integer circuitry for other stuff that isn't just doing math. That has to do with more about how computers work, namely things to do with memory, which we're going to talk about, like in the next section of this module. OK, yeah, that's it for this one.

The walkaway messages here, single precision, double precision, 32 bit, 64 bit. The way that it's stored is in basically like a type of. scientific notation.

And we won't really talk about it much more in detail than that. See you in the next one.