[ Arithmetic | Reference Manual | Alphabetic Index ]

copysign(+X, +Y, -Result)

-Result is copysign(+X, +Y)

Compose a number from magnitude and sign
X
A number
Y
A number
Result
Output: number

Description

Result is a number of the same type and absolute value as X, but with the sign of Y. Result is therefore either identical to X or to -X.

This function correctly considers the sign bits of special floating point values, such as -0.0, infinities and NaNs.

This predicate can be used as a function in arithmetic expressions. In coroutining mode, if X or Y is uninstantiated, the predicate delays.

Modes and Determinism

Exceptions

(4) instantiation fault
X or Y is not instantiated (non-coroutining mode only).
(20) arithmetic exception
Sign of Y is not known
(24) number expected
Number is not of a numeric type.

Examples

Success:
      Result is copysign( 3.0, 2.0).	% gives Result =  3.0
      Result is copysign( 3.0,-2.0).	% gives Result = -3.0
      Result is copysign(-3.0, 2.0).	% gives Result =  3.0
      Result is copysign(-3.0,-2.0).	% gives Result = -3.0

      Result is copysign( 3.0,-0.0).	% gives Result = -3.0
      Result is copysign( 3.0, 0.0).	% gives Result =  3.0
      Result is copysign( 3.0, 0).	% gives Result =  3.0

      Result is copysign( 3, -1.0).	% gives Result = -3
      Result is copysign( 1, -0.0).	% gives Result = -1
      Result is copysign( 3.0, -1).	% gives Result = -3.0
      Result is copysign(-3.0,  1).	% gives Result =  3.0

Error:
      Result is copysign(3.0, -0.1__0.1).	% arithmetic exception

See Also

is / 2, abs / 2, sgn / 2