text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
You use the star for multiplication.
2,906.39
1.88
Let's do one more.
2,908.27
0.75
Printf percent I, divided by percent i, is percent i,
2,909.02
5.56
backslash n. xy divided by y-- so you use the forward slash in C
2,914.58
5.88
to do division.
2,920.46
1.042
And let's do one other.
2,921.502
0.958
Remainder of percent i, divided by percent i, is percent i.
2,927.92
7.32
xy-- and now remainder is what's left over.
2,935.24
4.31
When you try dividing a denominator into a numerator,
2,939.55
3.43
how much is left over that you couldn't divide out?
2,942.98
2.59
>> So there isn't really, necessarily, a symbol
2,945.57
2.34
we've used in grade school for this.
2,947.91
1.56
But there in C. You can say x modulo y, where
2,949.47
4.36
this percent sign in this context-- confusingly when you're inside
2,953.83
4.17
of the double quotes, inside of printf, percent
2,958
2.17
is used as the format specifier.
2,960.17
1.66
>> When you use percent outside of that in a mathematical expression,
2,961.83
3.59
it's the modulo operator for modular arithmetic-- for our purposes
2,965.42
4.49
here, just means, what is the remainder of x divided by y?
2,969.91
3.74
So x divided by y is x slash y.
2,973.65
2.48
What's the remainder of x divided by y?
2,976.13
2.09
It's x mod y, as a programmer would say.
2,978.22
3.56
>> So if I made no mistakes here, let me go ahead and make ints, plural, nice,
2,981.78
6.52
and dot slash ints.
2,988.3
1.71
And let's go ahead and do, let's say, 1, 10.
2,990.01
5.26
All right, 1 plus 10 is 11, check.
2,995.27
3.12
1 minus 10 is negative 9, check.
2,998.39
2.85
>> 1 times 10 is 10, check.
3,001.24
2.18
1 divided by 10 is-- OK, we'll skip that one.
3,003.42
3.67
Remainder of 1 divided by 10 is 1.
3,007.09
2.39
That's correct.
3,009.48
1.2
But there's a bug in here.
3,010.68
1.95
>> So the one I put my hand over, not correct.
3,012.63
2.76
I mean, it's close to 0.
3,015.39
1.28
1 divided by 10, you know, if we're cutting some corners, sure, it's zero.
3,016.67
4
But it should really be 1/10, 0.1, or 0.10, 0.1000, or so forth.
3,020.67
7.38
>> It should not really be zero.
3,028.05
2.55
Well, it turns out that the computer is doing literally what we told it to do.
3,030.6
5.39
We are doing math like x divided by y.
3,035.99
3.47
And both x and y, per the lines of code earlier, are integers.
3,039.46
5.22
>> Moreover, on line 15, we are telling printf, hey, printf plug-in
3,044.68
5.76
an integer, plug-in an integer, plug-in an integer-- specifically
3,050.44
3.79
x, and then y, and then x divided by y. x and y are ints.
3,054.23
3.35
We're good there.
3,057.58
1.48
>> But what is x divided by x?
3,059.06
2.19
x divided by y should be, mathematically, 1/10, or 0.1,
3,061.25
5.54
which is a real number, a real number having, potentially, a decimal point.
3,066.79
4.81
It's not an integer.
3,071.6
1.63
>> But what is the closest integer to 1/10, or 0.1?
3,073.23
5.06
Yeah, it kind of is zero.
3,078.29
2.824
0.1 is like this much.
3,081.114
0.916
And 1 is this much.
3,082.03
0.86
So 1/10 is closer to 0 than it is to one.
3,082.89
2.98
>> And so what C is doing for us-- kind of because we told it to--
3,085.87
4.93
is truncating that integer.
3,090.8
1.8
It's taking the value, which again is supposed to be something like 0.1000,
3,092.6
7.94
0 and so forth.
3,100.54
1.26
And it's truncating everything after the decimal point
3,101.8
3.52
so that all of this stuff, because it doesn't
3,105.32
2.19
fit in the notion of an integer, which is just a number like negative 1, 0, 1,
3,107.51
4.4
up and down, it throws away everything after the decimal point because you
3,111.91
3.92
can't fit a decimal point in an integer by definition.
3,115.83
3.19
>> So the answer here is zero.
3,119.02
2.27
So how do we fix this?
3,121.29
1.31
We need another solution all together.
3,122.6
1.8
And we can do this, as follows.
3,124.4
2.48
>> Let me go ahead and create a new file, this one called floats.c.
3,126.88
5.94
And save it here in the same directory, float.c.
3,132.82
3.68
And let me go ahead and copy some of that code from earlier.
3,139.36
3.9
>> But instead of getting an int, let's do this.
3,143.26
4.43
Give me a floating point value called x. where a floating point
3,147.69
3.347
value is just literally something with a floating point.
3,151.037
2.333
It can move to the left, to the right.
3,153.37
1.04
It's a real number.
3,154.41
1.12
>> And let me call not get int, but get float,
3,155.53
2.52
which also was among the menu of options in the C250 library.
3,158.05
3.37
Let's change y to a float.
3,161.42
1.8
So this becomes get float.
3,163.22
1.78
>> And now, we don't want to plug in ints.
3,165
2.62
It turns out we have to use percent f for float, percent f for float,
3,167.62
5.51
and now save it.
3,173.13
1.43
And now, fingers crossed, make floats, nice, dot slash floats.
3,174.56
6.66
x is going to be one 1. y Is going to be 10 again.
3,181.22
3.06
>> And, nice, OK my addition is correct.
3,184.28
3.96
I was hoping for more, but I forgot to write it.
3,188.24
2
So let's go and fix this logical error.
3,190.24
3.01
>> Let's go ahead and grab the following.
3,193.25
3.03
We'll just do a little copy and paste.
3,196.28
1.8
And I'm going to say minus.
3,198.08
2
>> And I'm going to say times.
3,200.08
1.81
And I'm going to say divided.
3,201.89
2.17
And I'm not going to do modulo, which is not as germane here,
3,204.06
4.18
divided by f, and times plus-- OK, let's do this again.
3,208.24
5.45
>> Make floats, dot slash floats, and 1, 10, and-- nice, no, OK.
3,213.69
10.52
So I'm an idiot.
3,224.21
1.04
So this is very common in computer science
3,225.25
1.75
to make stupid mistakes like this.
3,227
2.78
>> For pedagogical purposes, what I really wanted to do
3,229.78
3.32
was change the science here to plus, to minus, to times,
3,233.1
4.31
and to divide, as you hopefully noticed during this exercise.
3,237.41
3.73