Skip to content

Commit 0e82565

Browse files
committed
fix weird log
1 parent 5309135 commit 0e82565

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

library/std/src/f64.rs

+3-27
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ impl f64 {
426426
#[stable(feature = "rust1", since = "1.0.0")]
427427
#[inline]
428428
pub fn ln(self) -> f64 {
429-
self.log_wrapper(|n| unsafe { intrinsics::logf64(n) })
429+
crate::sys::log_wrapper(self, |n| unsafe { intrinsics::logf64(n) })
430430
}
431431

432432
/// Returns the logarithm of the number with respect to an arbitrary base.
@@ -470,7 +470,7 @@ impl f64 {
470470
#[stable(feature = "rust1", since = "1.0.0")]
471471
#[inline]
472472
pub fn log2(self) -> f64 {
473-
self.log_wrapper(crate::sys::log2f64)
473+
crate::sys::log_wrapper(self, crate::sys::log2f64)
474474
}
475475

476476
/// Returns the base 10 logarithm of the number.
@@ -490,7 +490,7 @@ impl f64 {
490490
#[stable(feature = "rust1", since = "1.0.0")]
491491
#[inline]
492492
pub fn log10(self) -> f64 {
493-
self.log_wrapper(|n| unsafe { intrinsics::log10f64(n) })
493+
crate::sys::log_wrapper(self, |n| unsafe { intrinsics::log10f64(n) })
494494
}
495495

496496
/// The positive difference of two numbers.
@@ -925,28 +925,4 @@ impl f64 {
925925
pub fn atanh(self) -> f64 {
926926
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
927927
}
928-
929-
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
930-
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
931-
// of expected NaN).
932-
#[rustc_allow_incoherent_impl]
933-
fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
934-
if let Some(result) = crate::sys::log_wrapper(self) {
935-
log_fn(result)
936-
} else if self.is_finite() {
937-
if self > 0.0 {
938-
log_fn(self)
939-
} else if self == 0.0 {
940-
Self::NEG_INFINITY // log(0) = -Inf
941-
} else {
942-
Self::NAN // log(-n) = NaN
943-
}
944-
} else if self.is_nan() {
945-
self // log(NaN) = NaN
946-
} else if self > 0.0 {
947-
self // log(Inf) = Inf
948-
} else {
949-
Self::NAN // log(-Inf) = NaN
950-
}
951-
}
952928
}

library/std/src/sys/mod.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,32 @@ cfg_if::cfg_if! {
9393
}
9494
}
9595

96+
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
97+
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
98+
// of expected NaN).
9699
#[cfg(not(test))]
97100
cfg_if::cfg_if! {
98101
if #[cfg(any(target_os = "solaris", target_os = "illumos"))] {
99-
pub fn log_wrapper(n: f64) -> Option<f64> {
100-
Some(n)
102+
pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
103+
if n.is_finite() {
104+
if n > 0.0 {
105+
log_fn(n)
106+
} else if n == 0.0 {
107+
f64::NEG_INFINITY // log(0) = -Inf
108+
} else {
109+
f64::NAN // log(-n) = NaN
110+
}
111+
} else if n.is_nan() {
112+
n // log(NaN) = NaN
113+
} else if n > 0.0 {
114+
n // log(Inf) = Inf
115+
} else {
116+
f64::NAN // log(-Inf) = NaN
117+
}
101118
}
102119
} else {
103-
pub fn log_wrapper(_n: f64) -> Option<f64> {
104-
None
120+
pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
121+
log_fn(n)
105122
}
106123
}
107124
}

0 commit comments

Comments
 (0)