rust-lang / rust-lang/libs-team

Add associated consts to f32, f64 for mathematical constants

Open
#210 14 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

api-change-proposal
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

  1. 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.
  2. 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.
  3. 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: https://github.com/rust-lang/rust/compare/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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.