From fe5211a05bd8072a90543cc7f02f24cc992aa34a Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Wed, 6 Mar 2019 18:11:29 +0100 Subject: [PATCH] Use `Ieee754::copy_sign` --- src/tanh.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/tanh.rs b/src/tanh.rs index f49de45..504652a 100644 --- a/src/tanh.rs +++ b/src/tanh.rs @@ -1,3 +1,5 @@ +use ieee754::Ieee754; + /// Calculate the numerator of the `tanh` approximation. fn a(x: f32) -> f32 { let x2 = x * x; @@ -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 }