Home » Julia

copysign() Function with Example in Julia

Julia | copysign() function: In this tutorial, we are going to learn about the copysign() function with example in the Julia programming language.
Submitted by IncludeHelp, on April 04, 2020

Julia | copysign() function

copysign() function is a library function in Julia programming language, it accepts two values as parameters and returns a value with the magnitude of first value and sign of the second value.

Syntax:

    copysign(val1, val2)

Parameter(s):

  • val1 – represents the magnitude value i.e. first value that will be used as a magnitude to construct the return value.
  • val2 – represents the sign value i.e. second value whose sign will be used as a sign to construct the return value.

Return value:

The return type of this method is Int64, it returns a value with the magnitude of val1 and sign of val2.

Example:

    Input:
    val1 = 0
    val2 = 0
    Output:
    copysign(val1, val2): 0

    Input:
    val1 = -123
    val2 = 10
    Output:
    copysign(val1, val2): 123


    Input:
    val1 = 123
    val2 = -1
    Output:
    copysign(val1, val2): -123

Program:

# copysign() Function with Example in Julia

val1 = 0
val2 = 0
println("val1: ", val1)
println("val2: ", val2)
println("copysign(val1, val2): ", copysign(val1, val2))

val1 = -10
val2 = 0
println("val1: ", val1)
println("val2: ", val2)
println("copysign(val1, val2): ", copysign(val1, val2))

val1 = 10
val2 = -10
println("val1: ", val1)
println("val2: ", val2)
println("copysign(val1, val2): ", copysign(val1, val2))

val1 = -10.20
val2 = 1
println("val1: ", val1)
println("val2: ", val2)
println("copysign(val1, val2): ", copysign(val1, val2))

val1 = -10.23
val2 = -10
println("val1: ", val1)
println("val2: ", val2)
println("copysign(val1, val2): ", copysign(val1, val2))

# printing the return type of copysign()
println("typeof(copysign(123, -10)): ", typeof(copysign(123, -10)))

Output

val1: 0
val2: 0
copysign(val1, val2): 0
val1: -10
val2: 0
copysign(val1, val2): 10
val1: 10
val2: -10
copysign(val1, val2): -10
val1: -10.2
val2: 1
copysign(val1, val2): 10.2
val1: -10.23
val2: -10
copysign(val1, val2): -10.23
typeof(copysign(123, -10)): Int64


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.