Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed floating point issue with asinh function #63698

Merged
merged 5 commits into from
Aug 27, 2019
Merged
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
10 changes: 6 additions & 4 deletions src/libstd/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ impl f32 {
if self == NEG_INFINITY {
NEG_INFINITY
} else {
(self + ((self * self) + 1.0).sqrt()).ln()
(self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
}
}

Expand All @@ -932,9 +932,10 @@ impl f32 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn acosh(self) -> f32 {
match self {
x if x < 1.0 => crate::f32::NAN,
x => (x + ((x * x) - 1.0).sqrt()).ln(),
if self < 1.0 {
crate::f32::NAN
} else {
(self + ((self * self) - 1.0).sqrt()).ln()
}
}

Expand Down Expand Up @@ -1488,6 +1489,7 @@ mod tests {
assert_eq!(inf.asinh(), inf);
assert_eq!(neg_inf.asinh(), neg_inf);
assert!(nan.asinh().is_nan());
assert!((-0.0f32).asinh().is_sign_negative()); // issue 63271
assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
}
Expand Down
33 changes: 18 additions & 15 deletions src/libstd/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl f64 {
pub fn div_euclid(self, rhs: f64) -> f64 {
let q = (self / rhs).trunc();
if self % rhs < 0.0 {
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
}
q
}
Expand Down Expand Up @@ -437,9 +437,9 @@ impl f64 {
pub fn log2(self) -> f64 {
self.log_wrapper(|n| {
#[cfg(target_os = "android")]
return crate::sys::android::log2f64(n);
return crate::sys::android::log2f64(n);
#[cfg(not(target_os = "android"))]
return unsafe { intrinsics::log2f64(n) };
return unsafe { intrinsics::log2f64(n) };
})
}

Expand Down Expand Up @@ -481,16 +481,16 @@ impl f64 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
#[rustc_deprecated(since = "1.10.0",
reason = "you probably meant `(self - other).abs()`: \
reason = "you probably meant `(self - other).abs()`: \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for changing the identation 😢 , it might because of my IDE's formatter.

this operation is `(self - other).max(0.0)` \
except that `abs_sub` also propagates NaNs (also \
known as `fdim` in C). If you truly need the positive \
difference, consider using that expression or the C function \
`fdim`, depending on how you wish to handle NaN (please consider \
filing an issue describing your use-case too).")]
pub fn abs_sub(self, other: f64) -> f64 {
unsafe { cmath::fdim(self, other) }
}
pub fn abs_sub(self, other: f64) -> f64 {
unsafe { cmath::fdim(self, other) }
}

/// Takes the cubic root of a number.
///
Expand Down Expand Up @@ -834,7 +834,7 @@ impl f64 {
if self == NEG_INFINITY {
NEG_INFINITY
} else {
(self + ((self * self) + 1.0).sqrt()).ln()
(self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
}
}

Expand All @@ -853,9 +853,10 @@ impl f64 {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn acosh(self) -> f64 {
match self {
x if x < 1.0 => NAN,
x => (x + ((x * x) - 1.0).sqrt()).ln(),
if self < 1.0 {
NAN
} else {
(self + ((self * self) - 1.0).sqrt()).ln()
}
}

Expand Down Expand Up @@ -1188,7 +1189,7 @@ mod tests {
assert_eq!((-0f64).abs(), 0f64);
assert_eq!((-1f64).abs(), 1f64);
assert_eq!(NEG_INFINITY.abs(), INFINITY);
assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
assert_eq!((1f64 / NEG_INFINITY).abs(), 0f64);
assert!(NAN.abs().is_nan());
}

Expand All @@ -1200,7 +1201,7 @@ mod tests {
assert_eq!((-0f64).signum(), -1f64);
assert_eq!((-1f64).signum(), -1f64);
assert_eq!(NEG_INFINITY.signum(), -1f64);
assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
assert_eq!((1f64 / NEG_INFINITY).signum(), -1f64);
assert!(NAN.signum().is_nan());
}

Expand All @@ -1212,7 +1213,7 @@ mod tests {
assert!(!(-0f64).is_sign_positive());
assert!(!(-1f64).is_sign_positive());
assert!(!NEG_INFINITY.is_sign_positive());
assert!(!(1f64/NEG_INFINITY).is_sign_positive());
assert!(!(1f64 / NEG_INFINITY).is_sign_positive());
assert!(NAN.is_sign_positive());
assert!(!(-NAN).is_sign_positive());
}
Expand All @@ -1225,7 +1226,7 @@ mod tests {
assert!((-0f64).is_sign_negative());
assert!((-1f64).is_sign_negative());
assert!(NEG_INFINITY.is_sign_negative());
assert!((1f64/NEG_INFINITY).is_sign_negative());
assert!((1f64 / NEG_INFINITY).is_sign_negative());
assert!(!NAN.is_sign_negative());
assert!((-NAN).is_sign_negative());
}
Expand Down Expand Up @@ -1434,6 +1435,8 @@ mod tests {
assert_eq!(inf.asinh(), inf);
assert_eq!(neg_inf.asinh(), neg_inf);
assert!(nan.asinh().is_nan());
assert!((-0.0f64).asinh().is_sign_negative());
// issue 63271
assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
}
Expand Down