2

I have tried coming up with something but i couldn't in the end...

basically i want something to produce -1 or 1. given value of x will not be equal to zero at any point. But if you want to add that, do so in another example. I'd like to have both, but need first one.

so

if x=2 
y=1
if x=-3435
y=-1

what i've tried:

well i thought i am gonna need a way to make any number a one regardless of sign right. so..

x-((x*-1)) working on it :D

can't use square root, absolute value(unless there is a equation for deriving absolute value).

4 Answers4

2

It sounds like you want the sign function. Considering the restriction that $x \neq 0$, you could just write

$$\frac{x}{|x|}$$

  • check my comment thanks... – Muhammad Umer Aug 09 '13 at 05:34
  • @MuhammadUmer What do you mean "find absolute value using math only"? The absolute value is defined mathematically just to be $|x| = x$ when $x \geq 0$, and $|x| = -x$ else. –  Aug 09 '13 at 05:35
  • i am trying to write a equation that can be run in a program that calculate this. i could use conditionals like you said, or use builtin methods. But i am wondering is it really not possible to derive absolute value without ifsss. (greater than zero then this etc..) – Muhammad Umer Aug 09 '13 at 05:38
  • You could also use $|x| = \sqrt{x^2}$, but this will likely increase execution time if it's called a lot. –  Aug 09 '13 at 05:39
  • This does work perfectly fine, but it also sadly represented by a method in programming language. – Muhammad Umer Aug 09 '13 at 06:00
  • i guess it's not possible to get absolute value without using square root. – Muhammad Umer Aug 09 '13 at 06:00
  • 1
    Depending on whether your programming language has bitwise operations, you could use as the fastest method of absolute value (Four operations) the following: (Described in C) (y = x >> 31; x ^= y; x += y & 1;) (In words) (Store the value of arithmetically right-shifting your value (x) by 31 bits to a new variable, y. Store the xor of y and x to x. Store the sum of x and the bitwise and of y and 1 to x. – qaphla Aug 09 '13 at 06:09
  • I dont get bitwise operators at all, would like to change that. But in meanwhile i'd rely on others. So yes, i have done that. Using bitwise operators in js you can find the absolute value like this: (x^(x>>31))+((x>>31)&1) no idea how it works. But it does. http://stackoverflow.com/questions/664852/which-is-the-fastest-way-to-get-the-absolute-value-of-a-number If you could explain that would be wonderful, and link that'd be ok as well. But thanks. :D – Muhammad Umer Aug 09 '13 at 06:19
  • @MuhammadUmer It seems like you're going to a lot of effort to avoid a single case check, which is going to be very quick in terms of execution. –  Aug 09 '13 at 06:21
  • i agree, in js, i could just do this (x>0)?1:-1; which would just return 1 if number is big and -1 if negative. – Muhammad Umer Aug 09 '13 at 06:24
  • check it out http://jsperf.com/absolute-value-bitwisevsifvsbit – Muhammad Umer Aug 09 '13 at 06:30
2

$\frac{|x|}{x}$ where $x \ne 0$.

qaphla
  • 3,890
1

Why not something like

if(x<0) y=-1; else y=+1?

With some appropriate way to handle x=0?

  • trying to squeeze performance if possible. But it all maybe pointless...:D – Muhammad Umer Aug 09 '13 at 06:18
  • If all you want is the sign function, not the absolute value, an even faster way to do that would be sign(x) = (-2) * (x & 0x80000000) + 1, though this again relies on using bitwise and, and on being sure of the representation of your number. – qaphla Aug 09 '13 at 06:21
-1
If you want $0$ or $1$:

$$ \frac { x + | x | } { 2 x } $$

So if $ x = - 7 $ $$ \frac { - 7 + \sqrt { ( - 7 ) ^ 2 } } { 2 ( - 7 ) } = 0 $$

If $ x = 7 $ $$ \frac { 7 + \sqrt { 7 ^ 2 } } { 2 \times 7 } = 1 $$

If you want $ - 1 $ and $ 1 $:

$$ \frac x { | x | } $$

If $ x = - 7 $ $$ \frac { - 7 } { \sqrt { ( - 7 ) ^ 2 } } = - 1 $$

If x = 7 $$ \frac 7 { \sqrt { 7 ^ 2 } } = 1 $$

For $ x < 0 $ to become $ 0 $ and $ x > 0 $ remain $ x $:

$$ \frac { x + | x | } { 2 } $$

If $ x = - 7 $ $$ \frac { - 7 + \sqrt { ( - 7 ) ^ 2 } } { 2 } = 0 $$

If $ x = 7 $

$$ \frac { 7 + \sqrt { 7 ^ 2 } } { 2 } = 7 $$

For $ x < 0 $ to remain $ x $ and $ x > 0 $ become $ 0 $:

If $ x = - 7 $ $$ \frac { - 7 + \sqrt { ( - 7 ) ^ 2 } } { 2 } - \sqrt { ( - 7 ) ^ 2 } = -7 $$

If $ x = 7 $ $$ \frac { 7 + \sqrt { ( 7 ) ^ 2 } } { 2 } - \sqrt { ( 7 ) ^ 2 } = 0 $$

Pim
  • 11