Skip to content

Commit 3d3e2b6

Browse files
committed
Auto merge of #1274 - RalfJung:float-cast, r=RalfJung
fix float test comments and test a few more int->float casts
2 parents fc30266 + 4e471f7 commit 3d3e2b6

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

tests/run-pass/floats.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
use std::fmt::Debug;
33

44
// Helper function to avoid promotion so that this tests "run-time" casts, not CTFE.
5-
// Doesn't make a big difference when running this in Miri, but when running this in
6-
// rustc (with -Zmir-opt-level=0) for comparison it means we use LLVM casts.
5+
// Doesn't make a big difference when running this in Miri, but it means we can compare this
6+
// with the LLVM backend by running `rustc -Zmir-opt-level=0 -Zsaturating-float-casts`.
77
#[track_caller]
88
#[inline(never)]
99
fn assert_eq<T: PartialEq + Debug>(x: T, y: T) {
@@ -29,7 +29,7 @@ fn main() {
2929
let y: f32 = unsafe { std::mem::transmute(x) };
3030
assert_eq(y, 42.0_f32);
3131

32-
// f32-to-int casts
32+
// f32 <-> int casts
3333
assert_eq(5.0f32 as u32, 5);
3434
assert_eq(-5.0f32 as u32, 0);
3535
assert_eq(5.0f32 as i32, 5);
@@ -44,11 +44,13 @@ fn main() {
4444
assert_eq(std::f32::NEG_INFINITY as u32, 0);
4545
assert_eq(std::f32::NAN as i32, 0);
4646
assert_eq(std::f32::NAN as u32, 0);
47-
assert_eq(u128::MAX as f32, std::f32::INFINITY);
4847
assert_eq((u32::MAX-127) as f32 as u32, u32::MAX); // rounding loss
4948
assert_eq((u32::MAX-128) as f32 as u32, u32::MAX-255); // rounding loss
49+
assert_eq(127i8 as f32, 127.0f32);
50+
assert_eq(i128::MIN as f32, -170141183460469231731687303715884105728.0f32);
51+
assert_eq(u128::MAX as f32, std::f32::INFINITY); // saturation
5052

51-
// f64-to-int casts
53+
// f64 <-> int casts
5254
assert_eq(5.0f64 as u64, 5);
5355
assert_eq(-5.0f64 as u64, 0);
5456
assert_eq(5.0f64 as i64, 5);
@@ -63,9 +65,11 @@ fn main() {
6365
assert_eq(std::f64::NEG_INFINITY as u64, 0);
6466
assert_eq(std::f64::NAN as i64, 0);
6567
assert_eq(std::f64::NAN as u64, 0);
66-
assert_eq(u128::MAX as f64 as u128, u128::MAX);
6768
assert_eq((u64::MAX-1023) as f64 as u64, u64::MAX); // rounding loss
6869
assert_eq((u64::MAX-1024) as f64 as u64, u64::MAX-2047); // rounding loss
70+
assert_eq(u128::MAX as f64 as u128, u128::MAX);
71+
assert_eq(i16::MIN as f64, -32768.0f64);
72+
assert_eq(u128::MAX as f64, 340282366920938463463374607431768211455.0f64); // even that fits...
6973

7074
// f32 <-> f64 casts
7175
assert_eq(5.0f64 as f32, 5.0f32);

0 commit comments

Comments
 (0)