rust-lang / rust-lang/libs-team
Add associated consts to f32, f64 for mathematical constants
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 178
- Forks
- 28
- Avg merge
- 15m
- Merged PRs (30d)
- 1
Description
Proposal
Problem statement
Currently, to use mathematical constants such as π, you need to use std::f32::consts::PI.
This proposal is for adding associated constants to the f32 and f64 types so that the constants could be accessed with just f32::PI for example.
Motivation, use-cases
std::f32::consts::PIis quite long. It takes a while to type out, and makes code difficult to read if written out in full everywhere.
Consider
vsassert_eq!(std::f32::consts::FRAC_PI_4.sin(), std::f32::consts::FRAC_1_SQRT_2);
You can always do something likeassert_eq!(f32::FRAC_PI_4.sin(), f32::FRAC_1_SQRT_2);use std::f32::consts as f32c;but it would be nice for a shorter path to be included in the standard library.- I think this is where first-time users of Rust would expect mathematical constants to be. I know that when I first wanted to use π in a rust program, I wrote out
f32::PIand to my disappointment it didn't work and I had to do an internet search to find out the correct path. NAN,INFINITY, etc. are associated constants, and it feels inconsistent to me forf32::NANto exist, but notf32::PI.
Solution sketches
I have implemented this on my fork: https://github.com/rust-lang/rust/compare/master...pommicket:rust:shorter-paths-for-float-consts
Links and related work
- The popular crates
halfandfixedalready have these as associated constants on their types. - Rust internals thread: https://internals.rust-lang.org/t/should-pi-be-available-as-f32-pi-etc/18670
- [Edit:] RFC pull request: https://github.com/rust-lang/rfcs/pull/3418
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the API change proposal process and the RFC pull request, then compare the existing std::f32::consts paths with the fork implementation linked in the proposal. Done means the libs-api team has reviewed the proposal and agreed on whether f32 and f64 associated mathematical constants should be added.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100