Skip to content

ACP: Add a constant for the golden ratio (phi) to the floating point types #119

Closed
@ghost

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

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions