0

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) 
4

2 回答 2

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

相当于数学中的分段函数。

于 2013-07-03T08:26:38.473 回答
1

你的方程很接近,但需要绝对值:

vx = (vx + (math.abs(vx)/vx)) * (-1)
于 2013-07-02T21:42:52.940 回答