1
Fork 0

Implement f{16,32,64,128}::{erf,erfc}

Also add
```rust
// #[unstable(feature = "float_gamma", issue = "99842")]
```
to `gamma`-function-related methods on `f16` & `f128`,
as per https://github.com/rust-lang/rust/pull/136324#issuecomment-2626270247
This commit is contained in:
Pavel Grigorenko 2025-01-31 02:52:29 +03:00
parent 021fb9c09a
commit b8f0ed37bd
5 changed files with 280 additions and 0 deletions

View file

@ -1151,4 +1151,68 @@ impl f64 {
let x = unsafe { cmath::lgamma_r(self, &mut signgamp) };
(x, signgamp)
}
/// Error function.
///
/// # Unspecified precision
///
/// The precision of this function is non-deterministic. This means it varies by platform,
/// Rust version, and can even differ within the same execution from one invocation to the next.
///
/// This function currently corresponds to the `erf` from libc on Unix
/// and Windows. Note that this might change in the future.
///
/// # Examples
///
/// ```
/// #![feature(float_erf)]
/// /// The error function relates what percent of a normal distribution lies
/// /// within `x` standard deviations (scaled by `1/sqrt(2)`).
/// fn within_standard_deviations(x: f64) -> f64 {
/// (x * std::f64::consts::FRAC_1_SQRT_2).erf() * 100.0
/// }
///
/// // 68% of a normal distribution is within one standard deviation
/// assert!((within_standard_deviations(1.0) - 68.269).abs() < 0.01);
/// // 95% of a normal distribution is within two standard deviations
/// assert!((within_standard_deviations(2.0) - 95.450).abs() < 0.01);
/// // 99.7% of a normal distribution is within three standard deviations
/// assert!((within_standard_deviations(3.0) - 99.730).abs() < 0.01);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_erf", issue = "136321")]
#[inline]
pub fn erf(self) -> f64 {
unsafe { cmath::erf(self) }
}
/// Complementary error function.
///
/// # Unspecified precision
///
/// The precision of this function is non-deterministic. This means it varies by platform,
/// Rust version, and can even differ within the same execution from one invocation to the next.
///
/// This function currently corresponds to the `erfc` from libc on Unix
/// and Windows. Note that this might change in the future.
///
/// # Examples
///
/// ```
/// #![feature(float_erf)]
/// let x: f64 = 0.123;
///
/// let one = x.erf() + x.erfc();
/// let abs_difference = (one - 1.0).abs();
///
/// assert!(abs_difference <= f64::EPSILON);
/// ```
#[rustc_allow_incoherent_impl]
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "float_erf", issue = "136321")]
#[inline]
pub fn erfc(self) -> f64 {
unsafe { cmath::erfc(self) }
}
}