2015-05-06 03:01:49 -05:00
|
|
|
#![feature(plugin)]
|
|
|
|
#![plugin(clippy)]
|
|
|
|
|
2015-09-02 03:30:11 -05:00
|
|
|
#![deny(float_cmp)]
|
2016-05-13 09:43:47 -05:00
|
|
|
#![allow(unused, no_effect, unnecessary_operation)]
|
2015-09-02 03:30:11 -05:00
|
|
|
|
2015-05-06 03:01:49 -05: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 13:22:20 -05:00
|
|
|
x + x
|
2015-05-06 03:01:49 -05:00
|
|
|
}
|
|
|
|
|
2015-09-02 03:30:11 -05: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 03:01:49 -05:00
|
|
|
fn main() {
|
2015-08-17 05:06:56 -05:00
|
|
|
ZERO == 0f32; //no error, comparison with zero is ok
|
2015-11-10 04:19:33 -06:00
|
|
|
1.0f32 != ::std::f32::INFINITY; // also comparison with infinity
|
|
|
|
1.0f32 != ::std::f32::NEG_INFINITY; // and negative infinity
|
2015-08-17 05:06:56 -05:00
|
|
|
ZERO == 0.0; //no error, comparison with zero is ok
|
2015-11-10 04:19:33 -06:00
|
|
|
ZERO + ZERO != 1.0; //no error, comparison with zero is ok
|
2015-08-11 13:22:20 -05:00
|
|
|
|
2016-06-29 12:47:51 -05:00
|
|
|
ONE == 1f32;
|
|
|
|
//~^ ERROR strict comparison of f32 or f64
|
|
|
|
//~| HELP within some error
|
|
|
|
//~| SUGGESTION (ONE - 1f32).abs() < error
|
|
|
|
ONE == (1.0 + 0.0);
|
|
|
|
//~^ ERROR strict comparison of f32 or f64
|
|
|
|
//~| HELP within some error
|
|
|
|
//~| SUGGESTION (ONE - (1.0 + 0.0)).abs() < error
|
|
|
|
|
|
|
|
ONE + ONE == (ZERO + ONE + ONE);
|
|
|
|
//~^ ERROR strict comparison of f32 or f64
|
|
|
|
//~| HELP within some error
|
|
|
|
//~| SUGGESTION (ONE + ONE - (ZERO + ONE + ONE)).abs() < error
|
|
|
|
|
|
|
|
ONE != 2.0;
|
|
|
|
//~^ ERROR strict comparison of f32 or f64
|
|
|
|
//~| HELP within some error
|
|
|
|
//~| SUGGESTION (ONE - 2.0).abs() < error
|
2015-08-17 05:06:56 -05:00
|
|
|
ONE != 0.0; // no error, comparison with zero is ok
|
2016-06-29 12:47:51 -05:00
|
|
|
twice(ONE) != ONE;
|
|
|
|
//~^ ERROR strict comparison of f32 or f64
|
|
|
|
//~| HELP within some error
|
|
|
|
//~| SUGGESTION (twice(ONE) - ONE).abs() < error
|
|
|
|
ONE as f64 != 2.0;
|
|
|
|
//~^ ERROR strict comparison of f32 or f64
|
|
|
|
//~| HELP within some error
|
|
|
|
//~| SUGGESTION (ONE as f64 - 2.0).abs() < error
|
2015-08-17 05:06:56 -05:00
|
|
|
ONE as f64 != 0.0; // no error, comparison with zero is ok
|
2015-08-11 13:22:20 -05:00
|
|
|
|
|
|
|
let x : f64 = 1.0;
|
|
|
|
|
2016-06-29 12:47:51 -05:00
|
|
|
x == 1.0;
|
|
|
|
//~^ ERROR strict comparison of f32 or f64
|
|
|
|
//~| HELP within some error
|
|
|
|
//~| SUGGESTION (x - 1.0).abs() < error
|
2015-08-17 05:06:56 -05:00
|
|
|
x != 0f64; // no error, comparison with zero is ok
|
2015-08-11 13:22:20 -05:00
|
|
|
|
2016-06-29 12:47:51 -05:00
|
|
|
twice(x) != twice(ONE as f64);
|
|
|
|
//~^ ERROR strict comparison of f32 or f64
|
|
|
|
//~| HELP within some error
|
|
|
|
//~| SUGGESTION (twice(x) - twice(ONE as f64)).abs() < error
|
2015-08-11 13:22:20 -05:00
|
|
|
|
2015-08-17 05:06:56 -05:00
|
|
|
|
|
|
|
x < 0.0; // no errors, lower or greater comparisons need no fuzzyness
|
2015-08-11 13:22:20 -05:00
|
|
|
x > 0.0;
|
|
|
|
x <= 0.0;
|
|
|
|
x >= 0.0;
|
2016-06-25 11:59:37 -05:00
|
|
|
|
|
|
|
let xs : [f32; 1] = [0.0];
|
|
|
|
let a: *const f32 = xs.as_ptr();
|
|
|
|
let b: *const f32 = xs.as_ptr();
|
|
|
|
|
|
|
|
assert!(a == b); // no errors
|
2015-05-06 03:01:49 -05:00
|
|
|
}
|