flipsign
flipsign(x, y)
Return x with its sign flipped if y is negative. For example abs(x) = flipsign(x,x).
Examples
julia> flipsign(2, -1)
-2
julia> flipsign(2, abs(-1))
2
-
Flip the sign of a positive number when
yis negative:julia> flipsign(10, -5) -10In this example, since
yis negative, the sign ofx(which is 10) is flipped to -10. -
No sign flip when
yis positive:julia> flipsign(8, 3) 8Here,
yis positive, so the sign ofx(which is 8) remains unchanged. -
Flip the sign of a negative number when
yis negative:julia> flipsign(-15, -2) 15In this case, both
xandyare negative, so the sign ofx(which is -15) is flipped to positive 15. - Using
flipsignwith theabsfunction:julia> abs_val = flipsign(7, 7) 7This example demonstrates the relationship between
flipsignand theabsfunction. Sinceyis positive,flipsignreturnsxas is, and therefore,abs(x) = flipsign(x,x).
Common mistake example:
julia> flipsign(3, 0)
ERROR: DomainError with -0.0:
In this example, the function call results in a DomainError because y is zero. The y argument must be a non-zero number to avoid this error. Make sure to provide a valid and non-zero y value when using flipsign.
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.