From 02a188b1dd31c16278a35667ad5e9303f795c215 Mon Sep 17 00:00:00 2001 From: Abhishek Chanda Date: Mon, 25 May 2015 23:32:41 +0530 Subject: [PATCH] Expose some more functions from libc Closes #864 --- src/libstd/num/f32.rs | 89 +++++++++++++++++++++++++++++++++++++++++++ src/libstd/num/f64.rs | 89 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index e31d97b324038..3fcc8e53e1b75 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -652,6 +652,59 @@ impl f32 { unsafe { cmath::nextafterf(self, other) } } + /// Returns floating point exponent of the input + /// + /// ``` + /// use std::f32; + /// + /// let x = 0.5f32; + /// let abs_diff = ( -1 - x.logb()).abs(); + /// + /// assert!(abs_diff <= f32::EPSILON) + /// ``` + #[unstable(feature = "float_extras", + reason = "unsure about its place in the world")] + #[inline] + pub fn logb(self) -> f32 { + unsafe { cmath::logbf(self) } + } + + /// Returns integer exponent of the input + /// + /// ``` + /// use std::f32; + /// + /// let x = 0.5f32; + /// let abs_diff = (-1.0f32 - x.ilogb()).abs(); + /// + /// assert!(abs_diff <= f32::EPSILON) + /// ``` + #[unstable(feature = "float_extras", + reason = "unsure about its place in the world")] + #[inline] + pub fn ilogb(self) -> i32 { + unsafe { cmath::ilogbf(self) } + } + + /// Returns the value of the gamma function + /// at the given point + /// + /// ``` + /// use std::f32; + /// + /// let x = 1.2f32; + /// let g = 0.9181687f32; + /// let abs_diff = (x.gamma() - g ).abs(); + /// + /// assert!(abs_diff <= f32::EPSILON) + /// ``` + #[unstable(feature = "float_extras", + reason = "unsure about its place in the world")] + #[inline] + pub fn gamma(self) -> f32 { + unsafe { cmath::tgammaf(self) } + } + /// Returns the maximum of the two numbers. /// /// ``` @@ -981,6 +1034,42 @@ impl f32 { unsafe { cmath::coshf(self) } } + /// Error function. + /// + /// ``` + /// use std::f32; + /// + /// let x = 1.0f32; + /// let g = 0.8427008f32; + /// let f = x.erf(); + /// let abs_difference = (f - g).abs(); + /// assert!(abs_difference <= f32::EPSILON); + /// ``` + #[unstable(feature = "float_extras", + reason = "unsure about its place in the world")] + #[inline] + pub fn erf(self) -> f32 { + unsafe { cmath::erff(self) } + } + + /// Complementary error function. + /// + /// ``` + /// use std::f32; + /// + /// let x = 1.0f32; + /// let g = 0.1572992f32; + /// let f = x.erfc(); + /// let abs_difference = (f - g).abs(); + /// assert!(abs_difference <= f32::EPSILON); + /// ``` + #[unstable(feature = "float_extras", + reason = "unsure about its place in the world")] + #[inline] + pub fn erfc(self) -> f32 { + unsafe { cmath::erfcf(self) } + } + /// Hyperbolic tangent function. /// /// ``` diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index e87855ffd4eed..d6b6057d97a56 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -628,6 +628,59 @@ impl f64 { unsafe { cmath::nextafter(self, other) } } + /// Returns floating point exponent of the input + /// + /// ``` + /// use std::f64; + /// + /// let x = 0.5f64; + /// let abs_diff = (x.logb() - 1).abs(); + /// + /// assert!(abs_diff <= f64::EPSILON) + /// ``` + #[unstable(feature = "float_extras", + reason = "unsure about its place in the world")] + #[inline] + pub fn logb(self) -> f64 { + unsafe { cmath::logb(self) } + } + + /// Returns integer exponent of the input + /// + /// ``` + /// use std::f64; + /// + /// let x = 0.5f64; + /// let abs_diff = (x.ilogb() - 1.0f64).abs(); + /// + /// assert!(abs_diff <= f64::EPSILON) + /// ``` + #[unstable(feature = "float_extras", + reason = "unsure about its place in the world")] + #[inline] + pub fn ilogb(self) -> i32 { + unsafe { cmath::ilogb(self) } + } + + /// Returns the value of the gamma function + /// at the given point + /// + /// ``` + /// use std::f64; + /// + /// let x = 1.2f64; + /// let g = 0.9182f64; + /// let abs_diff = (x.gamma() - g ).abs(); + /// + /// assert!(abs_diff <= f64::EPSILON) + /// ``` + #[unstable(feature = "float_extras", + reason = "unsure about its place in the world")] + #[inline] + pub fn gamma(self) -> f64 { + unsafe { cmath::tgamma(self) } + } + /// Returns the maximum of the two numbers. /// /// ``` @@ -677,6 +730,42 @@ impl f64 { unsafe { cmath::fdim(self, other) } } + /// Error function. + /// + /// ``` + /// use std::f64; + /// + /// let x = 1.0f64; + /// let g = 0.8427008f64; + /// let f = x.erf(); + /// let abs_difference = (f - g).abs(); + /// assert!(abs_difference <= f64::EPSILON); + /// ``` + #[unstable(feature = "float_extras", + reason = "unsure about its place in the world")] + #[inline] + pub fn erf(self) -> f64 { + unsafe { cmath::erf(self) } + } + + /// Complementary error function. + /// + /// ``` + /// use std::f64; + /// + /// let x = 1.0f64; + /// let g = 0.1572992f64; + /// let f = x.erfc(); + /// let abs_difference = (f - g).abs(); + /// assert!(abs_difference <= f64::EPSILON); + /// ``` + #[unstable(feature = "float_extras", + reason = "unsure about its place in the world")] + #[inline] + pub fn erfc(self) -> f64 { + unsafe { cmath::erfc(self) } + } + /// Takes the cubic root of a number. /// /// ```