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
  1. Flip the sign of a positive number when y is negative:

    julia> flipsign(10, -5)
    -10

    In this example, since y is negative, the sign of x (which is 10) is flipped to -10.

  2. No sign flip when y is positive:

    julia> flipsign(8, 3)
    8

    Here, y is positive, so the sign of x (which is 8) remains unchanged.

  3. Flip the sign of a negative number when y is negative:

    julia> flipsign(-15, -2)
    15

    In this case, both x and y are negative, so the sign of x (which is -15) is flipped to positive 15.

  4. Using flipsign with the abs function:
    julia> abs_val = flipsign(7, 7)
    7

    This example demonstrates the relationship between flipsign and the abs function. Since y is positive, flipsign returns x as 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.

*Required Field
Details

Checking you are not a robot: