about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-02-02 16:29:23 +0800
committerkennytm <kennytm@gmail.com>2018-02-02 22:48:49 +0800
commit7c6380cdcf60269bd11457eb00e8d18f4f6955f6 (patch)
tree18c289173179b5d2e9d4051009774421ba42dd32 /src/libcore
parent5edeff31305c36a440ca8482043ad52e6f14b0ed (diff)
parente34c31bf02eb0f0ff4dd43ae72e0eae53f2ac519 (diff)
downloadrust-7c6380cdcf60269bd11457eb00e8d18f4f6955f6.tar.gz
rust-7c6380cdcf60269bd11457eb00e8d18f4f6955f6.zip
Rollup merge of #47919 - varkor:to_degrees-precision, r=rkruppe Use constant for 180/π in to_degrees The current `f32|f64.to_degrees` implementation uses a division to calculate `180/π`, which causes a loss of precision. Using a constant is still not perfect (implementing a maximally-precise algorithm would come with a high performance cost), but improves precision with a minimal change. As per the discussion in #29944, this fixes #29944 (the costs of improving the precision further would not outweigh the gains).
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/num/f32.rs4
-rw-r--r--src/libcore/num/f64.rs3
2 files changed, 6 insertions, 1 deletions
diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs
index 207df84d080..3586fa5442f 100644
--- a/src/libcore/num/f32.rs
+++ b/src/libcore/num/f32.rs
@@ -239,7 +239,9 @@ impl Float for f32 {
     /// Converts to degrees, assuming the number is in radians.
     #[inline]
     fn to_degrees(self) -> f32 {
-        self * (180.0f32 / consts::PI)
+        // Use a constant for better precision.
+        const PIS_IN_180: f32 = 57.2957795130823208767981548141051703_f32;
+        self * PIS_IN_180
     }
 
     /// Converts to radians, assuming the number is in degrees.
diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs
index 9206132e8b4..64c0d508b38 100644
--- a/src/libcore/num/f64.rs
+++ b/src/libcore/num/f64.rs
@@ -237,6 +237,9 @@ impl Float for f64 {
     /// Converts to degrees, assuming the number is in radians.
     #[inline]
     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.)
         self * (180.0f64 / consts::PI)
     }