flipsign(x, y)

Return x with its sign flipped if y is negative. For example abs(x) = flipsign(x,x).


julia> flipsign(2, -1)
julia> flipsign(2, abs(-1))
  1. Flip the sign of a positive number when y is negative:

    julia> flipsign(10, -5)

    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)

    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)

    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)

    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

Checking you are not a robot: