Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue Oct 13, 2022 · 3 comments
Closed
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@ghost
Copy link

ghost commented Oct 13, 2022

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.

@ghost ghost added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Oct 13, 2022
@cuviper
Copy link
Member

cuviper commented Oct 13, 2022

These are a subset of the ones in C++, although C++ also includes phi (φ), the golden ratio.

Maybe we should go ahead and add all that we're missing?

@ghost
Copy link
Author

ghost commented Oct 14, 2022

My statement about a "subset" of C++ was incorrect, as can be seen from the comparison table below.

Of the total 24 constants, Rust has 19 and C++ has 13.
8 of the constants are in both languages. Rust has 11 unique and C++ has 5 unique.

It may be worth adding the 5 constants that are in C++ but not in Rust (phi, gamma, 1/sqrt(pi), sqrt(3), and 1/sqrt(3)).

Click To See Table
Constant Rust C++
e
pi
phi
tau
gamma
1/pi
2/pi
1/sqrt(pi)
2/sqrt(pi)
sqrt(2)
sqrt(3)
1/sqrt(2)
1/sqrt(3)
pi/2
pi/3
pi/4
pi/6
pi/8
ln(2)
ln(10)
log2(10)
log2(e)
log10(2)
log10(e)

@ghost ghost changed the title ACP: Add a constant for the golden ratio (phi) to the floating point types ACP: Add a constant for the golden ratio (phi) to the floating point types (and maybe others) Oct 14, 2022
@ghost ghost changed the title ACP: Add a constant for the golden ratio (phi) to the floating point types (and maybe others) ACP: Add a constant for the golden ratio (phi) to the floating point types Oct 14, 2022
@joshtriplett
Copy link
Member

We reviewed this in today's @rust-lang/libs-api meeting, and we're fine with adding all five of the additional constants C++ has in that table (the five added by rust-lang/rust#103883).

@joshtriplett joshtriplett added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Jun 27, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Aug 28, 2023
Add additional float constants

Initial implementation of this ACP: rust-lang/libs-team#119. [Accepted]
Tracking issue: rust-lang#103883

The values for the constants are copied from the [`libstdc++` source code](https://github.com/gcc-mirror/gcc/blob/16e2427f50c208dfe07d07f18009969502c25dc8/libstdc%2B%2B-v3/include/std/numbers#L57-L120).
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Aug 29, 2023
Add additional float constants

Initial implementation of this ACP: rust-lang/libs-team#119. [Accepted]
Tracking issue: #103883

The values for the constants are copied from the [`libstdc++` source code](https://github.com/gcc-mirror/gcc/blob/16e2427f50c208dfe07d07f18009969502c25dc8/libstdc%2B%2B-v3/include/std/numbers#L57-L120).
lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Apr 7, 2024
Add additional float constants

Initial implementation of this ACP: rust-lang/libs-team#119. [Accepted]
Tracking issue: #103883

The values for the constants are copied from the [`libstdc++` source code](https://github.com/gcc-mirror/gcc/blob/16e2427f50c208dfe07d07f18009969502c25dc8/libstdc%2B%2B-v3/include/std/numbers#L57-L120).
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
Add additional float constants

Initial implementation of this ACP: rust-lang/libs-team#119. [Accepted]
Tracking issue: #103883

The values for the constants are copied from the [`libstdc++` source code](https://github.com/gcc-mirror/gcc/blob/16e2427f50c208dfe07d07f18009969502c25dc8/libstdc%2B%2B-v3/include/std/numbers#L57-L120).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

2 participants