Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions library/coretests/tests/num/floats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1699,6 +1699,8 @@ float_test! {
assert!(nan.asinh().is_nan());
assert!(flt(-0.0).asinh().is_sign_negative());

assert_ne!(Float::MAX.asinh(), inf);

// issue 63271
assert_approx_eq!(flt(2.0).asinh(), 1.443635475178810342493276740273105, Float::ASINH_APPROX);
assert_approx_eq!(flt(-2.0).asinh(), -1.443635475178810342493276740273105, Float::ASINH_APPROX);
Expand Down Expand Up @@ -1732,6 +1734,7 @@ float_test! {
assert!(nan.acosh().is_nan());
assert_approx_eq!(flt(2.0).acosh(), 1.31695789692481670862504634730796844, Float::ACOSH_APPROX);
assert_approx_eq!(flt(3.0).acosh(), 1.76274717403908605046521864995958461, Float::ACOSH_APPROX);
assert_ne!(Float::MAX.acosh(), inf);

#[allow(overflowing_literals)]
if Float::MAX > flt(66000.0) {
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/num/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,10 @@ impl f128 {
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn asinh(self) -> f128 {
let ax = self.abs();
if ax >= (1u128 << f128::MANTISSA_DIGITS / 2) as f128 {
return (ax.ln() + consts::LN_2).copysign(self);
}

let ix = 1.0 / ax;
(ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
}
Expand Down Expand Up @@ -902,6 +906,8 @@ impl f128 {
pub fn acosh(self) -> f128 {
if self < 1.0 {
Self::NAN
} else if self >= (1u128 << f128::MANTISSA_DIGITS / 2) as f128 {
self.ln() + consts::LN_2
} else {
(self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
}
Expand Down
10 changes: 2 additions & 8 deletions library/std/src/num/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,9 +832,7 @@ impl f16 {
#[unstable(feature = "f16", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn asinh(self) -> f16 {
let ax = self.abs();
let ix = 1.0 / ax;
(ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
cmath::asinhf(self as f32) as f16
}

/// Inverse hyperbolic cosine function.
Expand Down Expand Up @@ -865,11 +863,7 @@ impl f16 {
#[unstable(feature = "f16", issue = "116909")]
#[must_use = "method returns a new number and does not mutate the original value"]
pub fn acosh(self) -> f16 {
if self < 1.0 {
Self::NAN
} else {
(self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
}
cmath::acoshf(self as f32) as f16
}

/// Inverse hyperbolic tangent function.
Expand Down
10 changes: 2 additions & 8 deletions library/std/src/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,9 +1091,7 @@ impl f32 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn asinh(self) -> f32 {
let ax = self.abs();
let ix = 1.0 / ax;
(ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
cmath::asinhf(self)
}

/// Inverse hyperbolic cosine function.
Expand All @@ -1119,11 +1117,7 @@ impl f32 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn acosh(self) -> f32 {
if self < 1.0 {
Self::NAN
} else {
(self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
}
cmath::acoshf(self)
}

/// Inverse hyperbolic tangent function.
Expand Down
10 changes: 2 additions & 8 deletions library/std/src/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,9 +1091,7 @@ impl f64 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn asinh(self) -> f64 {
let ax = self.abs();
let ix = 1.0 / ax;
(ax + (ax / (Self::hypot(1.0, ix) + ix))).ln_1p().copysign(self)
cmath::asinh(self)
}

/// Inverse hyperbolic cosine function.
Expand All @@ -1119,11 +1117,7 @@ impl f64 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn acosh(self) -> f64 {
if self < 1.0 {
Self::NAN
} else {
(self + ((self - 1.0).sqrt() * (self + 1.0).sqrt())).ln()
}
cmath::acosh(self)
}

/// Inverse hyperbolic tangent function.
Expand Down
14 changes: 14 additions & 0 deletions library/std/src/sys/cmath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
// or by `compiler-builtins` on unsupported platforms.
unsafe extern "C" {
pub safe fn acos(n: f64) -> f64;
pub safe fn acosh(n: f64) -> f64;
pub safe fn asin(n: f64) -> f64;
pub safe fn asinh(n: f64) -> f64;
pub safe fn atan(n: f64) -> f64;
pub safe fn atan2(a: f64, b: f64) -> f64;
pub safe fn cosh(n: f64) -> f64;
Expand Down Expand Up @@ -57,6 +59,16 @@ cfg_select! {
f64::acos(n as f64) as f32
}

#[inline]
pub fn acoshf(n: f32) -> f32 {
f64::acosh(n as f64) as f32
}

#[inline]
pub fn asinhf(n: f32) -> f32 {
f64::asinh(n as f64) as f32
}

#[inline]
pub fn asinf(n: f32) -> f32 {
f64::asin(n as f64) as f32
Expand Down Expand Up @@ -95,7 +107,9 @@ cfg_select! {
_ => {
unsafe extern "C" {
pub safe fn acosf(n: f32) -> f32;
pub safe fn acoshf(n: f32) -> f32;
pub safe fn asinf(n: f32) -> f32;
pub safe fn asinhf(n: f32) -> f32;
pub safe fn atan2f(a: f32, b: f32) -> f32;
pub safe fn atanf(n: f32) -> f32;
pub safe fn coshf(n: f32) -> f32;
Expand Down
Loading