]> git.lizzy.rs Git - rust.git/blobdiff - src/libstd/num/f64.rs
Auto merge of #32141 - jseyfried:fix_resolution_in_lexical_scopes, r=nikomatsakis
[rust.git] / src / libstd / num / f64.rs
index f119b1d9f9ac1537939ddaf738ea494f658f6eb4..446e22a20ad7d576dc8cd18cb75b06cc0a69d92d 100644 (file)
@@ -206,7 +206,7 @@ pub fn classify(self) -> FpCategory { num::Float::classify(self) }
     ///
     /// assert!(abs_difference < 1e-10);
     /// ```
-    /// [floating-point]: ../../../../../reference.html#machine-types
+    /// [floating-point]: ../reference.html#machine-types
     #[unstable(feature = "float_extras", reason = "signature is undecided",
                issue = "27752")]
     #[inline]
@@ -511,28 +511,7 @@ pub fn exp2(self) -> f64 {
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn ln(self) -> f64 {
-        if !cfg!(target_os = "sunos") {
-            unsafe { intrinsics::logf64(self) }
-        } else {
-            // Illumos requires a wrapper around log, log2, and log10 functions
-            // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
-            // of expected NaN).
-            if self.is_finite() {
-                if self > 0.0 {
-                    unsafe { intrinsics::logf64(self) }
-                } else if self == 0.0 {
-                    NEG_INFINITY // log(0) = -Inf
-                } else {
-                    NAN // log(-n) = NaN
-                }
-            } else if self.is_nan() {
-                self // log(NaN) = NaN
-            } else if self > 0.0 {
-                self // log(Inf) = Inf
-            } else {
-                NAN // log(-Inf) = NaN
-            }
-        }
+        self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
     }
 
     /// Returns the logarithm of the number with respect to an arbitrary base.
@@ -567,27 +546,7 @@ pub fn log(self, base: f64) -> f64 { self.ln() / base.ln() }
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn log2(self) -> f64 {
-        if !cfg!(target_os = "sunos") {
-            unsafe { intrinsics::log2f64(self) }
-        } else {
-            // Illumos requires a wrapper around the log2 function because of
-            // its non-standard behavior
-            if self.is_finite() {
-                if self > 0.0 {
-                    unsafe { intrinsics::log2f64(self) }
-                } else if self == 0.0 {
-                    NEG_INFINITY // log2(0) = -Inf
-                } else {
-                    NAN // log2(-n) = NaN
-                }
-            } else if self.is_nan() {
-                self // log2(NaN) = NaN
-            } else if self > 0.0 {
-                self // log2(Inf) = Inf
-            } else {
-                NAN // log2(-Inf) = NaN
-            }
-        }
+        self.log_wrapper(|n| { unsafe { intrinsics::log2f64(n) } })
     }
 
     /// Returns the base 10 logarithm of the number.
@@ -603,27 +562,7 @@ pub fn log2(self) -> f64 {
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn log10(self) -> f64 {
-        if !cfg!(target_os = "sunos") {
-            unsafe { intrinsics::log10f64(self) }
-        } else {
-            // Illumos requires a wrapper around the log10 function because of
-            // its non-standard behavior.
-            if self.is_finite() {
-                if self > 0.0 {
-                    unsafe { intrinsics::log10f64(self) }
-                } else if self == 0.0 {
-                    NEG_INFINITY // log10(0) = -Inf
-                } else {
-                    NAN // log10(-n) = NaN
-                }
-            } else if self.is_nan() {
-                self // log10(NaN) = NaN
-            } else if self > 0.0 {
-                self // log10(Inf) = Inf
-            } else {
-                NAN // log10(-Inf) = NaN
-            }
-        }
+        self.log_wrapper(|n| { unsafe { intrinsics::log10f64(n) } })
     }
 
     /// Converts radians to degrees.
@@ -1126,6 +1065,31 @@ pub fn acosh(self) -> f64 {
     pub fn atanh(self) -> f64 {
         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
     }
+
+    // Solaris/Illumos requires a wrapper around log, log2, and log10 functions
+    // because of their non-standard behavior (e.g. log(-n) returns -Inf instead
+    // of expected NaN).
+    fn log_wrapper<F: Fn(f64) -> f64>(self, log_fn: F) -> f64 {
+        if !cfg!(target_os = "solaris") {
+            log_fn(self)
+        } else {
+            if self.is_finite() {
+                if self > 0.0 {
+                    log_fn(self)
+                } else if self == 0.0 {
+                    NEG_INFINITY // log(0) = -Inf
+                } else {
+                    NAN // log(-n) = NaN
+                }
+            } else if self.is_nan() {
+                self // log(NaN) = NaN
+            } else if self > 0.0 {
+                self // log(Inf) = Inf
+            } else {
+                NAN // log(-Inf) = NaN
+            }
+        }
+    }
 }
 
 #[cfg(test)]