Skip to content
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

Make to_degrees and to_radians const #124002

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions library/core/src/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,9 @@ impl f32 {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[stable(feature = "f32_deg_rad_conversions", since = "1.7.0")]
#[rustc_const_unstable(feature = "const_to_degrees_and_to_radians", issue = "none")]
#[inline]
pub fn to_degrees(self) -> f32 {
pub const fn to_degrees(self) -> f32 {
// Use a constant for better precision.
const PIS_IN_180: f32 = 57.2957795130823208767981548141051703_f32;
self * PIS_IN_180
Expand All @@ -899,8 +900,9 @@ impl f32 {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[stable(feature = "f32_deg_rad_conversions", since = "1.7.0")]
#[rustc_const_unstable(feature = "const_to_degrees_and_to_radians", issue = "none")]
#[inline]
pub fn to_radians(self) -> f32 {
pub const fn to_radians(self) -> f32 {
let value: f32 = consts::PI;
self * (value / 180.0f32)
}
Expand Down
6 changes: 4 additions & 2 deletions library/core/src/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,9 @@ impl f64 {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_to_degrees_and_to_radians", issue = "none")]
#[inline]
pub fn to_degrees(self) -> f64 {
pub const fn to_degrees(self) -> f64 {
// The division here is correctly rounded with respect to the true
// value of 180/π. (This differs from f32, where a constant must be
// used to ensure a correctly rounded result.)
Expand All @@ -910,8 +911,9 @@ impl f64 {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_to_degrees_and_to_radians", issue = "none")]
#[inline]
pub fn to_radians(self) -> f64 {
pub const fn to_radians(self) -> f64 {
let value: f64 = consts::PI;
self * (value / 180.0)
}
Expand Down
Loading