[ Arithmetic | The ECLiPSe Built-In Predicates | Reference Manual | Alphabetic Index ]
sgn(+Number, ?Result)
Unifies Result with the sign of Number which is either -1, 0 or 1.
- +Number
- A number.
- ?Result
- A variable or an integer.
Description
This predicate is used by the ECLiPSe compiler to expand evaluable
arithmetic expressions. So the call to sgn(Number, Result) is
equivalent to
Result is sgn(Number)
which should be preferred for portability.
sgn/2 gives the integer -1 if Number is negative, 0 if it is zero and 1
if it is greater than zero. It is always true that
X =:= sgn(X) * abs(X)
In coroutining mode, if Number is a free variable, the call to sgn/2 is
delayed until this variable is instantiated.
Fail Conditions
Fails if the result of the evaluation does not unify with Result.
Resatisfiable
No
Exceptions
- (4) instantiation fault
- Number is not instantiated (non-coroutining mode only).
- (5) type error
- Result is a non-integer number.
- (24) number expected
- Number is not of a numeric type.
- (24) number expected
- Result is neither a number nor a variable.
Examples
Success:
sgn(42, 1).
sgn(-5, Result). (gives Result = -1)
sgn(-6.2, Result). (gives Result = -1)
sgn(0.0, 0).
Fail:
sgn(1, 0).
Error:
sgn(A, 6). (Error 4).
sgn(1, 1.0). (Error 5).
sgn(4 + 2, 6). (Error 24).
sgn(1, r). (Error 24).
See Also
is / 2