Skip to content

ACP: More float constants #661

Closed
Closed

Description

@joshuarayton

Proposal

Implement more float constants, namely: sqrt(5), 1/sqrt(5), ln(3)

Problem statement

The need for using floating point representations of the numerical constants.

Motivating examples or use cases

Constant time generation of Fibonacci numbers

#![feature(f128)]
#![feature(more_float_constants)]

use std::f128::consts::PHI;

const FRAC_1_SQRT_5: f128 =
    0.447213595499957939281834733746255247088123671922305144854179_f128;

fn fib(n: i32) -> u64 {
    return (FRAC_1_SQRT_5 * PHI.powi(n)).round() as u64
}

pub fn main() {
    for n in 1..94 {
        println!("{}", fib(n));
    }
}

Generates all Fibonacci numbers that fit in u64

Solution sketch

In core/src/num/f32.rs:

pub mod consts {
    // ...

    // sqrt(5)
    pub const SQRT_5: f32 = 2.236067977499789696409173668731276235_f32;

    // 1/sqrt(5)
    pub const FRAC_1_SQRT_5: f32 = 0.447213595499957939281834733746255247_f32;

    // ln(3)
    pub const LN_3: f32 = 1.098612288668109691395245236922525704_f32;

    // ...
}

Basically the same in f64.rs, f16.rs, f128.rs

Alternatives

Links and related work

rust-lang/rust#146934
rust-lang/rust#146939
rust-lang/rust#103883
rust-lang/rust#125253
rust-lang/rust#116909
#383
#119

Activity

  1. kennytm commented on Sep 26, 2025

    @kennytm
    Member

    #119 ($\phi, \gamma, \frac1{\sqrt\pi}, \sqrt3, \frac1{\sqrt3}$) was accepted because they are part of C++20's list of constants

    #383 ($\frac1{\sqrt{2\pi}}$) was accepted because it indeed appears in many formula e.g. the normal distribution, error function, Fourier transform, Black-Scholes model etc.

    What is the reason we want $\sqrt5, \frac1{\sqrt5}, \ln 3$ in the standard library? IMO calculating Fibonacci number using $F_n = \left\lfloor \frac{\phi^n}{\sqrt5} \right\rceil$ is not one ($F_{2n} = F_n(2F_{n-1}+F_n)$ is perhaps more efficient), and this does not explain $\sqrt5, \ln 3$ either.

  2. joshuarayton commented on Sep 26, 2025

    @joshuarayton
    Author

    What is the reason we want $\sqrt{5}$ , $\frac{1}{\sqrt{5}}$, $\ln 3$ in the standard library? IMO calculating Fibonacci number using $F_n = ⌊ \frac{\phi^n}{\sqrt{5}} ⌉$ is not one ( $F_{2n} = F_n(2F_{n-1} + F_n)$ ) is perhaps more efficient), and this does not explain $\sqrt{5}$ , $\ln 3$ either.

    $\sqrt{5}$ was proposed in a comment on the issue (rust-lang/rust#103883 (comment))

    I agree with this, it is useful in geometry and trigonometry. When working with the golden ratio, the sqrt(5) is often useful by itself too. It's also used with Diophantine approximations. At least 8 others seemed to agree with this comment.

    $\frac{1}{\sqrt{5}}$ was proposed by me in the pull request. This is so division by the sqrt(5) can be done by reciprocal multiplication at full floating point precision.

    $\ln 3$ was originally proposed in the original issue (rust-lang/rust#103883) for additional constants.

    I no longer think it's needed. Searching GitHub for rust projects that contain the beginning of the decimal expansion didn't give results. It's not even a constant on Wikipedia's list of mathematical constants and it doesn't have it's own Wikipedia page unlike sqrt(5).

  3. the8472 commented on Sep 30, 2025

    @the8472
    Member

    Could you clarify which part you think is no longer needed? Just ln3 or the whole proposal?

    Additionally see rust-lang/rust#146939 (comment), we'd like to see some experimentation with constant-time infinite-precision calculation to happen to see whether a more general approach to produce such values is possible.

  4. joshuarayton commented on Oct 4, 2025

    @joshuarayton
    Author

    Could you clarify which part you think is no longer needed? Just ln3 or the whole proposal?

    Additionally see rust-lang/rust#146939 (comment), we'd like to see some experimentation with constant-time infinite-precision calculation to happen to see whether a more general approach to produce such values is possible.

    I meant that I think that just $\ln 3$ is not needed. This other implementation looks interesting too.

  5. the8472 commented on Oct 28, 2025

    @the8472
    Member

    We discussed this during today's libs-API meeting. We're a bit hesitant to add these since it feels pretty close to the line of diminishing returns, but this one barely passes.

    I meant that I think that just ln ⁡ 3 is not needed. This other implementation looks interesting too.

    Ok, just FRAC_1_SQRT_5 and SQRT_5 then.

    Please file a new tracking issue and then close this one.

  6. added
    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)
    on Oct 28, 2025
  7. added 2 commits that reference this issue on Feb 2, 2026
  8. added 3 commits that reference this issue on Feb 2, 2026
  9. added a commit that references this issue on Feb 3, 2026
  10. clarfonthey commented on Aug 30, 2026

    @clarfonthey
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)api-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