Bnhjhvbq7
Bnhjhvbq7

Reputation: 169

How to add +1 or -1 to a number

I'm working on a script where I need to switch number from plus to minus or otherwise, and add +1 or -1 according to if it's minus or plus. now I know I can check with if vx < 0 then ... but how can I do it with mathematical formula?

This is what i've tried, but it worked out only for +

vx = (vx + (vx/vx)) * (-1) 

Upvotes: 0

Views: 169

Answers (2)

Deco
Deco

Reputation: 5159

vx = (vx + (vx < 0 and -1 or 1)) * (-1)

Equivalent to a piecewise function in mathematics.

Upvotes: 1

Your equation is close, but needs the absolute value:

vx = (vx + (math.abs(vx)/vx)) * (-1)

Upvotes: 1

Related Questions