Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c4bd74c

Browse files
authoredJun 9, 2024
Rollup merge of rust-lang#125253 - sunsided:feature/FRAC_1_SQRT_PI, r=Mark-Simulacrum
Add `FRAC_1_SQRT_2PI` constant to f16/f32/f64/f128 This adds the `FRAC_1_SQRT_2PI` to the `f16`, `f32`, `f64` and `f128` as [`1/√(2π)`](https://www.wolframalpha.com/input?i=1%2Fsqrt%282*pi%29). The rationale is that while `FRAC_1_SQRT_PI` already exists, [Gaussian calculations](https://en.wikipedia.org/wiki/Gaussian_function) for random normal distributions require a `1/(σ√(2π))` term, which could then be directly expressed e.g. as `f32::FRAC_1_SQRT_2PI / sigma`. The actual value is approximately `1/√(2π) = 0.3989422804014326779399460599343818684758586311649346576659258296…`. Truncated/rounded forms were used for the individual types. --- ~~I did not any of the `#[unstable]` attributes since I am not aware of their implications.~~ **Edit:** I applied the stability attributes from the surrounding types according to what seemed most likely correct. I believe the `more_float_constants` feature marker is incorrectly applied, but I wasn't sure how to proceed.
2 parents c9c5d8f + 619e33b commit c4bd74c

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed
 

‎core/src/num/f128.rs

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ pub mod consts {
6868
pub const FRAC_1_SQRT_PI: f128 =
6969
0.564189583547756286948079451560772585844050629328998856844086_f128;
7070

71+
/// 1/sqrt(2π)
72+
#[doc(alias = "FRAC_1_SQRT_TAU")]
73+
#[unstable(feature = "f128", issue = "116909")]
74+
// Also, #[unstable(feature = "more_float_constants", issue = "103883")]
75+
pub const FRAC_1_SQRT_2PI: f128 =
76+
0.398942280401432677939946059934381868475858631164934657665926_f128;
77+
7178
/// 2/π
7279
#[unstable(feature = "f128", issue = "116909")]
7380
pub const FRAC_2_PI: f128 = 0.636619772367581343075535053490057448137838582961825794990669_f128;

‎core/src/num/f16.rs

+6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ pub mod consts {
6767
// Also, #[unstable(feature = "more_float_constants", issue = "103883")]
6868
pub const FRAC_1_SQRT_PI: f16 = 0.564189583547756286948079451560772586_f16;
6969

70+
/// 1/sqrt(2π)
71+
#[doc(alias = "FRAC_1_SQRT_TAU")]
72+
#[unstable(feature = "f16", issue = "116909")]
73+
// Also, #[unstable(feature = "more_float_constants", issue = "103883")]
74+
pub const FRAC_1_SQRT_2PI: f16 = 0.398942280401432677939946059934381868_f16;
75+
7076
/// 2/π
7177
#[unstable(feature = "f16", issue = "116909")]
7278
pub const FRAC_2_PI: f16 = 0.636619772367581343075535053490057448_f16;

‎core/src/num/f32.rs

+5
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ pub mod consts {
327327
#[unstable(feature = "more_float_constants", issue = "103883")]
328328
pub const FRAC_1_SQRT_PI: f32 = 0.564189583547756286948079451560772586_f32;
329329

330+
/// 1/sqrt(2π)
331+
#[doc(alias = "FRAC_1_SQRT_TAU")]
332+
#[unstable(feature = "more_float_constants", issue = "103883")]
333+
pub const FRAC_1_SQRT_2PI: f32 = 0.398942280401432677939946059934381868_f32;
334+
330335
/// 2/π
331336
#[stable(feature = "rust1", since = "1.0.0")]
332337
pub const FRAC_2_PI: f32 = 0.636619772367581343075535053490057448_f32;

‎core/src/num/f64.rs

+5
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ pub mod consts {
327327
#[unstable(feature = "more_float_constants", issue = "103883")]
328328
pub const FRAC_1_SQRT_PI: f64 = 0.564189583547756286948079451560772586_f64;
329329

330+
/// 1/sqrt(2π)
331+
#[doc(alias = "FRAC_1_SQRT_TAU")]
332+
#[unstable(feature = "more_float_constants", issue = "103883")]
333+
pub const FRAC_1_SQRT_2PI: f64 = 0.398942280401432677939946059934381868_f64;
334+
330335
/// 2/π
331336
#[stable(feature = "rust1", since = "1.0.0")]
332337
pub const FRAC_2_PI: f64 = 0.636619772367581343075535053490057448_f64;

0 commit comments

Comments
 (0)
Failed to load comments.