copysign

copysign(x, y)

Return x such that it has the same sign as y

Examples

julia> copysign(3.5, -2.0)
-3.5

julia> copysign(-4.6, 1.0)
4.6

In these examples, copysign returns x with the same sign as y. The first example shows that 3.5 has the same sign as -2.0, resulting in -3.5. The second example demonstrates that -4.6 has the same sign as 1.0, resulting in 4.6.

Common mistake example:

julia> copysign(0.0, -3.14)
0.0

In this example, the result is 0.0 because the sign of 0.0 is always considered positive, regardless of the sign of y.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: