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::PI is quite long. It takes a while to type out, and makes code difficult to read if written out in full everywhere.
Consider
assert_eq!(std::f32::consts::FRAC_PI_4.sin(), std::f32::consts::FRAC_1_SQRT_2);
vs
assert_eq!(f32::FRAC_PI_4.sin(), f32::FRAC_1_SQRT_2);
You can always do something like 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::PI and 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 for f32::NAN to exist, but not f32::PI.
Solution sketches
I have implemented this on my fork: rust-lang/rust@master...pommicket:rust:shorter-paths-for-float-consts
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.
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
f32andf64types so that the constants could be accessed with justf32::PIfor 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
use std::f32::consts as f32c;but it would be nice for a shorter path to be included in the standard library.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: rust-lang/rust@master...pommicket:rust:shorter-paths-for-float-consts
Links and related work
halfandfixedalready have these as associated constants on their types.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.