Skip to content

Commit fe5211a

Browse files
committed
Use Ieee754::copy_sign
1 parent aa77262 commit fe5211a

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/tanh.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use ieee754::Ieee754;
2+
13
/// Calculate the numerator of the `tanh` approximation.
24
fn a(x: f32) -> f32 {
35
let x2 = x * x;
@@ -32,15 +34,12 @@ pub fn tanh(x: f32) -> f32 {
3234

3335
let a = a(x);
3436
if !a.is_finite() {
35-
return if a < 0. { -1. } else { 1. };
37+
return 1_f32.copy_sign(a);
3638
}
3739

3840
let result = a / b(x);
39-
if result > 1. {
40-
return 1.;
41-
}
42-
if result < -1. {
43-
return -1.;
41+
if result.abs() > 1. {
42+
return 1_f32.copy_sign(result);
4443
}
4544
result
4645
}

0 commit comments

Comments
 (0)