about summary refs log tree commit diff
path: root/src/libstd/num
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd/num')
-rw-r--r--src/libstd/num/f64.rs92
1 files changed, 28 insertions, 64 deletions
diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs
index f119b1d9f9a..ed047caa0a3 100644
--- a/src/libstd/num/f64.rs
+++ b/src/libstd/num/f64.rs
@@ -511,28 +511,7 @@ impl 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 @@ impl f64 {
     #[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 @@ impl 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 @@ impl f64 {
     pub fn atanh(self) -> f64 {
         0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
     }
+
+    // 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 = "sunos") {
+            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)]