Skip to content

Commit

Permalink
Use Ieee754::copy_sign
Browse files Browse the repository at this point in the history
  • Loading branch information
vks committed Mar 6, 2019
1 parent aa77262 commit fe5211a
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/tanh.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ieee754::Ieee754;

/// Calculate the numerator of the `tanh` approximation.
fn a(x: f32) -> f32 {
let x2 = x * x;
Expand Down Expand Up @@ -32,15 +34,12 @@ pub fn tanh(x: f32) -> f32 {

let a = a(x);
if !a.is_finite() {
return if a < 0. { -1. } else { 1. };
return 1_f32.copy_sign(a);
}

let result = a / b(x);
if result > 1. {
return 1.;
}
if result < -1. {
return -1.;
if result.abs() > 1. {
return 1_f32.copy_sign(result);
}
result
}
Expand Down

0 comments on commit fe5211a

Please sign in to comment.