Skip to content

float tests: deduplicate min, max, and rounding tests #142243

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

Merged
merged 5 commits into from
Jun 14, 2025
Merged
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
122 changes: 2 additions & 120 deletions library/coretests/tests/floats/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use core::ops::{Add, Div, Mul, Sub};
use std::f128::consts;
use std::num::FpCategory as Fp;

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
Expand Down Expand Up @@ -53,34 +55,6 @@ fn test_num_f128() {
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
// the intrinsics.

#[test]
#[cfg(any(miri, target_has_reliable_f128_math))]
fn test_min_nan() {
assert_biteq!(f128::NAN.min(2.0), 2.0);
assert_biteq!(2.0f128.min(f128::NAN), 2.0);
}

#[test]
#[cfg(any(miri, target_has_reliable_f128_math))]
fn test_max_nan() {
assert_biteq!(f128::NAN.max(2.0), 2.0);
assert_biteq!(2.0f128.max(f128::NAN), 2.0);
}

#[test]
#[cfg(any(miri, target_has_reliable_f128_math))]
fn test_minimum() {
assert!(f128::NAN.minimum(2.0).is_nan());
assert!(2.0f128.minimum(f128::NAN).is_nan());
}

#[test]
#[cfg(any(miri, target_has_reliable_f128_math))]
fn test_maximum() {
assert!(f128::NAN.maximum(2.0).is_nan());
assert!(2.0f128.maximum(f128::NAN).is_nan());
}

#[test]
fn test_nan() {
let nan: f128 = f128::NAN;
Expand Down Expand Up @@ -232,98 +206,6 @@ fn test_classify() {
assert_eq!(1e-4932f128.classify(), Fp::Subnormal);
}

#[test]
#[cfg(target_has_reliable_f128_math)]
fn test_floor() {
assert_biteq!(1.0f128.floor(), 1.0f128);
assert_biteq!(1.3f128.floor(), 1.0f128);
assert_biteq!(1.5f128.floor(), 1.0f128);
assert_biteq!(1.7f128.floor(), 1.0f128);
assert_biteq!(0.0f128.floor(), 0.0f128);
assert_biteq!((-0.0f128).floor(), -0.0f128);
assert_biteq!((-1.0f128).floor(), -1.0f128);
assert_biteq!((-1.3f128).floor(), -2.0f128);
assert_biteq!((-1.5f128).floor(), -2.0f128);
assert_biteq!((-1.7f128).floor(), -2.0f128);
}

#[test]
#[cfg(any(miri, target_has_reliable_f128_math))]
fn test_ceil() {
assert_biteq!(1.0f128.ceil(), 1.0f128);
assert_biteq!(1.3f128.ceil(), 2.0f128);
assert_biteq!(1.5f128.ceil(), 2.0f128);
assert_biteq!(1.7f128.ceil(), 2.0f128);
assert_biteq!(0.0f128.ceil(), 0.0f128);
assert_biteq!((-0.0f128).ceil(), -0.0f128);
assert_biteq!((-1.0f128).ceil(), -1.0f128);
assert_biteq!((-1.3f128).ceil(), -1.0f128);
assert_biteq!((-1.5f128).ceil(), -1.0f128);
assert_biteq!((-1.7f128).ceil(), -1.0f128);
}

#[test]
#[cfg(any(miri, target_has_reliable_f128_math))]
fn test_round() {
assert_biteq!(2.5f128.round(), 3.0f128);
assert_biteq!(1.0f128.round(), 1.0f128);
assert_biteq!(1.3f128.round(), 1.0f128);
assert_biteq!(1.5f128.round(), 2.0f128);
assert_biteq!(1.7f128.round(), 2.0f128);
assert_biteq!(0.0f128.round(), 0.0f128);
assert_biteq!((-0.0f128).round(), -0.0f128);
assert_biteq!((-1.0f128).round(), -1.0f128);
assert_biteq!((-1.3f128).round(), -1.0f128);
assert_biteq!((-1.5f128).round(), -2.0f128);
assert_biteq!((-1.7f128).round(), -2.0f128);
}

#[test]
#[cfg(any(miri, target_has_reliable_f128_math))]
fn test_round_ties_even() {
assert_biteq!(2.5f128.round_ties_even(), 2.0f128);
assert_biteq!(1.0f128.round_ties_even(), 1.0f128);
assert_biteq!(1.3f128.round_ties_even(), 1.0f128);
assert_biteq!(1.5f128.round_ties_even(), 2.0f128);
assert_biteq!(1.7f128.round_ties_even(), 2.0f128);
assert_biteq!(0.0f128.round_ties_even(), 0.0f128);
assert_biteq!((-0.0f128).round_ties_even(), -0.0f128);
assert_biteq!((-1.0f128).round_ties_even(), -1.0f128);
assert_biteq!((-1.3f128).round_ties_even(), -1.0f128);
assert_biteq!((-1.5f128).round_ties_even(), -2.0f128);
assert_biteq!((-1.7f128).round_ties_even(), -2.0f128);
}

#[test]
#[cfg(any(miri, target_has_reliable_f128_math))]
fn test_trunc() {
assert_biteq!(1.0f128.trunc(), 1.0f128);
assert_biteq!(1.3f128.trunc(), 1.0f128);
assert_biteq!(1.5f128.trunc(), 1.0f128);
assert_biteq!(1.7f128.trunc(), 1.0f128);
assert_biteq!(0.0f128.trunc(), 0.0f128);
assert_biteq!((-0.0f128).trunc(), -0.0f128);
assert_biteq!((-1.0f128).trunc(), -1.0f128);
assert_biteq!((-1.3f128).trunc(), -1.0f128);
assert_biteq!((-1.5f128).trunc(), -1.0f128);
assert_biteq!((-1.7f128).trunc(), -1.0f128);
}

#[test]
#[cfg(any(miri, target_has_reliable_f128_math))]
fn test_fract() {
assert_biteq!(1.0f128.fract(), 0.0f128);
assert_biteq!(1.3f128.fract(), 0.300000000000000000000000000000000039f128);
assert_biteq!(1.5f128.fract(), 0.5f128);
assert_biteq!(1.7f128.fract(), 0.7f128);
assert_biteq!(0.0f128.fract(), 0.0f128);
assert_biteq!((-0.0f128).fract(), 0.0f128);
assert_biteq!((-1.0f128).fract(), 0.0f128);
assert_biteq!((-1.3f128).fract(), -0.300000000000000000000000000000000039f128);
assert_biteq!((-1.5f128).fract(), -0.5f128);
assert_biteq!((-1.7f128).fract(), -0.699999999999999999999999999999999961f128);
}

#[test]
#[cfg(any(miri, target_has_reliable_f128_math))]
fn test_abs() {
Expand Down
122 changes: 2 additions & 120 deletions library/coretests/tests/floats/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use std::f16::consts;
use std::num::FpCategory as Fp;

use super::{assert_approx_eq, assert_biteq};

/// Tolerance for results on the order of 10.0e-2
#[allow(unused)]
const TOL_N2: f16 = 0.0001;
Expand Down Expand Up @@ -49,34 +51,6 @@ fn test_num_f16() {
// FIXME(f16_f128,miri): many of these have to be disabled since miri does not yet support
// the intrinsics.

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_min_nan() {
assert_biteq!(f16::NAN.min(2.0), 2.0);
assert_biteq!(2.0f16.min(f16::NAN), 2.0);
}

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_max_nan() {
assert_biteq!(f16::NAN.max(2.0), 2.0);
assert_biteq!(2.0f16.max(f16::NAN), 2.0);
}

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_minimum() {
assert!(f16::NAN.minimum(2.0).is_nan());
assert!(2.0f16.minimum(f16::NAN).is_nan());
}

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_maximum() {
assert!(f16::NAN.maximum(2.0).is_nan());
assert!(2.0f16.maximum(f16::NAN).is_nan());
}

#[test]
fn test_nan() {
let nan: f16 = f16::NAN;
Expand Down Expand Up @@ -228,98 +202,6 @@ fn test_classify() {
assert_eq!(1e-5f16.classify(), Fp::Subnormal);
}

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_floor() {
assert_biteq!(1.0f16.floor(), 1.0f16);
assert_biteq!(1.3f16.floor(), 1.0f16);
assert_biteq!(1.5f16.floor(), 1.0f16);
assert_biteq!(1.7f16.floor(), 1.0f16);
assert_biteq!(0.0f16.floor(), 0.0f16);
assert_biteq!((-0.0f16).floor(), -0.0f16);
assert_biteq!((-1.0f16).floor(), -1.0f16);
assert_biteq!((-1.3f16).floor(), -2.0f16);
assert_biteq!((-1.5f16).floor(), -2.0f16);
assert_biteq!((-1.7f16).floor(), -2.0f16);
}

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_ceil() {
assert_biteq!(1.0f16.ceil(), 1.0f16);
assert_biteq!(1.3f16.ceil(), 2.0f16);
assert_biteq!(1.5f16.ceil(), 2.0f16);
assert_biteq!(1.7f16.ceil(), 2.0f16);
assert_biteq!(0.0f16.ceil(), 0.0f16);
assert_biteq!((-0.0f16).ceil(), -0.0f16);
assert_biteq!((-1.0f16).ceil(), -1.0f16);
assert_biteq!((-1.3f16).ceil(), -1.0f16);
assert_biteq!((-1.5f16).ceil(), -1.0f16);
assert_biteq!((-1.7f16).ceil(), -1.0f16);
}

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_round() {
assert_biteq!(2.5f16.round(), 3.0f16);
assert_biteq!(1.0f16.round(), 1.0f16);
assert_biteq!(1.3f16.round(), 1.0f16);
assert_biteq!(1.5f16.round(), 2.0f16);
assert_biteq!(1.7f16.round(), 2.0f16);
assert_biteq!(0.0f16.round(), 0.0f16);
assert_biteq!((-0.0f16).round(), -0.0f16);
assert_biteq!((-1.0f16).round(), -1.0f16);
assert_biteq!((-1.3f16).round(), -1.0f16);
assert_biteq!((-1.5f16).round(), -2.0f16);
assert_biteq!((-1.7f16).round(), -2.0f16);
}

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_round_ties_even() {
assert_biteq!(2.5f16.round_ties_even(), 2.0f16);
assert_biteq!(1.0f16.round_ties_even(), 1.0f16);
assert_biteq!(1.3f16.round_ties_even(), 1.0f16);
assert_biteq!(1.5f16.round_ties_even(), 2.0f16);
assert_biteq!(1.7f16.round_ties_even(), 2.0f16);
assert_biteq!(0.0f16.round_ties_even(), 0.0f16);
assert_biteq!((-0.0f16).round_ties_even(), -0.0f16);
assert_biteq!((-1.0f16).round_ties_even(), -1.0f16);
assert_biteq!((-1.3f16).round_ties_even(), -1.0f16);
assert_biteq!((-1.5f16).round_ties_even(), -2.0f16);
assert_biteq!((-1.7f16).round_ties_even(), -2.0f16);
}

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_trunc() {
assert_biteq!(1.0f16.trunc(), 1.0f16);
assert_biteq!(1.3f16.trunc(), 1.0f16);
assert_biteq!(1.5f16.trunc(), 1.0f16);
assert_biteq!(1.7f16.trunc(), 1.0f16);
assert_biteq!(0.0f16.trunc(), 0.0f16);
assert_biteq!((-0.0f16).trunc(), -0.0f16);
assert_biteq!((-1.0f16).trunc(), -1.0f16);
assert_biteq!((-1.3f16).trunc(), -1.0f16);
assert_biteq!((-1.5f16).trunc(), -1.0f16);
assert_biteq!((-1.7f16).trunc(), -1.0f16);
}

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_fract() {
assert_biteq!(1.0f16.fract(), 0.0f16);
assert_biteq!(1.3f16.fract(), 0.2998f16);
assert_biteq!(1.5f16.fract(), 0.5f16);
assert_biteq!(1.7f16.fract(), 0.7f16);
assert_biteq!(0.0f16.fract(), 0.0f16);
assert_biteq!((-0.0f16).fract(), 0.0f16);
assert_biteq!((-1.0f16).fract(), 0.0f16);
assert_biteq!((-1.3f16).fract(), -0.2998f16);
assert_biteq!((-1.5f16).fract(), -0.5f16);
assert_biteq!((-1.7f16).fract(), -0.7f16);
}

#[test]
#[cfg(any(miri, target_has_reliable_f16_math))]
fn test_abs() {
Expand Down
Loading
Loading