Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1d520e2

Browse files
Rollup merge of #145468 - karolzwolak:float-tests-dedup, r=tgross35
dedup recip, powi, to_degrees, and to_radians float tests Deduplicates recip, powi, to_degrees, and to_radians float tests. I had to fiddle and slightly increase the tolerances for a few comparisons, so maybe not all of the tests are worth deduplicating. Part of #141726. Best reviewed commit-by-commit. r? `@tgross35`
2 parents 828e45a + 9028efc commit 1d520e2

File tree

5 files changed

+105
-243
lines changed

5 files changed

+105
-243
lines changed

‎library/coretests/tests/floats/f128.rs

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// FIXME(f16_f128): only tested on platforms that have symbols and aren't buggy
22
#![cfg(target_has_reliable_f128)]
33

4-
use std::f128::consts;
5-
64
use super::{assert_approx_eq, assert_biteq};
75

86
// Note these tolerances make sense around zero, but not for more extreme exponents.
97

108
/// Default tolerances. Works for values that should be near precise but not exact. Roughly
119
/// the precision carried by `100 * 100`.
10+
#[allow(unused)]
1211
const TOL: f128 = 1e-12;
1312

1413
/// For operations that are near exact, usually not involving math of different
1514
/// signs.
15+
#[allow(unused)]
1616
const TOL_PRECISE: f128 = 1e-28;
1717

