Simple math problem!

Ok, I am not a mathematic genius ;)

How the heck do I make negative numbers positive, and viceversa?

Joakim

the basic way

x = -1
y = 5

y = (y * x) -- y should now equal negative 5

io.write ("Y=" .. y .."\n") -- this is a better way to 'print' it in your console

y = (y * x) -- y should now equal positive 5

io.write ("Y=" .. y .. "\n") -- this is a better way to 'print' it in your console

goodluck!

Multiplying any value by -1, will flip it's sign.

1
2
3
-5 * -1 = 5
 
5 * -1 = -5

Yes, so simple but so nice. Dividing by -1 did the trick!

Thanks!

Joakim

I hope you meant multiplying by -1.

Dividing by -1 won't quite get you there!

Opps, there seems to be another bug - but it is in my brain ;) Yes, multiply - nothing else!

Joakim

Actually dividing by -1 will get you the same place.

You're probably better off with multiplying though as it is usually a faster operation on cpu. But the fact that most probably lua will notice it and change it to multiplication and the fact that lua works with doubles and on doubles multiplying and dividing takes nearly the same cpu cycles, you can basically do as you please.

views:1454 update:2011/12/30 9:10:41
corona forums © 2003-2011