Skip to content

Commit 02a188b

Browse files
committed
Expose some more functions from libc
Closes #864
1 parent 38c943f commit 02a188b

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

src/libstd/num/f32.rs

+89
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,59 @@ impl f32 {
652652
unsafe { cmath::nextafterf(self, other) }
653653
}
654654

655+
/// Returns floating point exponent of the input
656+
///
657+
/// ```
658+
/// use std::f32;
659+
///
660+
/// let x = 0.5f32;
661+
/// let abs_diff = ( -1 - x.logb()).abs();
662+
///
663+
/// assert!(abs_diff <= f32::EPSILON)
664+
/// ```
665+
#[unstable(feature = "float_extras",
666+
reason = "unsure about its place in the world")]
667+
#[inline]
668+
pub fn logb(self) -> f32 {
669+
unsafe { cmath::logbf(self) }
670+
}
671+
672+
/// Returns integer exponent of the input
673+
///
674+
/// ```
675+
/// use std::f32;
676+
///
677+
/// let x = 0.5f32;
678+
/// let abs_diff = (-1.0f32 - x.ilogb()).abs();
679+
///
680+
/// assert!(abs_diff <= f32::EPSILON)
681+
/// ```
682+
#[unstable(feature = "float_extras",
683+
reason = "unsure about its place in the world")]
684+
#[inline]
685+
pub fn ilogb(self) -> i32 {
686+
unsafe { cmath::ilogbf(self) }
687+
}
688+
689+
/// Returns the value of the gamma function
690+
/// at the given point
691+
///
692+
/// ```
693+
/// use std::f32;
694+
///
695+
/// let x = 1.2f32;
696+
/// let g = 0.9181687f32;
697+
/// let abs_diff = (x.gamma() - g ).abs();
698+
///
699+
/// assert!(abs_diff <= f32::EPSILON)
700+
/// ```
701+
#[unstable(feature = "float_extras",
702+
reason = "unsure about its place in the world")]
703+
#[inline]
704+
pub fn gamma(self) -> f32 {
705+
unsafe { cmath::tgammaf(self) }
706+
}
707+
655708
/// Returns the maximum of the two numbers.
656709
///
657710
/// ```
@@ -981,6 +1034,42 @@ impl f32 {
9811034
unsafe { cmath::coshf(self) }
9821035
}
9831036

1037+
/// Error function.
1038+
///
1039+
/// ```
1040+
/// use std::f32;
1041+
///
1042+
/// let x = 1.0f32;
1043+
/// let g = 0.8427008f32;
1044+
/// let f = x.erf();
1045+
/// let abs_difference = (f - g).abs();
1046+
/// assert!(abs_difference <= f32::EPSILON);
1047+
/// ```
1048+
#[unstable(feature = "float_extras",
1049+
reason = "unsure about its place in the world")]
1050+
#[inline]
1051+
pub fn erf(self) -> f32 {
1052+
unsafe { cmath::erff(self) }
1053+
}
1054+
1055+
/// Complementary error function.
1056+
///
1057+
/// ```
1058+
/// use std::f32;
1059+
///
1060+
/// let x = 1.0f32;
1061+
/// let g = 0.1572992f32;
1062+
/// let f = x.erfc();
1063+
/// let abs_difference = (f - g).abs();
1064+
/// assert!(abs_difference <= f32::EPSILON);
1065+
/// ```
1066+
#[unstable(feature = "float_extras",
1067+
reason = "unsure about its place in the world")]
1068+
#[inline]
1069+
pub fn erfc(self) -> f32 {
1070+
unsafe { cmath::erfcf(self) }
1071+
}
1072+
9841073
/// Hyperbolic tangent function.
9851074
///
9861075
/// ```

src/libstd/num/f64.rs

+89
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,59 @@ impl f64 {
628628
unsafe { cmath::nextafter(self, other) }
629629
}
630630

631+
/// Returns floating point exponent of the input
632+
///
633+
/// ```
634+
/// use std::f64;
635+
///
636+
/// let x = 0.5f64;
637+
/// let abs_diff = (x.logb() - 1).abs();
638+
///
639+
/// assert!(abs_diff <= f64::EPSILON)
640+
/// ```
641+
#[unstable(feature = "float_extras",
642+
reason = "unsure about its place in the world")]
643+
#[inline]
644+
pub fn logb(self) -> f64 {
645+
unsafe { cmath::logb(self) }
646+
}
647+
648+
/// Returns integer exponent of the input
649+
///
650+
/// ```
651+
/// use std::f64;
652+
///
653+
/// let x = 0.5f64;
654+
/// let abs_diff = (x.ilogb() - 1.0f64).abs();
655+
///
656+
/// assert!(abs_diff <= f64::EPSILON)
657+
/// ```
658+
#[unstable(feature = "float_extras",
659+
reason = "unsure about its place in the world")]
660+
#[inline]
661+
pub fn ilogb(self) -> i32 {
662+
unsafe { cmath::ilogb(self) }
663+
}
664+
665+
/// Returns the value of the gamma function
666+
/// at the given point
667+
///
668+
/// ```
669+
/// use std::f64;
670+
///
671+
/// let x = 1.2f64;
672+
/// let g = 0.9182f64;
673+
/// let abs_diff = (x.gamma() - g ).abs();
674+
///
675+
/// assert!(abs_diff <= f64::EPSILON)
676+
/// ```
677+
#[unstable(feature = "float_extras",
678+
reason = "unsure about its place in the world")]
679+
#[inline]
680+
pub fn gamma(self) -> f64 {
681+
unsafe { cmath::tgamma(self) }
682+
}
683+
631684
/// Returns the maximum of the two numbers.
632685
///
633686
/// ```
@@ -677,6 +730,42 @@ impl f64 {
677730
unsafe { cmath::fdim(self, other) }
678731
}
679732

733+
/// Error function.
734+
///
735+
/// ```
736+
/// use std::f64;
737+
///
738+
/// let x = 1.0f64;
739+
/// let g = 0.8427008f64;
740+
/// let f = x.erf();
741+
/// let abs_difference = (f - g).abs();
742+
/// assert!(abs_difference <= f64::EPSILON);
743+
/// ```
744+
#[unstable(feature = "float_extras",
745+
reason = "unsure about its place in the world")]
746+
#[inline]
747+
pub fn erf(self) -> f64 {
748+
unsafe { cmath::erf(self) }
749+
}
750+
751+
/// Complementary error function.
752+
///
753+
/// ```
754+
/// use std::f64;
755+
///
756+
/// let x = 1.0f64;
757+
/// let g = 0.1572992f64;
758+
/// let f = x.erfc();
759+
/// let abs_difference = (f - g).abs();
760+
/// assert!(abs_difference <= f64::EPSILON);
761+
/// ```
762+
#[unstable(feature = "float_extras",
763+
reason = "unsure about its place in the world")]
764+
#[inline]
765+
pub fn erfc(self) -> f64 {
766+
unsafe { cmath::erfc(self) }
767+
}
768+
680769
/// Takes the cubic root of a number.
681770
///
682771
/// ```

0 commit comments

Comments
 (0)