diff --git a/vlib/math/math.v b/vlib/math/math.v index b69783261c..3f63b965ed 100644 --- a/vlib/math/math.v +++ b/vlib/math/math.v @@ -322,3 +322,8 @@ pub fn aprox_cos(a f64) f64 { a8 := -1.8776444013090451e-5 return a0 + a * (a1 + a * (a2 + a * (a3 + a * (a4 + a * (a5 + a * (a6 + a * (a7 + a * a8))))))) } + +// copysign returns a value with the magnitude of x and the sign of y +pub fn copysign(x, y f64) f64 { + return f64_from_bits((f64_bits(x) & ~sign_mask) | (f64_bits(y) & sign_mask)) +} diff --git a/vlib/math/math_test.v b/vlib/math/math_test.v index 7e2c01c22b..f869326ea7 100644 --- a/vlib/math/math_test.v +++ b/vlib/math/math_test.v @@ -62,3 +62,11 @@ fn test_mod() { a %= 2 assert a == 0 } + +fn test_copysign() { + assert copysign(5, -7) == -5.0 + assert copysign(-5, 7) == 5.0 + assert copysign(-5, -7) == -5.0 + assert copysign(10, 0) == 10.0 + assert copysign(10, 10) == 10.0 +}