1818
/// First pattern over the mantissa
@@ -44,70 +44,12 @@ fn test_mul_add() {
4444

4545
#[test]
4646
#[cfg(any(miri, target_has_reliable_f128_math))]
47-
fn test_recip() {
48-
let nan: f128 = f128::NAN;
49-
let inf: f128 = f128::INFINITY;
50-
let neg_inf: f128 = f128::NEG_INFINITY;
51-
assert_biteq!(1.0f128.recip(), 1.0);
52-
assert_biteq!(2.0f128.recip(), 0.5);
53-
assert_biteq!((-0.4f128).recip(), -2.5);
54-
assert_biteq!(0.0f128.recip(), inf);
47+
fn test_max_recip() {
5548
assert_approx_eq!(
5649
f128::MAX.recip(),
5750
8.40525785778023376565669454330438228902076605e-4933,
5851
1e-4900
5952
);
60-
assert!(nan.recip().is_nan());
61-
assert_biteq!(inf.recip(), 0.0);
62-
assert_biteq!(neg_inf.recip(), -0.0);
63-
}
64-
65-
#[test]
66-
#[cfg(not(miri))]
67-
#[cfg(target_has_reliable_f128_math)]
68-
fn test_powi() {
69-
let nan: f128 = f128::NAN;
70-
let inf: f128 = f128::INFINITY;
71-
let neg_inf: f128 = f128::NEG_INFINITY;
72-
assert_biteq!(1.0f128.powi(1), 1.0);
73-
assert_approx_eq!((-3.1f128).powi(2), 9.6100000000000005506706202140776519387, TOL);
74-
assert_approx_eq!(5.9f128.powi(-2), 0.028727377190462507313100483690639638451, TOL);
75-
assert_biteq!(8.3f128.powi(0), 1.0);
76-
assert!(nan.powi(2).is_nan());
77-
assert_biteq!(inf.powi(3), inf);
78-
assert_biteq!(neg_inf.powi(2), inf);
79-
}
80-
81-
#[test]
82-
fn test_to_degrees() {
83-
let pi: f128 = consts::PI;
84-
let nan: f128 = f128::NAN;
85-
let inf: f128 = f128::INFINITY;
86-
let neg_inf: f128 = f128::NEG_INFINITY;
87-
assert_biteq!(0.0f128.to_degrees(), 0.0);
88-
assert_approx_eq!((-5.8f128).to_degrees(), -332.31552117587745090765431723855668471, TOL);
89-
assert_approx_eq!(pi.to_degrees(), 180.0, TOL);
90-
assert!(nan.to_degrees().is_nan());
91-
assert_biteq!(inf.to_degrees(), inf);
92-
assert_biteq!(neg_inf.to_degrees(), neg_inf);
93-
assert_biteq!(1_f128.to_degrees(), 57.2957795130823208767981548141051703);
94-
}
95-
96-
#[test]
97-
fn test_to_radians() {
98-
let pi: f128 = consts::PI;
99-
let nan: f128 = f128::NAN;
100-
let inf: f128 = f128::INFINITY;
101-
let neg_inf: f128 = f128::NEG_INFINITY;
102-
assert_biteq!(0.0f128.to_radians(), 0.0);
103-
assert_approx_eq!(154.6f128.to_radians(), 2.6982790235832334267135442069489767804, TOL);
104-
assert_approx_eq!((-332.31f128).to_radians(), -5.7999036373023566567593094812182763013, TOL);
105-
// check approx rather than exact because round trip for pi doesn't fall on an exactly
106-
// representable value (unlike `f32` and `f64`).
107-
assert_approx_eq!(180.0f128.to_radians(), pi, TOL_PRECISE);
108-
assert!(nan.to_radians().is_nan());
109-
assert_biteq!(inf.to_radians(), inf);
110-
assert_biteq!(neg_inf.to_radians(), neg_inf);
11153
}
11254

11355
#[test]

‎library/coretests/tests/floats/f16.rs

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// FIXME(f16_f128): only tested on platforms that have symbols and aren't buggy
22
#![cfg(target_has_reliable_f16)]
33

4-
use std::f16::consts;
5-
64
use super::{assert_approx_eq, assert_biteq};
75

86
/// Tolerance for results on the order of 10.0e-2
@@ -50,64 +48,8 @@ fn test_mul_add() {
5048

5149
#[test]
5250
#[cfg(any(miri, target_has_reliable_f16_math))]
53-
fn test_recip() {
54-
let nan: f16 = f16::NAN;
55-
let inf: f16 = f16::INFINITY;
56-
let neg_inf: f16 = f16::NEG_INFINITY;
57-
assert_biteq!(1.0f16.recip(), 1.0);
58-
assert_biteq!(2.0f16.recip(), 0.5);
59-
assert_biteq!((-0.4f16).recip(), -2.5);
60-
assert_biteq!(0.0f16.recip(), inf);
51+
fn test_max_recip() {
6152
assert_approx_eq!(f16::MAX.recip(), 1.526624e-5f16, 1e-4);
62-
assert!(nan.recip().is_nan());
63-
assert_biteq!(inf.recip(), 0.0);
64-
assert_biteq!(neg_inf.recip(), -0.0);
65-
}
66-
67-
#[test]
68-
#[cfg(not(miri))]
69-
#[cfg(target_has_reliable_f16_math)]
70-
fn test_powi() {
71-
let nan: f16 = f16::NAN;
72-
let inf: f16 = f16::INFINITY;
73-
let neg_inf: f16 = f16::NEG_INFINITY;
74-
assert_biteq!(1.0f16.powi(1), 1.0);
75-
assert_approx_eq!((-3.1f16).powi(2), 9.61, TOL_0);
76-
assert_approx_eq!(5.9f16.powi(-2), 0.028727, TOL_N2);
77-
assert_biteq!(8.3f16.powi(0), 1.0);
78-
assert!(nan.powi(2).is_nan());
79-
assert_biteq!(inf.powi(3), inf);
80-
assert_biteq!(neg_inf.powi(2), inf);
81-
}
82-
83-
#[test]
84-
fn test_to_degrees() {
85-
let pi: f16 = consts::PI;
86-
let nan: f16 = f16::NAN;
87-
let inf: f16 = f16::INFINITY;
88-
let neg_inf: f16 = f16::NEG_INFINITY;
89-
assert_biteq!(0.0f16.to_degrees(), 0.0);
90-
assert_approx_eq!((-5.8f16).to_degrees(), -332.315521, TOL_P2);
91-
assert_approx_eq!(pi.to_degrees(), 180.0, TOL_P2);
92-
assert!(nan.to_degrees().is_nan());
93-
assert_biteq!(inf.to_degrees(), inf);
94-
assert_biteq!(neg_inf.to_degrees(), neg_inf);
95-
assert_biteq!(1_f16.to_degrees(), 57.2957795130823208767981548141051703);
96-
}
97-
98-
#[test]
99-
fn test_to_radians() {
100-
let pi: f16 = consts::PI;
101-
let nan: f16 = f16::NAN;
102-
let inf: f16 = f16::INFINITY;
103-
let neg_inf: f16 = f16::NEG_INFINITY;
104-
assert_biteq!(0.0f16.to_radians(), 0.0);
105-
assert_approx_eq!(154.6f16.to_radians(), 2.698279, TOL_0);
106-
assert_approx_eq!((-332.31f16).to_radians(), -5.799903, TOL_0);
107-
assert_approx_eq!(180.0f16.to_radians(), pi, TOL_0);
108-
assert!(nan.to_radians().is_nan());
109-
assert_biteq!(inf.to_radians(), inf);
110-
assert_biteq!(neg_inf.to_radians(), neg_inf);
11153
}
11254

11355
#[test]

‎library/coretests/tests/floats/f32.rs

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use core::f32;
2-
use core::f32::consts;
32

43
use super::{assert_approx_eq, assert_biteq};
54

@@ -9,11 +8,6 @@ const NAN_MASK1: u32 = 0x002a_aaaa;
98
/// Second pattern over the mantissa
109
const NAN_MASK2: u32 = 0x0055_5555;
1110

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-
1711
// FIXME(#140515): mingw has an incorrect fma https://sourceforge.net/p/mingw-w64/bugs/848/
1812
#[cfg_attr(all(target_os = "windows", target_env = "gnu", not(target_abi = "llvm")), ignore)]
1913
#[test]
@@ -32,64 +26,6 @@ fn test_mul_add() {
3226
assert_biteq!(f32::math::mul_add(-3.2f32, 2.4, neg_inf), neg_inf);
3327
}
3428

35-
#[test]
36-
fn test_recip() {
37-
let nan: f32 = f32::NAN;
38-
let inf: f32 = f32::INFINITY;
39-
let neg_inf: f32 = f32::NEG_INFINITY;
40-
assert_biteq!(1.0f32.recip(), 1.0);
41-
assert_biteq!(2.0f32.recip(), 0.5);
42-
assert_biteq!((-0.4f32).recip(), -2.5);
43-
assert_biteq!(0.0f32.recip(), inf);
44-
assert!(nan.recip().is_nan());
45-
assert_biteq!(inf.recip(), 0.0);
46-
assert_biteq!(neg_inf.recip(), -0.0);
47-
}
48-
49-
#[test]
50-
fn test_powi() {
51-
let nan: f32 = f32::NAN;
52-
let inf: f32 = f32::INFINITY;
53-
let neg_inf: f32 = f32::NEG_INFINITY;
54-
assert_approx_eq!(1.0f32.powi(1), 1.0);
55-
assert_approx_eq!((-3.1f32).powi(2), 9.61, APPROX_DELTA);
56-
assert_approx_eq!(5.9f32.powi(-2), 0.028727);
57-
assert_biteq!(8.3f32.powi(0), 1.0);
58-
assert!(nan.powi(2).is_nan());
59-
assert_biteq!(inf.powi(3), inf);
60-
assert_biteq!(neg_inf.powi(2), inf);
61-
}
62-
63-
#[test]
64-
fn test_to_degrees() {
65-
let pi: f32 = consts::PI;
66-
let nan: f32 = f32::NAN;
67-
let inf: f32 = f32::INFINITY;
68-
let neg_inf: f32 = f32::NEG_INFINITY;
69-
assert_biteq!(0.0f32.to_degrees(), 0.0);
70-
assert_approx_eq!((-5.8f32).to_degrees(), -332.315521);
71-
assert_biteq!(pi.to_degrees(), 180.0);
72-
assert!(nan.to_degrees().is_nan());
73-
assert_biteq!(inf.to_degrees(), inf);
74-
assert_biteq!(neg_inf.to_degrees(), neg_inf);
75-
assert_biteq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
76-
}
77-
78-
#[test]
79-
fn test_to_radians() {
80-
let pi: f32 = consts::PI;
81-
let nan: f32 = f32::NAN;
82-
let inf: f32 = f32::INFINITY;
83-
let neg_inf: f32 = f32::NEG_INFINITY;
84-
assert_biteq!(0.0f32.to_radians(), 0.0);
85-
assert_approx_eq!(154.6f32.to_radians(), 2.698279);
86-
assert_approx_eq!((-332.31f32).to_radians(), -5.799903);
87-
assert_biteq!(180.0f32.to_radians(), pi);
88-
assert!(nan.to_radians().is_nan());
89-
assert_biteq!(inf.to_radians(), inf);
90-
assert_biteq!(neg_inf.to_radians(), neg_inf);
91-
}
92-
9329
#[test]
9430
fn test_float_bits_conv() {
9531
assert_eq!((1f32).to_bits(), 0x3f800000);

‎library/coretests/tests/floats/f64.rs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use core::f64;
2-
use core::f64::consts;
32

43
use super::{assert_approx_eq, assert_biteq};
54

@@ -27,63 +26,6 @@ fn test_mul_add() {
2726
assert_biteq!((-3.2f64).mul_add(2.4, neg_inf), neg_inf);
2827
}
2928

30-
#[test]
31-
fn test_recip() {
32-
let nan: f64 = f64::NAN;
33-
let inf: f64 = f64::INFINITY;
34-
let neg_inf: f64 = f64::NEG_INFINITY;
35-
assert_biteq!(1.0f64.recip(), 1.0);
36-
assert_biteq!(2.0f64.recip(), 0.5);
37-
assert_biteq!((-0.4f64).recip(), -2.5);
38-
assert_biteq!(0.0f64.recip(), inf);
39-
assert!(nan.recip().is_nan());
40-
assert_biteq!(inf.recip(), 0.0);
41-
assert_biteq!(neg_inf.recip(), -0.0);
42-
}
43-
44-
#[test]
45-
fn test_powi() {
46-
let nan: f64 = f64::NAN;
47-
let inf: f64 = f64::INFINITY;
48-
let neg_inf: f64 = f64::NEG_INFINITY;
49-
assert_approx_eq!(1.0f64.powi(1), 1.0);
50-
assert_approx_eq!((-3.1f64).powi(2), 9.61);
51-
assert_approx_eq!(5.9f64.powi(-2), 0.028727);
52-
assert_biteq!(8.3f64.powi(0), 1.0);
53-
assert!(nan.powi(2).is_nan());
54-
assert_biteq!(inf.powi(3), inf);
55-
assert_biteq!(neg_inf.powi(2), inf);
56-
}
57-
58-
#[test]
59-
fn test_to_degrees() {
60-
let pi: f64 = consts::PI;
61-
let nan: f64 = f64::NAN;
62-
let inf: f64 = f64::INFINITY;
63-
let neg_inf: f64 = f64::NEG_INFINITY;
64-
assert_biteq!(0.0f64.to_degrees(), 0.0);
65-
assert_approx_eq!((-5.8f64).to_degrees(), -332.315521);
66-
assert_biteq!(pi.to_degrees(), 180.0);
67-
assert!(nan.to_degrees().is_nan());
68-
assert_biteq!(inf.to_degrees(), inf);
69-
assert_biteq!(neg_inf.to_degrees(), neg_inf);
70-
}
71-
72-
#[test]
73-
fn test_to_radians() {
74-
let pi: f64 = consts::PI;
75-
let nan: f64 = f64::NAN;
76-
let inf: f64 = f64::INFINITY;
77-
let neg_inf: f64 = f64::NEG_INFINITY;
78-
assert_biteq!(0.0f64.to_radians(), 0.0);
79-
assert_approx_eq!(154.6f64.to_radians(), 2.698279);
80-
assert_approx_eq!((-332.31f64).to_radians(), -5.799903);
81-
assert_biteq!(180.0f64.to_radians(), pi);
82-
assert!(nan.to_radians().is_nan());
83-
assert_biteq!(inf.to_radians(), inf);
84-
assert_biteq!(neg_inf.to_radians(), neg_inf);
85-
}
86-
8729
#[test]
8830
fn test_float_bits_conv() {
8931
assert_eq!((1f64).to_bits(), 0x3ff0000000000000);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /