Skip to content

Commit 3932e2e

Browse files
bors[bot]kirch7japaric
committed
117: implement cosh r=japaric a=kirch7 `cosh(f64)` implemented. I had to implement `__expo2(f64)` also. Co-authored-by: Cássio Kirch <[email protected]> Co-authored-by: Jorge Aparicio <[email protected]>
2 parents 0e97df2 + 7175c5b commit 3932e2e

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ pub trait F64Ext: private::Sealed + Sized {
394394

395395
fn sinh(self) -> Self;
396396

397-
#[cfg(todo)]
398397
fn cosh(self) -> Self;
399398

400399
fn tanh(self) -> Self;
@@ -569,7 +568,6 @@ impl F64Ext for f64 {
569568
sinh(self)
570569
}
571570

572-
#[cfg(todo)]
573571
#[inline]
574572
fn cosh(self) -> Self {
575573
cosh(self)

src/math/cosh.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use super::exp;
2+
use super::expm1;
3+
use super::k_expo2;
4+
5+
#[inline]
6+
pub fn cosh(mut x: f64) -> f64 {
7+
/* |x| */
8+
let mut ix = x.to_bits();
9+
ix &= 0x7fffffffffffffff;
10+
x = f64::from_bits(ix);
11+
let w = ix >> 32;
12+
13+
/* |x| < log(2) */
14+
if w < 0x3fe62e42 {
15+
if w < 0x3ff00000 - (26 << 20) {
16+
let x1p120 = f64::from_bits(0x4770000000000000);
17+
force_eval!(x + x1p120);
18+
return 1.;
19+
}
20+
let t = expm1(x); // exponential minus 1
21+
return 1. + t * t / (2. * (1. + t));
22+
}
23+
24+
/* |x| < log(DBL_MAX) */
25+
if w < 0x40862e42 {
26+
let t = exp(x);
27+
/* note: if x>log(0x1p26) then the 1/t is not needed */
28+
return 0.5 * (t + 1. / t);
29+
}
30+
31+
/* |x| > log(DBL_MAX) or nan */
32+
k_expo2(x)
33+
}

src/math/k_expo2.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use super::exp;
2+
3+
/* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */
4+
const K: i32 = 2043;
5+
6+
/* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */
7+
#[inline]
8+
pub(crate) fn k_expo2(x: f64) -> f64 {
9+
let k_ln2 = f64::from_bits(0x40962066151add8b);
10+
/* note that k is odd and scale*scale overflows */
11+
let scale = f64::from_bits(((((0x3ff + K / 2) as u32) << 20) as u64) << 32);
12+
/* exp(x - k ln2) * 2**(k-1) */
13+
exp(x - k_ln2) * scale * scale
14+
}

src/math/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod ceil;
1919
mod ceilf;
2020
mod cos;
2121
mod cosf;
22+
mod cosh;
2223
mod coshf;
2324
mod exp;
2425
mod exp2;
@@ -77,6 +78,7 @@ pub use self::ceil::ceil;
7778
pub use self::ceilf::ceilf;
7879
pub use self::cos::cos;
7980
pub use self::cosf::cosf;
81+
pub use self::cosh::cosh;
8082
pub use self::coshf::coshf;
8183
pub use self::exp::exp;
8284
pub use self::exp2::exp2;
@@ -127,6 +129,7 @@ mod expo2;
127129
mod fenv;
128130
mod k_cos;
129131
mod k_cosf;
132+
mod k_expo2;
130133
mod k_expo2f;
131134
mod k_sin;
132135
mod k_sinf;
@@ -140,6 +143,7 @@ mod rem_pio2f;
140143
use self::expo2::expo2;
141144
use self::k_cos::k_cos;
142145
use self::k_cosf::k_cosf;
146+
use self::k_expo2::k_expo2;
143147
use self::k_expo2f::k_expo2f;
144148
use self::k_sin::k_sin;
145149
use self::k_sinf::k_sinf;

test-generator/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ f64_f64! {
703703
cbrt,
704704
ceil,
705705
cos,
706-
// cosh,
706+
cosh,
707707
exp,
708708
exp2,
709709
expm1,

0 commit comments

Comments
 (0)