[ Arithmetic | Reference Manual | Alphabetic Index ]

sgn(+Number, -Result)

-Result is sgn(+Number)

Unifies Result with the sign of Number which is either -1, 0 or 1.
Number
A number.
Result
Output: an integer.

Description

Unifies Result with the sign of Number which is either -1, 0 or 1.

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)
Caution: this operation does not distinguish between +0.0 and -0.0. See copysign/3 for an alternative.

This predicate can be used as a function in arithmetic expressions. In coroutining mode, if Number is uninstantiated, the call to sgn/2 is delayed until this variable is instantiated.

Modes and Determinism

Exceptions

(4) instantiation fault
Number is not instantiated (non-coroutining mode only).
(24) number expected
Number is not of a numeric type.

Examples

    Result is sgn(42).		% gives Result = 1
    Result is sgn(-5).		% gives Result = -1
    Result is sgn(-6.2).	% gives Result = -1
    Result is sgn(0.0).		% gives Result = 0
    Result is sgn(-0.0).	% gives Result = 0

See Also

is / 2, abs / 2, copysign / 3