I used this in other languages, but lua seems to lack this pretty useful feature.
Can one of you glorious sentences give me the lua function to get the sign of the passed number?
You can check signas follows:
sign
i = -2 if i == math.abs(i) then -- or i >= 0 print "positive" else print "negative" end
function math.sign(x) if x<0 then return -1 elseif x>0 then return 1 else return 0 end end
- : - :
function sign(x) return x>0 and 1 or x<0 and -1 or 0 end
, , 1 -1 . , , 0. . , , (x), 0. 0.
I would stick
function sign(x) return (x<0 and -1) or 1 end
You can also get the sign of a number like this:
x/ math.abs(x)
I would use this only for integers, and since Lua does not distinguish ints from floats, I would not use it in Lua at all.
Source: https://habr.com/ru/post/1715809/More articles:Looking for an alternative to .NET CryptoStream? - c #stable match - algorithmJava Stringparsing Ρ Regexp - javaGNU screen: `` quote programmer '' in Readbuf? - gnu-screenData Warehouse Type Bit Data Bit - typesGNU Screen: environment variables - environment-variablesXSLT: complex conversions, sentences? - xmlHow to display custom asp.net compilation error message - asp.netStructureMap AlwaysUnique does not work with constructor injection - structuremaphttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1715815/gnu-screen-files-to-numbered-buffers&usg=ALkJrhj7pC7OBWnF70PfrpZqRPkYu-4pLQAll Articles