-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add core::{f32,f64}::consts::TAU. #66769
Conversation
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
489fbd5
to
83763ff
Compare
r? @dtolnay |
I am not on the libs team, so my opinion doesn't really matter, but I don't think we should add this. Rust is not batteries included, and this just doesn't add enough value to justify itself. |
Python has added this a few years ago, and it has become quite popular. (More popular than things like Considering the Rust standard library already comes with constants like |
@steveklabnik given that we already have various fractions of pi (pi/2, pi/3, pi/4, pi/6, and pi/8), and adding a new const is super low cost, I don't think that there is much of a cost to adding TAU. Adding TAU allows thinking in terms of rotations instead of half rotations, which is significantly easier. So it does have really nice benefits. We also have FRAC_2_SQRT_PI which is significantly more niche that TAU. I realize that saying we have other constants is not an argument for adding another one, but the cost of adding a constant is almost nonexistent, so I don't see a reason to block it if there is some reason for adding it. Given that TAU makes thinking about rotations easier, and no one would pull a crate for a constant, I think this is fine. |
Related: https://blog.wolfram.com/2015/06/28/2-pi-or-not-2-pi/ // with pi
use std::f64::{PI, sin, cos};
let omega = (cos(2*PI / n), sin(2*PI / n));
// with tau
use std::f64::{TAU, sin, cos};
let omega = (cos(TAU / n), sin(TAU / n)); You can see tau makes code simpler for understanding. @m-ou-se , do you mind adding
|
@MikailBag The reason to add it to the standard library is that it's popular enough now that there are plenty of people who will want to use it. The point is not to use Rust's documentation to convice people to switch to tau (e.g. by linking the manifesto). I already put the definition (τ=2π) in the doc comments. |
3eb11bb
to
d220ed4
Compare
Note also that two other new constants ( Considering tau has way more use cases, and has been in use in Python, Perl, Processing, Nim, Zig, and some others, I don't see why the idea of adding tau should lead to a big discussion every time. |
@KrishnaSannasi We have
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am on board with adding a 2π constant. 2π appears commonly, and the justification that it's useful to have something that couples tightly (#66770 (comment)) is compelling to me. #66769 (comment) is compelling as well: "The point is not to use Rust's documentation to convince people to switch to tau (e.g. by linking the manifesto)." People are free to continue writing 2.0 * PI
, just as they are free to continue using PI / 2.0
instead of our FRAC_PI_2
constant.
I added two checklist items to the tracking issue to follow up prior to stabilization:
- Confirm that these numeric values are accurate. I did not do any validation other than eyeballing the numbers.
- Decide whether we are comfortable using the name TAU or instead a longer but more widely accepted name like TWO_PI could be better.
@bors r+ |
📌 Commit d220ed4 has been approved by |
…nt, r=dtolnay Add core::{f32,f64}::consts::TAU. ### **`τ`**
Rollup of 17 pull requests Successful merges: - #64325 (Stabilize nested self receivers in 1.41.0) - #66222 (Use `eq_opaque_type_and_type` when type-checking closure signatures) - #66305 (Add by-value arrays to `improper_ctypes` lint) - #66399 (rustc_metadata: simplify the interactions between Lazy and Table.) - #66534 (Allow global references via ForeignItem and Item for the same symbol name during LLVM codegen) - #66700 (Fix pointing at arg for fulfillment errors in function calls) - #66704 (Intra doc enum variant field) - #66718 (Refactor `parse_enum_item` to use `parse_delim_comma_seq`) - #66722 (Handle non_exhaustive in borrow checking) - #66744 (Fix shrink_to panic documentation) - #66761 (Use LLVMDisposePassManager instead of raw delete in rustllvm) - #66769 (Add core::{f32,f64}::consts::TAU.) - #66774 (Clean up error codes) - #66777 (Put back tidy check on error codes) - #66797 (Fixes small typo in array docs r? @steveklabnik) - #66798 (Fix spelling typos) - #66800 (Combine similar tests for const match) Failed merges: r? @ghost
Because of floating point, I would expect |
@scottmcm Would the same argument apply to Edit: One can input the digits of pi and tau from this PR here in separate tabs and confirm they share the same significand, differing only by exponent. For a bit of fun, press "toggle details", and tick the exponent value up or down. |
τ