Skip to content

Commit c8a352e

Browse files
authored
Rollup merge of rust-lang#63698 - Phosphorus15:master, r=nagisa
Fixed floating point issue with asinh function This should fixes rust-lang#63271 , in which `asinh(-0.0)` returns `0.0` instead of `-0.0`. according to @nagisa > > > IEEE-754 (2008), section 9.2.1: > > > For the functions expm1, exp2m1, exp10m1, logp1, log2p1, log10p1, sin, tan, sinPi, atanPi, asin, atan, sinh, tanh, asinh, and atanh, f(+0) is +0 and f(−0) is −0 with no exception. > > and > > > sinh(±∞) and asinh(±∞) are ±∞ with no exception. After ensuring that the function `asinh` is the only function affected (functions like `sin`, `sinh` are all based on `cmath` library or `llvm` intrinsics), and that `atanh` always gives the correct result. The only function to modify is `asinh`.
2 parents 6aa4ae9 + e33d870 commit c8a352e

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/libstd/f32.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ impl f32 {
910910
if self == NEG_INFINITY {
911911
NEG_INFINITY
912912
} else {
913-
(self + ((self * self) + 1.0).sqrt()).ln()
913+
(self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
914914
}
915915
}
916916

@@ -931,9 +931,10 @@ impl f32 {
931931
#[stable(feature = "rust1", since = "1.0.0")]
932932
#[inline]
933933
pub fn acosh(self) -> f32 {
934-
match self {
935-
x if x < 1.0 => crate::f32::NAN,
936-
x => (x + ((x * x) - 1.0).sqrt()).ln(),
934+
if self < 1.0 {
935+
crate::f32::NAN
936+
} else {
937+
(self + ((self * self) - 1.0).sqrt()).ln()
937938
}
938939
}
939940

@@ -1487,6 +1488,7 @@ mod tests {
14871488
assert_eq!(inf.asinh(), inf);
14881489
assert_eq!(neg_inf.asinh(), neg_inf);
14891490
assert!(nan.asinh().is_nan());
1491+
assert!((-0.0f32).asinh().is_sign_negative()); // issue 63271
14901492
assert_approx_eq!(2.0f32.asinh(), 1.443635475178810342493276740273105f32);
14911493
assert_approx_eq!((-2.0f32).asinh(), -1.443635475178810342493276740273105f32);
14921494
}

src/libstd/f64.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl f64 {
244244
pub fn div_euclid(self, rhs: f64) -> f64 {
245245
let q = (self / rhs).trunc();
246246
if self % rhs < 0.0 {
247-
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 }
247+
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
248248
}
249249
q
250250
}
@@ -437,9 +437,9 @@ impl f64 {
437437
pub fn log2(self) -> f64 {
438438
self.log_wrapper(|n| {
439439
#[cfg(target_os = "android")]
440-
return crate::sys::android::log2f64(n);
440+
return crate::sys::android::log2f64(n);
441441
#[cfg(not(target_os = "android"))]
442-
return unsafe { intrinsics::log2f64(n) };
442+
return unsafe { intrinsics::log2f64(n) };
443443
})
444444
}
445445

@@ -481,16 +481,16 @@ impl f64 {
481481
#[stable(feature = "rust1", since = "1.0.0")]
482482
#[inline]
483483
#[rustc_deprecated(since = "1.10.0",
484-
reason = "you probably meant `(self - other).abs()`: \
484+
reason = "you probably meant `(self - other).abs()`: \
485485
this operation is `(self - other).max(0.0)` \
486486
except that `abs_sub` also propagates NaNs (also \
487487
known as `fdim` in C). If you truly need the positive \
488488
difference, consider using that expression or the C function \
489489
`fdim`, depending on how you wish to handle NaN (please consider \
490490
filing an issue describing your use-case too).")]
491-
pub fn abs_sub(self, other: f64) -> f64 {
492-
unsafe { cmath::fdim(self, other) }
493-
}
491+
pub fn abs_sub(self, other: f64) -> f64 {
492+
unsafe { cmath::fdim(self, other) }
493+
}
494494

495495
/// Takes the cubic root of a number.
496496
///
@@ -833,7 +833,7 @@ impl f64 {
833833
if self == NEG_INFINITY {
834834
NEG_INFINITY
835835
} else {
836-
(self + ((self * self) + 1.0).sqrt()).ln()
836+
(self + ((self * self) + 1.0).sqrt()).ln().copysign(self)
837837
}
838838
}
839839

@@ -852,9 +852,10 @@ impl f64 {
852852
#[stable(feature = "rust1", since = "1.0.0")]
853853
#[inline]
854854
pub fn acosh(self) -> f64 {
855-
match self {
856-
x if x < 1.0 => NAN,
857-
x => (x + ((x * x) - 1.0).sqrt()).ln(),
855+
if self < 1.0 {
856+
NAN
857+
} else {
858+
(self + ((self * self) - 1.0).sqrt()).ln()
858859
}
859860
}
860861

@@ -1187,7 +1188,7 @@ mod tests {
11871188
assert_eq!((-0f64).abs(), 0f64);
11881189
assert_eq!((-1f64).abs(), 1f64);
11891190
assert_eq!(NEG_INFINITY.abs(), INFINITY);
1190-
assert_eq!((1f64/NEG_INFINITY).abs(), 0f64);
1191+
assert_eq!((1f64 / NEG_INFINITY).abs(), 0f64);
11911192
assert!(NAN.abs().is_nan());
11921193
}
11931194

@@ -1199,7 +1200,7 @@ mod tests {
11991200
assert_eq!((-0f64).signum(), -1f64);
12001201
assert_eq!((-1f64).signum(), -1f64);
12011202
assert_eq!(NEG_INFINITY.signum(), -1f64);
1202-
assert_eq!((1f64/NEG_INFINITY).signum(), -1f64);
1203+
assert_eq!((1f64 / NEG_INFINITY).signum(), -1f64);
12031204
assert!(NAN.signum().is_nan());
12041205
}
12051206

@@ -1211,7 +1212,7 @@ mod tests {
12111212
assert!(!(-0f64).is_sign_positive());
12121213
assert!(!(-1f64).is_sign_positive());
12131214
assert!(!NEG_INFINITY.is_sign_positive());
1214-
assert!(!(1f64/NEG_INFINITY).is_sign_positive());
1215+
assert!(!(1f64 / NEG_INFINITY).is_sign_positive());
12151216
assert!(NAN.is_sign_positive());
12161217
assert!(!(-NAN).is_sign_positive());
12171218
}
@@ -1224,7 +1225,7 @@ mod tests {
12241225
assert!((-0f64).is_sign_negative());
12251226
assert!((-1f64).is_sign_negative());
12261227
assert!(NEG_INFINITY.is_sign_negative());
1227-
assert!((1f64/NEG_INFINITY).is_sign_negative());
1228+
assert!((1f64 / NEG_INFINITY).is_sign_negative());
12281229
assert!(!NAN.is_sign_negative());
12291230
assert!((-NAN).is_sign_negative());
12301231
}
@@ -1433,6 +1434,8 @@ mod tests {
14331434
assert_eq!(inf.asinh(), inf);
14341435
assert_eq!(neg_inf.asinh(), neg_inf);
14351436
assert!(nan.asinh().is_nan());
1437+
assert!((-0.0f64).asinh().is_sign_negative());
1438+
// issue 63271
14361439
assert_approx_eq!(2.0f64.asinh(), 1.443635475178810342493276740273105f64);
14371440
assert_approx_eq!((-2.0f64).asinh(), -1.443635475178810342493276740273105f64);
14381441
}

0 commit comments

Comments
 (0)