Dear viewer, these subtitles were generated by a machine via the service Trint and therefore are (very) buggy. If you are capable, please help us to create good quality subtitles: https://c3subtitles.de/talk/2092 Thanks!
Hello and welcome to the film channel.
The next speaker is Antonio Sefwi,
talking about the dark arts of floating
point numbers and why multiplying
a number by end is not always
the same as adding it by itself and times
of expression and so on.
To talk it off of Don't you possessed?
That's what I did, whom the I to
that song, to her on CompTIA and web
player and this and unfun native
to translate it and now
have fun with the talk.
So I
am Antonia's and I'm going to talk
about floating print numbers, which
in computer programing languages
are generally the numbers that have a
decimal point in them, like the ones on
the left, mostly the
grunts underwrite the integers.
Use a different number type except for
JavaScript, in which they're both
floating points.
So if you've done pretty much any
programing at all, you've probably
interacted with these and you might have
noticed that they're a bit weird
sometimes.
For example, I'm going to start
with this. This is a very
big number one is 16.
That's everyone with 16 zeros,
and I'm not going to take 25
just so it's a pretty small number,
right?
I'm going to add the big number and then
I'm going to subtract it again.
And that shouldn't do anything right.
That doesn't make sense.
To add another man subtracted again, I
think should happen.
But actually, if we do this, we get back
24, not 25,
which is a bit strange.
I think that's fair to say.
So let's try some of the other stuff.
Let's do at one the big number
and see what changes and
nothing changes, right?
Like, you're not you're not even supposed
to do this. You're not supposed to have
floats for exact equality because there
might be the rounding errors, but these
two fields are exactly equal.
I added one, and literally nothing has
changed.
So this is strange, right?
So for this talk, I kind
of thought about all of this very cool
and very complex stuff that I could
talk about, but I'm really not sure
where
y'all are at with your math knowledge
because it can vary quite a bit.
So I just decided to start very
basic.
And so the beginning of the talk is going
to be very simple, and then it's going
to get progressively more complex.
And I hope that this is a good
compromise.
The beginning might be a bit to
might be a bit boring.
If you know that's already the end might
be a bit hard if this is the first time
you're hearing this, because it's really
not a lot of time to understand.
But I hope that that's a good compromise.
Hey, going to start part one
and to start my explanation, I've brought
like the display of like one of these
old calculating machines, right?
So it's just a bunch of zeros
right now. The reason why this is useful
is that it has a limited number of digits
and
computers when they're autonomous, only
half of them the number of digits.
So this is going to help us see some of
these things that happen.
This is a very simple machine.
I can add some numbers and then
I can also add a second number and
then I can add them together.
And that's a result.
It can't really do much more, but it's
got these other two interesting buttons
that I can use to shift the whole number
just one digit to the left and
also back to the right
again, right? And you can see it goes
goes across the decimal point.
So and just because this is going
to be a bit important, I'm going to show
you a trick what this does because it
turns out with our number system, it's
this machine uses shifting a number
left, multiply it by ten.
So we've got a two here.
I'm going to shift left
and we get to 20, which is
ten times too.
And if I shift again, then
we get twenty times ten, which is 200.
And that's that.
So that's nice.
And this
also works and you are so right.
So shifting
shifting right again is going to
divide by 10.
So I'm going from twenty to twenty to
twenty and from twenty
back to two.
OK.
This is actually a fundamental property
of our number system
because what this means is that
we only need the digits from zero to
nine. We don't have a digit for
10 because in order to write
10, we use the digit for one.
And then we shift it once and
once we've done the shift, it gets
multiplied by 10.
And then we can represent 10.
By that, we don't need the digit.
OK. This would become important later.
So now let's do a bit of math with
this machine. And because I'm an
astrophysicist
and actually just because I need an
excuse to be a bit mean because I want to
show some stuff, I'm going
to enter the mass of demos, which is one
of the moons of Mars
in kilograms.
That's kind of heavy.
Say, one and eight, followed by
14 zeroes.
So I'm going to start typing one two
three four five six
and have arrived at the end
of the display we have here, right?
That's a bit.
That's a bit inconvenient,
because in
order to have the phone number and we
have, we have eight zeros that are
missing from this.
So what do we do?
I'm just going to write them down, right?
I mean, you know, what's meant by those
I've just told you when you see the
number on the display, you just have
to add an extra zeros and
then you get the actual number that I'm
into, right?
Actually, now that I have this account up
there, I could have saved myself
some bureaucrat. I could have typed one
zero loss and written down an extra zero
and I could have.
It's not supposed to do that
and I could have saved myself another
year and another zero and
so on. And now we're back at this.
So one and eight with
14 zeroes after it.
So it's kind of it's it's very easy,
right?
And the other thing that's that's kind of
interesting to notice is that shifting
and moving zeros around as kind of the
same thing.
Right. I can shift to the left
and that's the same as taking
a zero out of the counter out there and
moving it back in the No.
And I can also shift the number
right, which is the same as
putting a zero up in the counter.
And actually so
and actually, now that we're talking
about shifts,
maybe we could do this right.
We could shifted one extra bit and
now no longer makes sense to
think about us as the number of zeros
that you have to append.
But it's so rux if you think of it as the
number of shifts you have to do
to the left, which is this way for you,
the number of shifts you have to do
to get back the number right of the
display we had was a lot bigger.
We could actually
shift the whole
number out to the right,
to the left, like this, like you see
on the screen.
So it's just as well
as adding zeros, and it's just that
with this, they kind of look the same.
OK.
So once you've got this,
let's do some extra math
and let's go back here and
I'm going to say I want to add
one kilogram one singer kg
to the mass of demos,
and I'm going to do it.
The obvious, but wrong by first,
which is I'm just going to add one
and I'm going to add them together.
And so now if
I look at the phone number, if I do the
shifting,
I didn't the one
I added the one with eight zeros.
And if you go back, just
kind of makes sense because
right here I'm
the one that I enter.
The kind of the display setting
was that eight shifts.
So of course, anyone that I enter has
its efforts, but I don't want
one with it, so I want one with zero
shifts. So what do I do?
Well,
if you remember
what I just said a few minutes ago,
I can change the countdown of there by
shifting the number around specifically.
I can shift the number to the right
and increase the number of shifts that we
get just like that, right?
So I can just keep shifting it to the
right.
And so the counters, it's
another match, and
now we can do the addition.
And if we now do the same, shifting
out the number one thing and you can see
that it works out, exactly.
We've literally added just one kilogram
to this mess.
That's kind of an important point.
If we work with these shifts, when we do
an addition, we have to make sure that
the shifts match.
And now you can also, I guess
now you can kind of see why I chose
the mass of a very specific
astronomical object,
right?
Because of it, it had been just a little
bit heavier.
If I wanted to add 0.1
kilograms instead of one
than the one I had wanted to add would
have fallen off the back of the display,
right?
So the addition wouldn't
have changed anything,
which is very similar to what you
saw in the second slide.
And to take this a bit further,
if we had added something like 1.2
kilograms, the one would have stayed
because it would have been in the in the
digit range, but they point to what have
gotten cut off.
So if we
then subtracted the mass of 10
plus again, then we would
have gone from one point to.
KG to just one,
because the point two would have gotten
cut off, and that's similar to
the other case on the second slide.
Right.
But to really understand that
we're going to have to go one level
deeper
and the
only thing I'm going to change and this
sounds kind of small.
Remember how I had shifting a number
to the left
would multiply it by 10
to the right? What?
Yeah, to the left.
When might have had better?
Well, I'm not going to change that factor
to two.
And so we don't forget I've I've changed
the fund as well.
And so let's look real quick at what that
does. I've entered one.
I'm now going to shift it.
And this looks like a 10, right?
This looks like this many fingers.
But it isn't because the shift factors.
Two and one times to
adjust to that said, to
some of you know this, this is called
binary.
And the reason that computers like
to use it is because now obviously,
we no longer need a digit for two
and we don't need any higher digits, just
like we didn't need the digit 10 for
a nominal system.
And what this means is that we only need
0.1 as digits and computers
like that, because that could be having
two things.
All right. So.
Let's use this to
actually understand that the twenty four,
twenty five example I gave on the
second slide and I'm not going
to actually use one, is 16
as a number because it's very large
and because we need and out of digits.
And that's not super easy to understand,
but I'm going to show you an equivalent,
an equivalent thing.
And so it's a very large number that I'm
going to use here for this specific
display with this number of digits.
The one you can see right here, let's say
one, and then it's got like seven zeroes
after it and you've got nine extra
shifts.
That's the large number, and
this is our number 25.
That's 25 in binary, it's going to
have to trust me.
And we want to add them together,
which means remember, we have to match
the shifts.
So this is at nine versus at zero
and two.
We now have to get those to nine shifts,
which means we have to shift
the spec just like us.
And you can already see that there's one
digit.
The one at the very end is falling off
the display, right?
But let's keep this just
for proper decorum.
We're going to do the addition
like this.
Then we're going to round like this
and then I'm going to subtract
the very large number again, like
this and then we can shift
the result back to the front so we can
read it.
Now I see what shifts.
And you can see this is very similar to
the number we started with, except
for this one that's missing
that got rounded away after
the addition we did.
And so there
is no one missing from a 25 in
we 24.
So basically, the reason that
we went from 25 to 24 is the same
as with the decimal system where we would
have gone from 1.2 kg
to just one.
That's that the last digit has
gotten rounded off to zero,
except that.
It's it's not the last decimated,
it's the last time we did it.
And so by the time it gets converted back
into decimal and shown to you on the
screen, it's no longer asking you to see
that it's just the that's missing.
OK.
So
I've just said weird
thing with the rounding, and that was
proper decorum,
which sounds a bit weird.
So so what did I mean by that?
Well, up until now,
everything we've talked about is kind of
general properties of floating point
numbers, right?
You don't need a specific system,
but for what comes next, we're going to
need more specifics.
And we're going to look at
one very specific system of floating
point numbers.
That's pretty much the only
that's in use today, and
that is actually seven
five four standard.
No matter whether you use floats in Ruby,
your Python or JavaScript or sea or
rust or C-sharp, or I
don't know whatever if you use a float
on a computer.
It's almost certainly one of these.
And so what
does this what does this tell us
about how we have to do floats?
But it specifies digit lengths,
of course, for different types, right?
So that's standardized and also
tells us a standardized way to read
floats. So if I take the twenty five
again a test that we have to shift
the float such that that's only one
digit and one of the decimal point.
Like so since
we can shift them around freely, this
helps us kind of get
down to only one valid representation
for each number, right?
And because we do it this way, we don't
need all these digit digits in front.
And since the first digit
is always going to be a one right,
it can be zero.
Otherwise, we could shift the number, and
that's one of the first position.
We don't even really need to start the
first one. So it's a very efficient
format and.
Yeah. What else does it say?
It also says that when we do an
operation on
a No on two numbers,
we have to compute the actual result
of the of the operation
with as much precision as we need
to represent it exactly,
and only then round it down
into the number of digits that we
actually have available,
right?
And it also specifies
a few rounding modes
for this particular round.
So you could always shrink the number up
or down, or it was something
similar to what you're used to, which is
down to the nearest number,
right?
And so
this extra another which we can now
finally look at
the retreat that I made
a year ago that inspired this whole talk.
And that's
bad. If you take
no attempts to, that's
the same as adding it to itself,
right? That's two times a
day, you say. No, that's the same as if I
say.
And if I take the number of times three,
that's the same as adding the number two
itself three times
and so on the four and five.
And
it turns out that if you do this
and 13 points, all of these
are exact equalities.
And
except for except for the
six times eight case, which is not an
exact equality anymore.
And so what's going on here intuitively
is that
on the right side, there's all these
additions and every addition
as a separate operation.
So it computes the sum
as a very exact result and then it has
to round it down.
And then it adds in another
copy of the number, and it has a very
exact out and then it gets around it
down. And so it does this multiple times,
and for the first few steps,
it just so happens that all of
these rounding errors kind of cancel out
and work out, and
only for the last one,
only four six times a day, it finally
gets out of step.
And so six times a and
they are almost equal.
So basically, it's like the last digit
is a little bit different,
but they're not, but they're not exactly
equal. And that's kind of the reason why
people tell you to never
compare shots for exact equality, right?
Because smugglers rounding arrows like
this can add up and opinions out
and mess everything
up.
But I think it's interesting to look at
why it works for the first few
cases and why it doesn't
work for the last one.
So
let's start with this number.
This is the number that we're going to
try to sort of set
for shifts, and it's got this
binary pattern that's pretty simple.
And so I'm now going to add it to
itself like this at
them together.
We get this.
Remember that we're supposed to have only
one digit in front of the decimal point,
so we shift it back and increase the
number of shifts.
And
now you can see that in the shifting
operation,
we've lost a digit of precision
at the back.
Now, in this case, it doesn't matter
that digit is zero.
And because we multiply by two
and multiplying by two is always
a left and
multiplying by two.
It's always a left shift.
The last digit is always zero.
So when we shift it back to
make sure we only have pundits in front,
it doesn't change the decision.
So what this means is that
right here on screen is
the exact result of two times a
quarter of a percent.
And there's no difference yet.
So let's do the next step.
I'm going to push this up
and I'm typists, and I'm you just compute
it.
Below is our original number.
I have now had to shift it back,
eventually member for additions to match
the number of shifts because you went
from four to five.
Three.
And now we can
do this citizen, OK,
and you can see this already a digit
hanging over the end.
But you've also got two in front of the
decimal point, so we have to shift again.
Like that's know, six shifts
and now there's digits past the decimal
points.
And now we have to round.
And what this means is that the results
of three times our original number.
It's not something that we can represent
in the simple display of our machine.
We're going to have to round
like this.
And what this means is that now
when we take this number, which
is almost three times our number,
but not exactly.
And add on another copy,
the result can be exactly
four times the number that we started
with.
So let's see what happens.
I'm going to
I'm going to shift this up right and
then add another copy of our number.
This time shifted twice because we now
check six shifts and
do the addition.
And that's our results.
And so to compare what should actually
happen after the multiplication
by far. Well, it's basically
worth multiplying by two two times.
So it should look like this, right?
Remember how with multiplication by two?
We only had a zero left over at the end
that got cut off.
So here there's two zeros that
got cut off.
And again,
four times the number is
an exact result.
And now it just so happens.
This is the result that we get that we
actually get out.
And the error that that you see
on the very right as
the result of the rounding error that we
had for three times.
And now it just so happens that
the rounding error gets
rounded off again once we go
to four times the number just like
this.
And it turns out
that there's a few different other cases
for this.
But with the default rounding mode, which
I say round to nearest mode.
Just works out that
it's the second
that the addition onto the three times
eight always cancels
out and forms a still
exactly equal to
a plus a plus eight plus eight
and so cents
for a as an exact number, just
like to a going from four
to five as the set is the same
as going from two to three.
But again, five
can't necessarily be represented
exactly right.
There might be some digits that could
pushed off the back end that have to be
rounded off.
And then finally, when we go
from five to six, the need,
that's a rounding trick that made
everything, everything just
work out. So for the step
from three to four, it doesn't work
anymore.
And so for six times
a day and all factors above it
that they no longer match
right and then the rounding errors just
keep accumulating,
but not gaps.
So.
Why do all of this
right, in most cases, you might
be fine with the old advice
that we started with, which is don't
compare floats for exactly
quality, always use an approximate,
approximate comparison
and, well, a it's
fun. I hope to
look at this in more detail.
And the other reasons
are, depending on what you do
with us, you
might run into problems where
rounding errors accumulate
or
rounding errors work out just the way
that to numbers you subtract
accidentally subtract to zero and
then you divide by zero and to get
infinity, and then you divide by zero
again and you get an N..
So in these cases, it can be very helpful
to know how it floats really work
and also to know, well,
maybe understand a little bit why you're
supposed to only
compare for approximate equality
and not exact equality.
And so you're not confused when this
happens the next time.
I kind of put myself in the foot of it
with this talk because I went in with,
Oh my god, I can finally talk about all
of this really, really interesting and
kind of weird floating point stuff.
And then I started out with
the very basic terms, and
I'm kind of at the limit for what I want
to do in this single talk just for
my brain as well as yours, probably.
So
I hope that this was worth it.
The very basic explanation at the
beginning.
And.
Yeah, that
that you're not disappointed that we
didn't get to maybe some of the
even more fancier details that
you might be able to get to.
And I hope that
on the other hand, my very quick
explanation of especially the more
advanced stuff was to understand
understandable bomb to fill
in some of the gaps, like the other
cases, for rounding, for going from three
to four day or a
few loose ends that I've left in the talk
in a few places and that's if
you've seen this for the first time, can
be tied up very neatly with some other
concepts that you may have seen before.
That's going to be a Supplementary
Material Materials page if
I managed to put this up between the time
of recording this and the Congress
actually happening, I hope.
And if
you have questions or feedback
or want to tell me
that I included too much basic
stuff that I included too much advance
stuff, you can find me
in these locations.
But for now, I want to thank you for
listening. And yeah.
Yeah, thank you very much for this
very interesting talk.
Next up is at six
p.m. Why one should not implement
time zones themselves,
showing us a few curiosities from
the Air and Air Time Zone database.
And now the Herald News Show.
Have fun.