dedup to_radians float test

This commit is contained in:
Karol Zwolak 2025-08-31 18:19:06 +02:00
parent c81a8a89ee
commit 9028efcf2e
5 changed files with 26 additions and 68 deletions

View file

@ -1,18 +1,18 @@
// FIXME(f16_f128): only tested on platforms that have symbols and aren't buggy
#![cfg(target_has_reliable_f128)]
use std::f128::consts;
use super::{assert_approx_eq, assert_biteq};
// Note these tolerances make sense around zero, but not for more extreme exponents.
/// Default tolerances. Works for values that should be near precise but not exact. Roughly
/// the precision carried by `100 * 100`.
#[allow(unused)]
const TOL: f128 = 1e-12;
/// For operations that are near exact, usually not involving math of different
/// signs.
#[allow(unused)]
const TOL_PRECISE: f128 = 1e-28;
/// First pattern over the mantissa
@ -52,23 +52,6 @@ fn test_max_recip() {
);
}
#[test]
fn test_to_radians() {
let pi: f128 = consts::PI;
let nan: f128 = f128::NAN;
let inf: f128 = f128::INFINITY;
let neg_inf: f128 = f128::NEG_INFINITY;
assert_biteq!(0.0f128.to_radians(), 0.0);
assert_approx_eq!(154.6f128.to_radians(), 2.6982790235832334267135442069489767804, TOL);
assert_approx_eq!((-332.31f128).to_radians(), -5.7999036373023566567593094812182763013, TOL);
// check approx rather than exact because round trip for pi doesn't fall on an exactly
// representable value (unlike `f32` and `f64`).
assert_approx_eq!(180.0f128.to_radians(), pi, TOL_PRECISE);
assert!(nan.to_radians().is_nan());
assert_biteq!(inf.to_radians(), inf);
assert_biteq!(neg_inf.to_radians(), neg_inf);
}
#[test]
fn test_float_bits_conv() {
assert_eq!((1f128).to_bits(), 0x3fff0000000000000000000000000000);

View file

@ -1,8 +1,6 @@
// FIXME(f16_f128): only tested on platforms that have symbols and aren't buggy
#![cfg(target_has_reliable_f16)]
use std::f16::consts;
use super::{assert_approx_eq, assert_biteq};
/// Tolerance for results on the order of 10.0e-2
@ -54,21 +52,6 @@ fn test_max_recip() {
assert_approx_eq!(f16::MAX.recip(), 1.526624e-5f16, 1e-4);
}
#[test]
fn test_to_radians() {
let pi: f16 = consts::PI;
let nan: f16 = f16::NAN;
let inf: f16 = f16::INFINITY;
let neg_inf: f16 = f16::NEG_INFINITY;
assert_biteq!(0.0f16.to_radians(), 0.0);
assert_approx_eq!(154.6f16.to_radians(), 2.698279, TOL_0);
assert_approx_eq!((-332.31f16).to_radians(), -5.799903, TOL_0);
assert_approx_eq!(180.0f16.to_radians(), pi, TOL_0);
assert!(nan.to_radians().is_nan());
assert_biteq!(inf.to_radians(), inf);
assert_biteq!(neg_inf.to_radians(), neg_inf);
}
#[test]
fn test_float_bits_conv() {
assert_eq!((1f16).to_bits(), 0x3c00);

View file

@ -1,5 +1,4 @@
use core::f32;
use core::f32::consts;
use super::{assert_approx_eq, assert_biteq};
@ -27,21 +26,6 @@ fn test_mul_add() {
assert_biteq!(f32::math::mul_add(-3.2f32, 2.4, neg_inf), neg_inf);
}
#[test]
fn test_to_radians() {
let pi: f32 = consts::PI;
let nan: f32 = f32::NAN;
let inf: f32 = f32::INFINITY;
let neg_inf: f32 = f32::NEG_INFINITY;
assert_biteq!(0.0f32.to_radians(), 0.0);
assert_approx_eq!(154.6f32.to_radians(), 2.698279);
assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
assert_biteq!(180.0f32.to_radians(), pi);
assert!(nan.to_radians().is_nan());
assert_biteq!(inf.to_radians(), inf);
assert_biteq!(neg_inf.to_radians(), neg_inf);
}
#[test]
fn test_float_bits_conv() {
assert_eq!((1f32).to_bits(), 0x3f800000);

View file

@ -1,5 +1,4 @@
use core::f64;
use core::f64::consts;
use super::{assert_approx_eq, assert_biteq};
@ -27,21 +26,6 @@ fn test_mul_add() {
assert_biteq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
}
#[test]
fn test_to_radians() {
let pi: f64 = consts::PI;
let nan: f64 = f64::NAN;
let inf: f64 = f64::INFINITY;
let neg_inf: f64 = f64::NEG_INFINITY;
assert_biteq!(0.0f64.to_radians(), 0.0);
assert_approx_eq!(154.6f64.to_radians(), 2.698279);
assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
assert_biteq!(180.0f64.to_radians(), pi);
assert!(nan.to_radians().is_nan());
assert_biteq!(inf.to_radians(), inf);
assert_biteq!(neg_inf.to_radians(), neg_inf);
}
#[test]
fn test_float_bits_conv() {
assert_eq!((1f64).to_bits(), 0x3ff0000000000000);

View file

@ -8,6 +8,8 @@ trait TestableFloat: Sized {
const APPROX: Self;
/// Allow looser tolerance for f32 on miri
const POWI_APPROX: Self = Self::APPROX;
/// Allow looser tolerance for f16
const _180_TO_RADIANS_APPROX: Self = Self::APPROX;
/// Allow for looser tolerance for f16
const PI_TO_DEGREES_APPROX: Self = Self::APPROX;
const ZERO: Self;
@ -30,6 +32,7 @@ trait TestableFloat: Sized {
impl TestableFloat for f16 {
type Int = u16;
const APPROX: Self = 1e-3;
const _180_TO_RADIANS_APPROX: Self = 1e-2;
const PI_TO_DEGREES_APPROX: Self = 0.125;
const ZERO: Self = 0.0;
const ONE: Self = 1.0;
@ -1416,3 +1419,24 @@ float_test! {
assert_biteq!((1.0 as Float).to_degrees(), 57.2957795130823208767981548141051703);
}
}
float_test! {
name: to_radians,
attrs: {
f16: #[cfg(target_has_reliable_f16)],
f128: #[cfg(target_has_reliable_f128)],
},
test<Float> {
let pi: Float = Float::PI;
let nan: Float = Float::NAN;
let inf: Float = Float::INFINITY;
let neg_inf: Float = Float::NEG_INFINITY;
assert_biteq!((0.0 as Float).to_radians(), 0.0);
assert_approx_eq!((154.6 as Float).to_radians(), 2.6982790235832334267135442069489767804);
assert_approx_eq!((-332.31 as Float).to_radians(), -5.7999036373023566567593094812182763013);
assert_approx_eq!((180.0 as Float).to_radians(), pi, Float::_180_TO_RADIANS_APPROX);
assert!(nan.to_radians().is_nan());
assert_biteq!(inf.to_radians(), inf);
assert_biteq!(neg_inf.to_radians(), neg_inf);
}
}