Closed

Description
Proposal
Problem statement
The std::f64::consts
module (and its friend std::f32::consts
) currently contain constants for e (euler's number) and pi, along with some others (ln(2), 1/sqrt(2), etc). These are a subset of the ones in C++, although C++ also includes phi (φ), the golden ratio.
I am proposing to add a PHI
constant to the standard library, in similar fashion to the existing floating point constants.
Motivation, use-cases
From a math expression parsing library I'm working on:
use std::f64;
enum MathConst {
E, // euler's number
Pi, // pi
Phi, // golden ratio
}
impl From<MathConst> for f64 {
fn from(value: MathConst) -> f64 {
match value {
MathConst::E => f64::consts::E,
MathConst::Pi => f64::consts::PI,
MathConst::Phi => f64::consts::PHI, // not a thing :(
}
}
}
Solution sketches
In core/src/num/f64.rs
:
pub mod consts {
// ...
/// The golden ratio (φ)
pub const PHI: f64 = 1.618033988749894848204586834365638118_f64;
// ...
}
In core/src/num/f32.rs
:
pub mod consts {
// ...
/// The golden ratio (φ)
pub const PHI: f64 = 1.618033988749894848204586834365638118_f32;
// ...
}
Links and related work
- An RFC discussion on the addition of the
TAU
constant (with some mentions of phi) - cppreference.com
<numbers>
header - libstdc++
inline constexpr double phi
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.