Simple math problem!

Ok, I am not a mathematic genius :wink:

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

Joakim [import]uid: 81188 topic_id: 19191 reply_id: 319191[/import]

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! [import]uid: 100299 topic_id: 19191 reply_id: 74012[/import]

Multiplying any value by -1, will flip it’s sign.
[lua]-5 * -1 = 5

5 * -1 = -5[/lua]
Am I missing something?

If you want to check a value to see if you need to flip it’s sign you need to figure out if the value is less than or greater than 0.
[lua]if value > 0 – positive

– or

if value < 0 – negative[/lua]
[import]uid: 5317 topic_id: 19191 reply_id: 74013[/import]

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

Thanks!

Joakim [import]uid: 81188 topic_id: 19191 reply_id: 74015[/import]

I hope you meant multiplying by -1.

Dividing by -1 won’t quite get you there! [import]uid: 5317 topic_id: 19191 reply_id: 74051[/import]

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

Joakim
[import]uid: 81188 topic_id: 19191 reply_id: 74141[/import]

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. [import]uid: 61899 topic_id: 19191 reply_id: 74237[/import]