Skip to content

Commit 28caff6

Browse files
committed
return approximations for max float values in acosh and asinh
1 parent 44e6620 commit 28caff6

5 files changed

Lines changed: 27 additions & 0 deletions

File tree

library/coretests/tests/num/floats.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,8 @@ float_test! {
16991699
assert!(nan.asinh().is_nan());
17001700
assert!(flt(-0.0).asinh().is_sign_negative());
17011701

1702+
assert_ne!(Float::MAX.asinh(), inf);
1703+
17021704
// issue 63271
17031705
assert_approx_eq!(flt(2.0).asinh(), 1.443635475178810342493276740273105, Float::ASINH_APPROX);
17041706
assert_approx_eq!(flt(-2.0).asinh(), -1.443635475178810342493276740273105, Float::ASINH_APPROX);
@@ -1732,6 +1734,7 @@ float_test! {
17321734
assert!(nan.acosh().is_nan());
17331735
assert_approx_eq!(flt(2.0).acosh(), 1.31695789692481670862504634730796844, Float::ACOSH_APPROX);
17341736
assert_approx_eq!(flt(3.0).acosh(), 1.76274717403908605046521864995958461, Float::ACOSH_APPROX);
1737+
assert_ne!(Float::MAX.acosh(), inf);
17351738

17361739
#[allow(overflowing_literals)]
17371740
if Float::MAX > flt(66000.0) {

library/std/src/num/f128.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,10 @@ impl f128 {
868868
#[must_use = "method returns a new number and does not mutate the original value"]
869869
pub fn asinh(self) -> f128 {
870870
let ax = self.abs();
871+
if ax >= (1u128 << f128::MANTISSA_DIGITS / 2) as f128 {
872+
return (ax.ln() + consts::LN_2).copysign(self);
873+
}
874+
871875
let ix = 1.0 / ax;
872876
(ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
873877
}
@@ -902,6 +906,8 @@ impl f128 {
902906
pub fn acosh(self) -> f128 {
903907
if self < 1.0 {
904908
Self::NAN
909+
} else if self >= (1u128 << f128::MANTISSA_DIGITS / 2) as f128 {
910+
self.ln() + consts::LN_2
905911
} else {
906912
(self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
907913
}

library/std/src/num/f16.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,10 @@ impl f16 {
833833
#[must_use = "method returns a new number and does not mutate the original value"]
834834
pub fn asinh(self) -> f16 {
835835
let ax = self.abs();
836+
if ax >= (1u16 << f16::MANTISSA_DIGITS / 2) as f16 {
837+
return (ax.ln() + consts::LN_2).copysign(self);
838+
}
839+
836840
let ix = 1.0 / ax;
837841
(ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
838842
}
@@ -867,6 +871,8 @@ impl f16 {
867871
pub fn acosh(self) -> f16 {
868872
if self < 1.0 {
869873
Self::NAN
874+
} else if self >= (1u16 << f16::MANTISSA_DIGITS / 2) as f16 {
875+
self.ln() + consts::LN_2
870876
} else {
871877
(self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
872878
}

library/std/src/num/f32.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,10 @@ impl f32 {
10921092
#[inline]
10931093
pub fn asinh(self) -> f32 {
10941094
let ax = self.abs();
1095+
if ax >= (1u32 << f32::MANTISSA_DIGITS / 2) as f32 {
1096+
return (ax.ln() + consts::LN_2).copysign(self);
1097+
}
1098+
10951099
let ix = 1.0 / ax;
10961100
(ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
10971101
}
@@ -1121,6 +1125,8 @@ impl f32 {
11211125
pub fn acosh(self) -> f32 {
11221126
if self < 1.0 {
11231127
Self::NAN
1128+
} else if self >= (1u32 << f32::MANTISSA_DIGITS / 2) as f32 {
1129+
self.ln() + consts::LN_2
11241130
} else {
11251131
(self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
11261132
}

library/std/src/num/f64.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,10 @@ impl f64 {
10921092
#[inline]
10931093
pub fn asinh(self) -> f64 {
10941094
let ax = self.abs();
1095+
if ax >= (1u64 << f64::MANTISSA_DIGITS / 2) as f64 {
1096+
return (ax.ln() + consts::LN_2).copysign(self);
1097+
}
1098+
10951099
let ix = 1.0 / ax;
10961100
(ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
10971101
}
@@ -1121,6 +1125,8 @@ impl f64 {
11211125
pub fn acosh(self) -> f64 {
11221126
if self < 1.0 {
11231127
Self::NAN
1128+
} else if self >= (1u64 << f64::MANTISSA_DIGITS / 2) as f64 {
1129+
self.ln() + consts::LN_2
11241130
} else {
11251131
(self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
11261132
}

0 commit comments

Comments
 (0)