File tree 5 files changed +52
-3
lines changed
5 files changed +52
-3
lines changed Original file line number Diff line number Diff line change @@ -394,7 +394,6 @@ pub trait F64Ext: private::Sealed + Sized {
394
394
395
395
fn sinh ( self ) -> Self ;
396
396
397
- #[ cfg( todo) ]
398
397
fn cosh ( self ) -> Self ;
399
398
400
399
fn tanh ( self ) -> Self ;
@@ -569,7 +568,6 @@ impl F64Ext for f64 {
569
568
sinh ( self )
570
569
}
571
570
572
- #[ cfg( todo) ]
573
571
#[ inline]
574
572
fn cosh ( self ) -> Self {
575
573
cosh ( self )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ mod ceil;
19
19
mod ceilf;
20
20
mod cos;
21
21
mod cosf;
22
+ mod cosh;
22
23
mod coshf;
23
24
mod exp;
24
25
mod exp2;
@@ -77,6 +78,7 @@ pub use self::ceil::ceil;
77
78
pub use self :: ceilf:: ceilf;
78
79
pub use self :: cos:: cos;
79
80
pub use self :: cosf:: cosf;
81
+ pub use self :: cosh:: cosh;
80
82
pub use self :: coshf:: coshf;
81
83
pub use self :: exp:: exp;
82
84
pub use self :: exp2:: exp2;
@@ -127,6 +129,7 @@ mod expo2;
127
129
mod fenv;
128
130
mod k_cos;
129
131
mod k_cosf;
132
+ mod k_expo2;
130
133
mod k_expo2f;
131
134
mod k_sin;
132
135
mod k_sinf;
@@ -140,6 +143,7 @@ mod rem_pio2f;
140
143
use self :: expo2:: expo2;
141
144
use self :: k_cos:: k_cos;
142
145
use self :: k_cosf:: k_cosf;
146
+ use self :: k_expo2:: k_expo2;
143
147
use self :: k_expo2f:: k_expo2f;
144
148
use self :: k_sin:: k_sin;
145
149
use self :: k_sinf:: k_sinf;
Original file line number Diff line number Diff line change @@ -703,7 +703,7 @@ f64_f64! {
703
703
cbrt,
704
704
ceil,
705
705
cos,
706
- // cosh,
706
+ cosh,
707
707
exp,
708
708
exp2,
709
709
expm1,
You can’t perform that action at this time.
0 commit comments