Skip to content

Commit e10e6d7

Browse files
committed
dedup powi float test
1 parent 6ebd009 commit e10e6d7

File tree

5 files changed

+28
-66
lines changed

5 files changed

+28
-66
lines changed

library/coretests/tests/floats/f128.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,6 @@ fn test_max_recip() {
5252
);
5353
}
5454

55-
#[test]
56-
#[cfg(not(miri))]
57-
#[cfg(target_has_reliable_f128_math)]
58-
fn test_powi() {
59-
let nan: f128 = f128::NAN;
60-
let inf: f128 = f128::INFINITY;
61-
let neg_inf: f128 = f128::NEG_INFINITY;
62-
assert_biteq!(1.0f128.powi(1), 1.0);
63-
assert_approx_eq!((-3.1f128).powi(2), 9.6100000000000005506706202140776519387, TOL);
64-
assert_approx_eq!(5.9f128.powi(-2), 0.028727377190462507313100483690639638451, TOL);
65-
assert_biteq!(8.3f128.powi(0), 1.0);
66-
assert!(nan.powi(2).is_nan());
67-
assert_biteq!(inf.powi(3), inf);
68-
assert_biteq!(neg_inf.powi(2), inf);
69-
}
70-
7155
#[test]
7256
fn test_to_degrees() {
7357
let pi: f128 = consts::PI;

library/coretests/tests/floats/f16.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,6 @@ fn test_max_recip() {
5454
assert_approx_eq!(f16::MAX.recip(), 1.526624e-5f16, 1e-4);
5555
}
5656

57-
#[test]
58-
#[cfg(not(miri))]
59-
#[cfg(target_has_reliable_f16_math)]
60-
fn test_powi() {
61-
let nan: f16 = f16::NAN;
62-
let inf: f16 = f16::INFINITY;
63-
let neg_inf: f16 = f16::NEG_INFINITY;
64-
assert_biteq!(1.0f16.powi(1), 1.0);
65-
assert_approx_eq!((-3.1f16).powi(2), 9.61, TOL_0);
66-
assert_approx_eq!(5.9f16.powi(-2), 0.028727, TOL_N2);
67-
assert_biteq!(8.3f16.powi(0), 1.0);
68-
assert!(nan.powi(2).is_nan());
69-
assert_biteq!(inf.powi(3), inf);
70-
assert_biteq!(neg_inf.powi(2), inf);
71-
}
72-
7357
#[test]
7458
fn test_to_degrees() {
7559
let pi: f16 = consts::PI;

library/coretests/tests/floats/f32.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ const NAN_MASK1: u32 = 0x002a_aaaa;
99
/// Second pattern over the mantissa
1010
const NAN_MASK2: u32 = 0x0055_5555;
1111

12-
/// Miri adds some extra errors to float functions; make sure the tests still pass.
13-
/// These values are purely used as a canary to test against and are thus not a stable guarantee Rust provides.
14-
/// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
15-
const APPROX_DELTA: f32 = if cfg!(miri) { 1e-4 } else { 1e-6 };
16-
1712
// FIXME(#140515): mingw has an incorrect fma https://sourceforge.net/p/mingw-w64/bugs/848/
1813
#[cfg_attr(all(target_os = "windows", target_env = "gnu", not(target_abi = "llvm")), ignore)]
1914
#[test]
@@ -32,20 +27,6 @@ fn test_mul_add() {
3227
assert_biteq!(f32::math::mul_add(-3.2f32, 2.4, neg_inf), neg_inf);
3328
}
3429

35-
#[test]
36-
fn test_powi() {
37-
let nan: f32 = f32::NAN;
38-
let inf: f32 = f32::INFINITY;
39-
let neg_inf: f32 = f32::NEG_INFINITY;
40-
assert_approx_eq!(1.0f32.powi(1), 1.0);
41-
assert_approx_eq!((-3.1f32).powi(2), 9.61, APPROX_DELTA);
42-
assert_approx_eq!(5.9f32.powi(-2), 0.028727);
43-
assert_biteq!(8.3f32.powi(0), 1.0);
44-
assert!(nan.powi(2).is_nan());
45-
assert_biteq!(inf.powi(3), inf);
46-
assert_biteq!(neg_inf.powi(2), inf);
47-
}
48-
4930
#[test]
5031
fn test_to_degrees() {
5132
let pi: f32 = consts::PI;

library/coretests/tests/floats/f64.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ fn test_mul_add() {
2727
assert_biteq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
2828
}
2929

30-
#[test]
31-
fn test_powi() {
32-
let nan: f64 = f64::NAN;
33-
let inf: f64 = f64::INFINITY;
34-
let neg_inf: f64 = f64::NEG_INFINITY;
35-
assert_approx_eq!(1.0f64.powi(1), 1.0);
36-
assert_approx_eq!((-3.1f64).powi(2), 9.61);
37-
assert_approx_eq!(5.9f64.powi(-2), 0.028727);
38-
assert_biteq!(8.3f64.powi(0), 1.0);
39-
assert!(nan.powi(2).is_nan());
40-
assert_biteq!(inf.powi(3), inf);
41-
assert_biteq!(neg_inf.powi(2), inf);
42-
}
43-
4430
#[test]
4531
fn test_to_degrees() {
4632
let pi: f64 = consts::PI;

library/coretests/tests/floats/mod.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use std::num::FpCategory as Fp;
22
use std::ops::{Add, Div, Mul, Rem, Sub};
33

4-
trait TestableFloat {
4+
trait TestableFloat: Sized {
55
/// Unsigned int with the same size, for converting to/from bits.
66
type Int;
77
/// Set the default tolerance for float comparison based on the type.
88
const APPROX: Self;
9+
/// Allow looser tolerance for f32 on miri
10+
const POWI_APPROX: Self = Self::APPROX;
911
const ZERO: Self;
1012
const ONE: Self;
1113
const MIN_POSITIVE_NORMAL: Self;
@@ -39,6 +41,10 @@ impl TestableFloat for f16 {
3941
impl TestableFloat for f32 {
4042
type Int = u32;
4143
const APPROX: Self = 1e-6;
44+
/// Miri adds some extra errors to float functions; make sure the tests still pass.
45+
/// These values are purely used as a canary to test against and are thus not a stable guarantee Rust provides.
46+
/// They serve as a way to get an idea of the real precision of floating point operations on different platforms.
47+
const POWI_APPROX: Self = if cfg!(miri) { 1e-4 } else { Self::APPROX };
4248
const ZERO: Self = 0.0;
4349
const ONE: Self = 1.0;
4450
const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
@@ -1360,3 +1366,24 @@ float_test! {
13601366
assert_biteq!(neg_inf.recip(), -0.0);
13611367
}
13621368
}
1369+
1370+
float_test! {
1371+
name: powi,
1372+
attrs: {
1373+
const: #[cfg(false)],
1374+
f16: #[cfg(all(not(miri), target_has_reliable_f16_math))],
1375+
f128: #[cfg(all(not(miri), target_has_reliable_f128_math))],
1376+
},
1377+
test<Float> {
1378+
let nan: Float = Float::NAN;
1379+
let inf: Float = Float::INFINITY;
1380+
let neg_inf: Float = Float::NEG_INFINITY;
1381+
assert_approx_eq!(Float::ONE.powi(1), Float::ONE);
1382+
assert_approx_eq!((-3.1 as Float).powi(2), 9.6100000000000005506706202140776519387, Float::POWI_APPROX);
1383+
assert_approx_eq!((5.9 as Float).powi(-2), 0.028727377190462507313100483690639638451);
1384+
assert_biteq!((8.3 as Float).powi(0), Float::ONE);
1385+
assert!(nan.powi(2).is_nan());
1386+
assert_biteq!(inf.powi(3), inf);
1387+
assert_biteq!(neg_inf.powi(2), inf);
1388+
}
1389+
}

0 commit comments

Comments
 (0)