implement cosh

This commit is contained in:
Cássio Kirch 2018-07-15 19:49:12 -03:00
parent 3736944c23
commit 06aba07681
4 changed files with 52 additions and 2 deletions

View file

@ -407,7 +407,6 @@ pub trait F64Ext: private::Sealed {
fn sinh(self) -> Self;
#[cfg(todo)]
fn cosh(self) -> Self;
#[cfg(todo)]
@ -589,7 +588,6 @@ impl F64Ext for f64 {
sinh(self)
}
#[cfg(todo)]
#[inline]
fn cosh(self) -> Self {
cosh(self)

View file

@ -0,0 +1,34 @@
use super::exp;
use super::expm1;
use super::k_expo2;
#[inline]
pub fn cosh(mut x: f64) -> f64 {
/* |x| */
let mut ix = x.to_bits();
ix &= 0x7fffffffffffffff;
x = f64::from_bits(ix);
let w = ix >> 32;
let w = w as u32;
/* |x| < log(2) */
if w < 0x3fe62e42 {
if w < 0x3ff00000 - (26 << 20) {
let x1p120 = f64::from_bits(0x4770000000000000);
force_eval!(x + x1p120);
return 1.;
}
let t = expm1(x); // exponential minus 1
return 1. + t * t / (2. * (1. + t));
}
/* |x| < log(DBL_MAX) */
if w < 0x40862e42 {
let t = exp(x);
/* note: if x>log(0x1p26) then the 1/t is not needed */
return 0.5 * (t + 1. / t);
}
/* |x| > log(DBL_MAX) or nan */
k_expo2(x)
}

View file

@ -0,0 +1,14 @@
use super::exp;
/* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */
const K: i64 = 2043;
/* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */
#[inline]
pub(crate) fn k_expo2(x: f64) -> f64 {
let k_ln2 = f64::from_bits(0x40962066151add8b);
/* note that k is odd and scale*scale overflows */
let scale = f64::from_bits(((0x3ff + K / 2) as u64) << 20);
/* exp(x - k ln2) * 2**(k-1) */
exp(x - k_ln2) * scale * scale
}

View file

@ -19,6 +19,7 @@ mod ceil;
mod ceilf;
mod cos;
mod cosf;
mod cosh;
mod coshf;
mod exp;
mod exp2;
@ -75,6 +76,7 @@ pub use self::ceil::ceil;
pub use self::ceilf::ceilf;
pub use self::cos::cos;
pub use self::cosf::cosf;
pub use self::cosh::cosh;
pub use self::coshf::coshf;
pub use self::exp::exp;
pub use self::exp2::exp2;
@ -122,6 +124,7 @@ pub use self::truncf::truncf;
mod expo2;
mod k_cos;
mod k_cosf;
mod k_expo2;
mod k_expo2f;
mod k_sin;
mod k_sinf;
@ -135,6 +138,7 @@ mod rem_pio2f;
use self::expo2::expo2;
use self::k_cos::k_cos;
use self::k_cosf::k_cosf;
use self::k_expo2::k_expo2;
use self::k_expo2f::k_expo2f;
use self::k_sin::k_sin;
use self::k_sinf::k_sinf;