File tree 7 files changed +75
-0
lines changed
7 files changed +75
-0
lines changed Original file line number Diff line number Diff line change @@ -920,4 +920,24 @@ impl f32 {
920
920
pub fn atanh ( self ) -> f32 {
921
921
0.5 * ( ( 2.0 * self ) / ( 1.0 - self ) ) . ln_1p ( )
922
922
}
923
+
924
+ /// Gamma function.
925
+ ///
926
+ /// # Examples
927
+ ///
928
+ /// ```
929
+ /// #![feature(float_gamma)]
930
+ /// let x = 5.0f32;
931
+ ///
932
+ /// let abs_difference = (x.gamma() - 24.0).abs();
933
+ ///
934
+ /// assert!(abs_difference <= f32::EPSILON);
935
+ /// ```
936
+ #[ rustc_allow_incoherent_impl]
937
+ #[ must_use = "method returns a new number and does not mutate the original value" ]
938
+ #[ unstable( feature = "float_gamma" , issue = "99842" ) ]
939
+ #[ inline]
940
+ pub fn gamma ( self ) -> f32 {
941
+ unsafe { cmath:: tgammaf ( self ) }
942
+ }
923
943
}
Original file line number Diff line number Diff line change @@ -550,6 +550,21 @@ fn test_atanh() {
550
550
assert_approx_eq ! ( ( -0.5f32 ) . atanh( ) , -0.54930614433405484569762261846126285f32 ) ;
551
551
}
552
552
553
+ #[ test]
554
+ fn test_gamma ( ) {
555
+ assert_eq ! ( 0.0f32 . gamma( ) , f32 :: INFINITY ) ;
556
+ assert_approx_eq ! ( 1.0f32 . gamma( ) , 1.0f32 ) ;
557
+ assert_approx_eq ! ( 2.0f32 . gamma( ) , 1.0f32 ) ;
558
+ assert_approx_eq ! ( 3.0f32 . gamma( ) , 2.0f32 ) ;
559
+ assert_approx_eq ! ( 4.0f32 . gamma( ) , 6.0f32 ) ;
560
+ assert_approx_eq ! ( 5.0f32 . gamma( ) , 24.0f32 ) ;
561
+
562
+ assert_approx_eq ! ( 0.5f32 . gamma( ) , consts:: PI . sqrt( ) ) ;
563
+ assert_approx_eq ! ( ( -0.5f32 ) . gamma( ) , -2.0 * consts:: PI . sqrt( ) ) ;
564
+
565
+ assert ! ( ( -1.0f32 ) . gamma( ) . is_nan( ) ) ;
566
+ }
567
+
553
568
#[ test]
554
569
fn test_real_consts ( ) {
555
570
use super :: consts;
Original file line number Diff line number Diff line change @@ -946,4 +946,24 @@ impl f64 {
946
946
Self :: NAN // log(-Inf) = NaN
947
947
}
948
948
}
949
+
950
+ /// Gamma function.
951
+ ///
952
+ /// # Examples
953
+ ///
954
+ /// ```
955
+ /// #![feature(float_gamma)]
956
+ /// let x = 5.0f64;
957
+ ///
958
+ /// let abs_difference = (x.gamma() - 24.0).abs();
959
+ ///
960
+ /// assert!(abs_difference <= f64::EPSILON);
961
+ /// ```
962
+ #[ rustc_allow_incoherent_impl]
963
+ #[ must_use = "method returns a new number and does not mutate the original value" ]
964
+ #[ unstable( feature = "float_gamma" , issue = "99842" ) ]
965
+ #[ inline]
966
+ pub fn gamma ( self ) -> f64 {
967
+ unsafe { cmath:: tgamma ( self ) }
968
+ }
949
969
}
Original file line number Diff line number Diff line change @@ -535,6 +535,21 @@ fn test_atanh() {
535
535
assert_approx_eq ! ( ( -0.5f64 ) . atanh( ) , -0.54930614433405484569762261846126285f64 ) ;
536
536
}
537
537
538
+ #[ test]
539
+ fn test_gamma ( ) {
540
+ assert_eq ! ( 0.0f64 . gamma( ) , f64 :: INFINITY ) ;
541
+ assert_approx_eq ! ( 1.0f64 . gamma( ) , 1.0f64 ) ;
542
+ assert_approx_eq ! ( 2.0f64 . gamma( ) , 1.0f64 ) ;
543
+ assert_approx_eq ! ( 3.0f64 . gamma( ) , 2.0f64 ) ;
544
+ assert_approx_eq ! ( 4.0f64 . gamma( ) , 6.0f64 ) ;
545
+ assert_approx_eq ! ( 5.0f64 . gamma( ) , 24.0f64 ) ;
546
+
547
+ assert_approx_eq ! ( 0.5f64 . gamma( ) , consts:: PI . sqrt( ) ) ;
548
+ assert_approx_eq ! ( ( -0.5f64 ) . gamma( ) , -2.0 * consts:: PI . sqrt( ) ) ;
549
+
550
+ assert ! ( ( -1.0f64 ) . gamma( ) . is_nan( ) ) ;
551
+ }
552
+
538
553
#[ test]
539
554
fn test_real_consts ( ) {
540
555
use super :: consts;
Original file line number Diff line number Diff line change 272
272
#![ feature( exact_size_is_empty) ]
273
273
#![ feature( exclusive_wrapper) ]
274
274
#![ feature( extend_one) ]
275
+ #![ feature( float_gamma) ]
275
276
#![ feature( float_minimum_maximum) ]
276
277
#![ feature( hasher_prefixfree_extras) ]
277
278
#![ feature( hashmap_internals) ]
Original file line number Diff line number Diff line change @@ -30,4 +30,6 @@ extern "C" {
30
30
pub fn tanf ( n : f32 ) -> f32 ;
31
31
pub fn tanh ( n : f64 ) -> f64 ;
32
32
pub fn tanhf ( n : f32 ) -> f32 ;
33
+ pub fn tgamma ( n : f64 ) -> f64 ;
34
+ pub fn tgammaf ( n : f32 ) -> f32 ;
33
35
}
Original file line number Diff line number Diff line change @@ -23,6 +23,8 @@ extern "C" {
23
23
pub fn sinh ( n : c_double ) -> c_double ;
24
24
pub fn tan ( n : c_double ) -> c_double ;
25
25
pub fn tanh ( n : c_double ) -> c_double ;
26
+ pub fn tgamma ( n : c_double ) -> c_double ;
27
+ pub fn tgammaf ( n : c_float ) -> c_float ;
26
28
}
27
29
28
30
pub use self :: shims:: * ;
You can’t perform that action at this time.
0 commit comments