]> git.lizzy.rs Git - rust.git/commitdiff
fix float test comments and test a few more int->float casts
authorRalf Jung <post@ralfj.de>
Sat, 28 Mar 2020 14:45:45 +0000 (15:45 +0100)
committerRalf Jung <post@ralfj.de>
Sat, 28 Mar 2020 14:47:00 +0000 (15:47 +0100)
tests/run-pass/floats.rs

index 5a7413e98ea819498d008705eb2cc357d49a0be4..394e91ac9c3b8df24fc29de96541f533d7c57b06 100644 (file)
@@ -2,8 +2,8 @@
 use std::fmt::Debug;
 
 // Helper function to avoid promotion so that this tests "run-time" casts, not CTFE.
-// Doesn't make a big difference when running this in Miri, but when running this in
-// rustc (with -Zmir-opt-level=0) for comparison it means we use LLVM casts.
+// Doesn't make a big difference when running this in Miri, but it means we can compare this
+// with the LLVM backend by running `rustc -Zmir-opt-level=0 -Zsaturating-float-casts`.
 #[track_caller]
 #[inline(never)]
 fn assert_eq<T: PartialEq + Debug>(x: T, y: T) {
@@ -29,7 +29,7 @@ fn main() {
     let y: f32 = unsafe { std::mem::transmute(x) };
     assert_eq(y, 42.0_f32);
 
-    // f32-to-int casts
+    // f32 <-> int casts
     assert_eq(5.0f32 as u32, 5);
     assert_eq(-5.0f32 as u32, 0);
     assert_eq(5.0f32 as i32, 5);
@@ -44,11 +44,13 @@ fn main() {
     assert_eq(std::f32::NEG_INFINITY as u32, 0);
     assert_eq(std::f32::NAN as i32, 0);
     assert_eq(std::f32::NAN as u32, 0);
-    assert_eq(u128::MAX as f32, std::f32::INFINITY);
     assert_eq((u32::MAX-127) as f32 as u32, u32::MAX); // rounding loss
     assert_eq((u32::MAX-128) as f32 as u32, u32::MAX-255); // rounding loss
+    assert_eq(127i8 as f32, 127.0f32);
+    assert_eq(i128::MIN as f32, -170141183460469231731687303715884105728.0f32);
+    assert_eq(u128::MAX as f32, std::f32::INFINITY); // saturation
 
-    // f64-to-int casts
+    // f64 <-> int casts
     assert_eq(5.0f64 as u64, 5);
     assert_eq(-5.0f64 as u64, 0);
     assert_eq(5.0f64 as i64, 5);
@@ -63,9 +65,11 @@ fn main() {
     assert_eq(std::f64::NEG_INFINITY as u64, 0);
     assert_eq(std::f64::NAN as i64, 0);
     assert_eq(std::f64::NAN as u64, 0);
-    assert_eq(u128::MAX as f64 as u128, u128::MAX);
     assert_eq((u64::MAX-1023) as f64 as u64, u64::MAX); // rounding loss
     assert_eq((u64::MAX-1024) as f64 as u64, u64::MAX-2047); // rounding loss
+    assert_eq(u128::MAX as f64 as u128, u128::MAX);
+    assert_eq(i16::MIN as f64, -32768.0f64);
+    assert_eq(u128::MAX as f64, 340282366920938463463374607431768211455.0f64); // even that fits...
 
     // f32 <-> f64 casts
     assert_eq(5.0f64 as f32, 5.0f32);