Alex Rivera | Logout

Lua decimal sign?

Asked 2009-08-23T10:15:27.570
9

I've used this in other languages, but lua seems to be lacking this rather useful function.

Could one of you nice chappies provide me a lua function to get the sign of the number passed to it?

Edit
Report

2 Answers

13

Just in case anyone stumbles on this one:, here's my somehow shorter version:

function sign(x)
  return x>0 and 1 or x<0 and -1 or 0
end
answered 2010-04-12T23:42:50.663
-2

You can check for the sign like this:

i = -2
if i == math.abs(i) then -- or i >= 0
   print "positive"
else
   print "negative"
end
answered 2009-08-23T10:33:30.637

Your Answer