Skip to content

f32::FRAC_PI_6.to_degrees() is now less accurate #48617

Closed
@cuviper

Description

@cuviper

On stable, f32 FRAC_PI_6.to_degrees() returns exactly 30.0. On beta and nightly, it returns 30.000002.

This is probably related to #47919, which intended to increase accuracy. cc @varkor

Here's a full test of PI constants, asserting errors < 1e-6:

use std::f32::consts::*;

const DEG_RAD_PAIRS: [(f32, f32); 7] = [
    (0.0, 0.),
    (22.5, FRAC_PI_8),
    (30.0, FRAC_PI_6),
    (45.0, FRAC_PI_4),
    (60.0, FRAC_PI_3),
    (90.0, FRAC_PI_2),
    (180.0, PI),
];

fn main() {
    for &(deg, rad) in &DEG_RAD_PAIRS {
        let rad2 = deg.to_radians();
        let deg2 = rad.to_degrees();
        
        println!("degrees: {:?} {:?}", deg, deg2);
        assert!((deg2 - deg).abs() < 1e-6);
        
        println!("radians: {:?} {:?}", rad, rad2);
        assert!((rad2 - rad).abs() < 1e-6);
    }
}

playground

It passes on stable 1.24, but fails on beta and nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions