Transcript from the "Negative Numbers" Lesson
>> Okay, here's a fun one. I don't have a benchmark to show you but I think it's fun to talk about for a second. And that is, why in the world is- x slower than 0- x? Anybody who wants to take a crack at this, I think this is a pretty fun one.
>> Go ahead.
>> Is- x a multiplication?
>> No, okay.
>> That was a good one.
>> Does it have to do with coercion?
>> Sort of, yes. Yes, you're on track. So, you were talking about two's complement, right? Negative numbers.
>> No, I was thinking that, is it more steps in the assembly code to change it from positive?
[00:01:08] This is insane, 0- 0? 0, but- 0, -0. Okay, what in the world's going on here, right? To understand this is there is no way for the CPU to encode- 0. Negative 0 doesn't exist because two's complement of zero, remember two's complement is you flip all the bits.
[00:01:31] So you get one, one, one everywhere, and then you add one to it. And when you add one to one, one, one everywhere, it overflows and becomes zero again. So negation of a zero, or two's complement of zero is zero, right? And that's fine, because in math there's no such thing as- 0 unless you do limits, right?
[00:02:40] So this bit that defines the sign, positive or negative, then we have the exponent and mantissa which is basically the value minus the exponent, right? So for 0 you can actually encode- 0 in triple notation and this is why a negative x if x is 0 becomes a floating point number, right?
[00:03:02] And so the problem is if you do 0- x for, if input is 1 then you get- 1, if the input is 0 you get 0, but if you get- x and if you start with 1, then you get with- 1. But if you start with 0, then you get a float.
[00:03:22] And you can see how that would ruin the day for the compiler, right? Because compiler makes an assumption that the number it's getting is going to be an integer. And so now, all of a sudden, that assumption is broken. And so the way you ruin your day is if you try to go and access a property using a integer.
[00:03:41] This is easy, that's easy. Remember, eex instruction and the xx index, there you go, there's your memory, piece of cake. You put a- 0, it's a float, I can't use floats to access the thing. As a matter of fact, I don't know how it is NUMA registers but the old CPUs had dedicated registers for floating point numbers.
[00:04:03] They were separate beasts. If you wanted to deal with floating point numbers, totally different registers, totally different operations you could do on them, et cetera. The arithmetic logic unit for adding and subtracting or multiplying floating points is way different than doing it for regular integers, right? So, the amusing part here is that through a negation, you can end up with a negative floating point number.
[00:04:30] If you try to use it in array axis or property axis, will really mess up the generated code that the compiler has to do. And the result of that is this, that Chrome has been actually pretty good, it's only 3x, it's not too bad. It used to be way worse in the past.
[00:05:10] You would not be allowed to just randomly do these kinds of things.