Placeholder Image

Subtitles section Play video

  • ANNOUNCER: Open content is provided under a creative

  • commons license.

  • Your support will help MIT OpenCourseWare continue to

  • offer high-quality educational resources for free.

  • To make a donation, or view additional materials from

  • hundreds of MIT courses, visit MIT OpenCourseWare

  • at ocw.mit.edu .

  • PROFESSOR JOHN GUTTAG: Good morning.

  • We should start with the confession, for those of

  • you looking at this on OpenCourseWare, that I'm

  • currently lecturing to an empty auditorium.

  • The fifth lecture for 600 this term, we ran into some

  • technical difficulties, which left us with a recording we

  • weren't very satisfied with.

  • So, I'm-- this is a redo, and if you will hear no questions

  • from the audience and that's because there is no audience.

  • Nevertheless I will do my best to pretend.

  • I've been told this is a little bit like giving a speech before

  • the US Congress when C-SPAN is the only thing watching.

  • OK.

  • Computers are supposed to be good for crunching numbers.

  • And we've looked a little bit at numbers this term, but I now

  • want to get into looking at them in more depth than

  • we've been doing.

  • Python has two different kinds of numbers.

  • So far, the only kind we've really paid any attention

  • to is type int.

  • And those were intended to mirror the integers, as we

  • all learned about starting in elementary school.

  • And they're good for things that you can count.

  • Any place you'd use whole numbers.

  • Interestingly, Python, unlike some languages, has what

  • are called arbitrary precision integers.

  • By that, we mean, you can make numbers as big

  • as you want them to.

  • Let's look at an example.

  • We'll just take a for a variable name, and we'll set

  • a to be two raised to the one-thousandth power.

  • That, by the way, is a really big number.

  • And now what happens if we try and display it?

  • We get a lot of digits.

  • You can see why I'm doing this on the screen instead of

  • writing it on the blackboard.

  • I'm not going to ask you whether you believe this is

  • the right answer, trust me, trust Python.

  • I would like you to notice, at the very end of

  • this is the letter L.

  • What does that mean?

  • It means long.

  • That's telling us that it's representing these-- this

  • particular integer in what it calls it's

  • internal long format.

  • You needn't worry about that.

  • The only thing to say about it is, when you're dealing with

  • long integers, it's a lot less efficient than when you're

  • dealing with smaller numbers.

  • And that's all it's kind of warning you, by

  • printing this L.

  • About two billion is the magic number.

  • When you get over two billion, it's now going to deal with

  • long integers, so if, for example, you're trying to deal

  • with the US budget deficit, you will need integers of type L.

  • OK.

  • Let's look at another interesting example.

  • Suppose I said, b equal to two raised to the nine hundred

  • ninety-ninth power.

  • I can display b, and it's a different number, considerably

  • smaller, but again, ending in an L.

  • And now, what you think I'll get if we try a divided by b?

  • And remember, we're now doing integer division.

  • Well, let's see.

  • We get 2L.

  • Well, you'd expect it to be two, because if you think about

  • the meaning of exponentiation, indeed, the difference between

  • raising something to the nine hundred ninety-ninth power and

  • to the one-thousandth power should be, in this case, two,

  • since that's what we're raising to a power.

  • Why does it say 2L, right?

  • Two is considerably less than two billion, and that's because

  • once you get L, you stay L.

  • Not particularly important, but kind of worth knowing.

  • Well, why am I bothering you with this whole issue of how

  • numbers are represented in the computer?

  • In an ideal world, you would ignore this completely, and

  • just say, numbers do what numbers are supposed to do.

  • But as we're about to see, sometimes in Python, and in

  • fact in every programming language, things behave

  • contrary to what your intuition suggests.

  • And I want to spend a little time helping you understand

  • why this happens.

  • So let's look at a different kind of number.

  • And now we're going to look at what Python, and almost every

  • other programming language, calls type float.

  • Which is short for floating point.

  • And that's the way that programming languages typically

  • represent what we think of as real numbers.

  • So, let's look at an example.

  • I'm going to set the variable x to be 0.1, 1/10, and now

  • we're going to display x.

  • Huh?

  • Take a look at this.

  • Why isn't it .1?

  • Why is it 0.1, a whole bunch of zeros, and then this mysterious

  • one appearing at the end?

  • Is it because Python just wants to be obnoxious

  • and is making life hard?

  • No, it has to do with the way the numbers are represented

  • inside the computer.

  • Python, like almost every modern programming language,

  • represents numbers using the i triple e floating point

  • standard, and it's i triple e 754.

  • Never again will you have to remember that it's 754.

  • I promise not to ask you that question on a quiz.

  • But that's what they do.

  • This is a variant of scientific notation.

  • Something you probably learned about in high school, as a way

  • to represent very large numbers.

  • Typically, the way we do that, is we represent the numbers in

  • the form of a mantissa and an exponent.

  • So we represent a floating point number as a pair, of a

  • mantissa and an exponent.

  • And because computers work in the binary system, it's unlike

  • what you probably learned in high school, where we

  • raise ten to some power.

  • Here we'll always be raising two to some power.

  • Maybe a little later in the term, if we talk about computer

  • architecture, we'll get around to explaining why computers

  • working binary, but for now, just assume that they do and

  • in fact they always have.

  • All right.

  • Purists manage to refer to the mantissa as a significant, but

  • I won't do that, because I'm an old guy and it was a mantissa

  • when I first learned about it and I just can't break

  • myself of the habit.

  • All right.

  • So how does this work?

  • Well, when we recognize so-- when we represent

  • something, the mantissa is between one and two.

  • Whoops.

  • Strictly less than two, greater than or equal to one.

  • The exponent, is in the range, -1022 to +1023.

  • So this lets us represent numbers up to about 10 to the

  • 308th, plus or minus 10 to the 308th, plus or minus.

  • So, quite a large range of numbers.

  • Where did these magic things come from?

  • You know, what-- kind of a strange numbers to see here.

  • Well, it has to do with the fact that computers typically

  • have words in them, and the words today in a modern

  • computer are 64 bits.

  • For many years they were 32 bits, before that they were 16

  • bits, before that they were 8 bits, they've continually

  • grown, but we've been at 64 for a while and I think we'll

  • be stuck at 64 for a while.

  • So as we do this, what we do is, we get one bit for the

  • sign-- is it a positive or negative number?-- 11 for the

  • exponent, and that leaves 52 for the mantissa.

  • And that basically tells us how we're storing numbers.

  • Hi, are you here for the 600 lecture?

  • There is none today, because we have a quiz this evening.

  • It's now the time that the lecture would normally have

  • started, and a couple of students who forgot that we

  • have a quiz this evening, instead of a lecture,

  • just strolled in, and now strolled out.

  • OK.

  • You may never need to know these constants again, but it's

  • worth knowing that they exist, and that basically, this gives

  • us about the equivalent of seventeen decimal

  • digits of precision.

  • So we can represent numbers up to seventeen

  • decimal digits long.

  • This is an important concept to understand, that unlike the

  • long ints where they can grow arbitrarily big, when we're

  • dealing with floating points, if we need something more than