Skip to content

Commit 042d69a

Browse files
authored
Rollup merge of rust-lang#99747 - ankane:float_gamma, r=joshtriplett
Add gamma function to f32 and f64 Adds the [gamma function](https://en.wikipedia.org/wiki/Gamma_function) to `f32` and `f64` (`tgamma` and `tgammaf` from C). Refs: - rust-lang/rfcs#864 - rust-lang#18271
2 parents 5996733 + 7d2e763 commit 042d69a

File tree

7 files changed

+75
-0
lines changed

7 files changed

+75
-0
lines changed

Diff for: library/std/src/f32.rs

+20
Original file line numberDiff line numberDiff line change
@@ -920,4 +920,24 @@ impl f32 {
920920
pub fn atanh(self) -> f32 {
921921
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
922922
}
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+
}
923943
}

Diff for: library/std/src/f32/tests.rs

+15
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,21 @@ fn test_atanh() {
550550
assert_approx_eq!((-0.5f32).atanh(), -0.54930614433405484569762261846126285f32);
551551
}
552552

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+
553568
#[test]
554569
fn test_real_consts() {
555570
use super::consts;

Diff for: library/std/src/f64.rs

+20
Original file line numberDiff line numberDiff line change
@@ -946,4 +946,24 @@ impl f64 {
946946
Self::NAN // log(-Inf) = NaN
947947
}
948948
}
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+
}
949969
}

Diff for: library/std/src/f64/tests.rs

+15
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,21 @@ fn test_atanh() {
535535
assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64);
536536
}
537537

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+
538553
#[test]
539554
fn test_real_consts() {
540555
use super::consts;

Diff for: library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@
272272
#![feature(exact_size_is_empty)]
273273
#![feature(exclusive_wrapper)]
274274
#![feature(extend_one)]
275+
#![feature(float_gamma)]
275276
#![feature(float_minimum_maximum)]
276277
#![feature(hasher_prefixfree_extras)]
277278
#![feature(hashmap_internals)]

Diff for: library/std/src/sys/unix/cmath.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ extern "C" {
3030
pub fn tanf(n: f32) -> f32;
3131
pub fn tanh(n: f64) -> f64;
3232
pub fn tanhf(n: f32) -> f32;
33+
pub fn tgamma(n: f64) -> f64;
34+
pub fn tgammaf(n: f32) -> f32;
3335
}

Diff for: library/std/src/sys/windows/cmath.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ extern "C" {
2323
pub fn sinh(n: c_double) -> c_double;
2424
pub fn tan(n: c_double) -> c_double;
2525
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;
2628
}
2729

2830
pub use self::shims::*;

0 commit comments

Comments
 (0)