2017-09-18 12:47:33 +02:00
|
|
|
|
|
|
|
|
2015-05-06 10:01:49 +02:00
|
|
|
|
2017-05-17 14:19:44 +02:00
|
|
|
#![warn(float_cmp)]
|
2017-08-29 14:48:19 +02:00
|
|
|
#![allow(unused, no_effect, unnecessary_operation, cast_lossless)]
|
2015-09-02 10:30:11 +02:00
|
|
|
|
2015-05-06 10:01:49 +02:00
|
|
|
use std::ops::Add;
|
|
|
|
|
|
|
|
const ZERO : f32 = 0.0;
|
|
|
|
const ONE : f32 = ZERO + 1.0;
|
|
|
|
|
|
|
|
fn twice<T>(x : T) -> T where T : Add<T, Output = T>, T : Copy {
|
2015-08-11 20:22:20 +02:00
|
|
|
x + x
|
2015-05-06 10:01:49 +02:00
|
|
|
}
|
|
|
|
|
2015-09-02 10:30:11 +02:00
|
|
|
fn eq_fl(x: f32, y: f32) -> bool {
|
|
|
|
if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fl_eq(x: f32, y: f32) -> bool {
|
|
|
|
if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
|
|
|
|
}
|
|
|
|
|
|
|
|
struct X { val: f32 }
|
|
|
|
|
|
|
|
impl PartialEq for X {
|
|
|
|
fn eq(&self, o: &X) -> bool {
|
|
|
|
if self.val.is_nan() {
|
|
|
|
o.val.is_nan()
|
|
|
|
} else {
|
|
|
|
self.val == o.val // no error, inside "eq" fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 10:01:49 +02:00
|
|
|
fn main() {
|
2015-08-17 12:06:56 +02:00
|
|
|
ZERO == 0f32; //no error, comparison with zero is ok
|
2015-11-10 11:19:33 +01:00
|
|
|
1.0f32 != ::std::f32::INFINITY; // also comparison with infinity
|
|
|
|
1.0f32 != ::std::f32::NEG_INFINITY; // and negative infinity
|
2015-08-17 12:06:56 +02:00
|
|
|
ZERO == 0.0; //no error, comparison with zero is ok
|
2015-11-10 11:19:33 +01:00
|
|
|
ZERO + ZERO != 1.0; //no error, comparison with zero is ok
|
2015-08-11 20:22:20 +02:00
|
|
|
|
2016-06-29 19:47:51 +02:00
|
|
|
ONE == 1f32;
|
2016-06-29 21:25:23 +02:00
|
|
|
ONE == 1.0 + 0.0;
|
|
|
|
ONE + ONE == ZERO + ONE + ONE;
|
2016-06-29 19:47:51 +02:00
|
|
|
ONE != 2.0;
|
2015-08-17 12:06:56 +02:00
|
|
|
ONE != 0.0; // no error, comparison with zero is ok
|
2016-06-29 19:47:51 +02:00
|
|
|
twice(ONE) != ONE;
|
|
|
|
ONE as f64 != 2.0;
|
2015-08-17 12:06:56 +02:00
|
|
|
ONE as f64 != 0.0; // no error, comparison with zero is ok
|
2015-08-11 20:22:20 +02:00
|
|
|
|
|
|
|
let x : f64 = 1.0;
|
|
|
|
|
2016-06-29 19:47:51 +02:00
|
|
|
x == 1.0;
|
2015-08-17 12:06:56 +02:00
|
|
|
x != 0f64; // no error, comparison with zero is ok
|
2015-08-11 20:22:20 +02:00
|
|
|
|
2016-06-29 19:47:51 +02:00
|
|
|
twice(x) != twice(ONE as f64);
|
2017-02-08 14:58:07 +01:00
|
|
|
|
2015-08-17 12:06:56 +02:00
|
|
|
x < 0.0; // no errors, lower or greater comparisons need no fuzzyness
|
2015-08-11 20:22:20 +02:00
|
|
|
x > 0.0;
|
|
|
|
x <= 0.0;
|
|
|
|
x >= 0.0;
|
2016-06-25 18:59:37 +02:00
|
|
|
|
|
|
|
let xs : [f32; 1] = [0.0];
|
|
|
|
let a: *const f32 = xs.as_ptr();
|
|
|
|
let b: *const f32 = xs.as_ptr();
|
|
|
|
|
2017-02-17 09:32:51 +01:00
|
|
|
assert_eq!(a, b); // no errors
|
2015-05-06 10:01:49 +02:00
|
|
|
}
|