Btw, I wonder if there is any other mathematical way of always returning the positive version of a number. I looked at the Math.abs function in Google Chrome and this is what they're doing:
JellyFish wrote:Dur... Why didn't I think of that? lol
Thanks.
Btw, I wonder if there is any other mathematical way of always returning the positive version of a number. I looked at the Math.abs function in Google Chrome and this is what they're doing:
Well as it turns out, my original idea is flawed, due to the fact that I didn't understand the binary representation of numbers in JavaScript.
I guess the standard by which JavaScript (or ECMAScript) inherits for it's numbers is IEEE-745. In the standard negative numbers are almost opposite in binary form to there equivalent positive:
Basically the last bit (left most bit) is the signed bit, the bit which tells you whether the number is positive or negative. But when a number is negative all the bits are inverted, meaning all 1s are 0s and 0s are 1s. I thought that maybe a negative number would just be the same as a positive number only the signed bit was turned on. But in fact the whole thing is inverted.
My original idea was to get a bitmask to turn off the signed bit:
0111 1111 1111 1111 1111 1111 1111 1111
and use this to do a bitwise AND operation on another number, causing that number to always be positive. It would have been simple, but now I need to put a little more thought into it.
JellyFish wrote:My original idea was to get a bitmask to turn off the signed bit:
0111 1111 1111 1111 1111 1111 1111 1111
and use this to do a bitwise AND operation on another number, causing that number to always be positive. It would have been simple, but now I need to put a little more thought into it.
That's why I gave you the left shift bitwise operator example "<<" it will shift all bits to the left, effectively dropping the most significant bit (MSB) (i.e. the one we expect to be the sign bit). The right shift will shift all bits to the right and will insert a 0 value for the new MSB.
will always return the absolute value of X regardless of how long X is (so we don't need to precalculate bitmask). But ... if the MSB is indeed the sign bit.
JellyFish wrote:I guess the standard by which JavaScript (or ECMAScript) inherits for it's numbers is IEEE-745. In the standard negative numbers are almost opposite in binary form to there equivalent positive:
I see but this only works on integers in other languages. Like I said earlier JavaScript uses a very different standard on dealing with numbers. A negative number in JavaScript is an inverted positive number plus one:
You asked "What were/are you thinking?"
I just want to show you an alternative (and better IMHO) approach to your AND-bitmask approach.
It was clear it won't work in JS as I said in my 3rd post
There are 10 types of people in this world, those who understand binary and those who don't
JellyFish wrote:PS: Why did ECMA decide to use such awkward standard?
Oh...
Now I remember that from school. I don't know how it's called in English but in Bulgarian it is "additional code".
You can calculate it in two ways - the one you already did (the "reverse code" + binary 1) and:
where M is the modulus (again, I don't know the English word) of the machine word. E.g. for 8 bit machine word M = 100000000.
As far as I remember the "additional code" is always used for binary substraction.
There are 10 types of people in this world, those who understand binary and those who don't
I guess what your saying is the modulus is the number that we never get to but rather just start over again? For example, in 8-bit numbers the 9th bit would be the modulus.
Anyhow, what I need is some kind of formula like so:
+n = f(-n) and +n = f(+n)
What I mean by this is positive n is what f returns regardless of whether f's argument is positive or negative. f is the formula I'm searching for.