Equation that produces negative one if x is less than zero and vice versa

2.2k Views Asked by At

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

There are 4 best solutions below

11
On BEST ANSWER

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

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

0
On

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

2
On

Why not something like

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

With some appropriate way to handle x=0?

2
On
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 $$