Home »
Julia
Sign and absolute value functions in Julia
Julia | Sign and absolute value functions: This tutorial contains the Julia library functions related to the sign and absolute values with their usages, syntaxes, and examples.
Submitted by IncludeHelp, on April 04, 2020
Julia | Sign and absolute value functions
The sign and absolute value functions are,
Function |
Description |
abs(val) |
It returns a positive (absolute) value with the magnitude of val. |
abs2(val) |
It returns the squared magnitude of val. |
sign(val) |
It returns the 0 if val==0, or ±1 for real val) |
signbit(val) |
It returns true or false based on the sign bit, it returns true if the sign bit is ON, otherwise, false. |
copysign(val1,val2) |
It returns a value with the magnitude of val1 and the sign of val2. |
flipsing(val1,val2) |
It returns a value with the magnitude of val1 and the sign of val1 * val2. |
Julia program to demonstrate example of Sign and absolute value functions
# Julia program to demonstrate example of
# Sign and absolute value functions
# abs() functions
println("abs(0): ", abs(0))
println("abs(10): ", abs(10))
println("abs(-10): ", abs(-10))
println("abs(10.23): ", abs(10.23))
println("abs(-10.23): ", abs(-10.23))
# special case
println("abs(tyiemin(Int64)): ", abs(typemin(Int64)))
println()
# sign() functions
println("sign(0): ", sign(0))
println("sign(10): ", sign(10))
println("sign(-10): ", sign(-10))
println("sign(10.23): ", sign(10.23))
println("sign(-10.23): ", sign(-10.23))
println()
# signbit() functions
println("signbit(0): ", signbit(0))
println("signbit(10): ", signbit(10))
println("signbit(-10): ", signbit(-10))
println("signbit(10.23): ", signbit(10.23))
println("signbit(-10.23): ", signbit(-10.23))
println()
# copysign() functions
println("copysign(0,-10): ", copysign(0,-10))
println("copysign(10, -10): ", copysign(10, -10))
println("copysign(-10, 1): ", copysign(-10, 1))
println("copysign(10.23, -23): ", copysign(10.23, -23))
println("copysign(-10.23, -2.3): ", copysign(-10.23, -2.3))
println()
# flipsign() functions
println("flipsign(0,-10): ", flipsign(0,-10))
println("flipsign(10, -10): ", flipsign(10, -10))
println("flipsign(-10, 1): ", flipsign(-10, 1))
println("flipsign(10.23, -23): ", flipsign(10.23, -23))
println("flipsign(-10.23, -2.3): ", flipsign(-10.23, -2.3))
println()
Output
abs(0): 0
abs(10): 10
abs(-10): 10
abs(10.23): 10.23
abs(-10.23): 10.23
abs(tyiemin(Int64)): -9223372036854775808
sign(0): 0
sign(10): 1
sign(-10): -1
sign(10.23): 1.0
sign(-10.23): -1.0
signbit(0): false
signbit(10): false
signbit(-10): true
signbit(10.23): false
signbit(-10.23): true
copysign(0,-10): 0
copysign(10, -10): -10
copysign(-10, 1): 10
copysign(10.23, -23): -10.23
copysign(-10.23, -2.3): -10.23
flipsign(0,-10): 0
flipsign(10, -10): -10
flipsign(-10, 1): -10
flipsign(10.23, -23): -10.23
flipsign(-10.23, -2.3): 10.23
Reference: Sign and absolute value functions