abs / sign / odd / even
Definition: |
atom res = abs(atom a)
-- or -- integer res = sign(atom a) -- or -- integer res = odd(atom a) -- or -- integer res = even(atom a) |
Description: |
abs(a) returns the absolute (unsigned) value of x1. sign(a) returns -1, 0, or +1 if a is negative, zero, or positive respectively. odd(a) returns true(1) for eg 1,3,5.., or false(0) for eg 2,4,6. even(a) returns false(0) for eg 1,3,5.., or true(1) for eg 2,4,6. |
pwa/p2js: | Supported. |
Comments: |
sign(a) returns the same result as compare(a,0). Be warned that atoms above or below the 53/63-bit limits always return false for odd() and true for even(), since the least significant bit is guaranteed to be 0 on all such numbers. These functions may be applied to an atom or sq_abs() / sq_sign() / sq_odd() / sq_even() to all elements of a sequence. The rules for sequence operations apply. |
Example: |
?abs(-4) -- prints 4 ?sign(5) -- prints 1 ?sign(0) -- prints 0 ?sign(-2.5) -- prints -1 ?odd(1) -- prints 1 ?even(1) -- prints 0 |
Implementation: |
See builtins\pmaths.e (an autoinclude) for details of the actual implementation. Since 1.0.2 odd() [and even()] are mapped directly to faster equivalent [not] and_bits() expressions by the front end (search for T_odd/T_even in psym.e and pmain.e). |
See Also: | compare |