math: copysign()

pull/4147/head
vamsi-shankar 2020-03-28 23:02:38 +05:30 committed by GitHub
parent fa02130359
commit 715d4f6601
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -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))
}

View File

@ -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
}