Comments (3)
Rust follow IEEE 754 all is specified here. (but I have no idea if this will answer you)
from approx.
@dsroche does the code below illustrate your point? I'm not that familiar with relative floating point comparisons:
fn relative_eq(lhs: f32, rhs: f32, epsilon: f32, max_relative: f32) -> bool {
// Handle same infinities
if lhs == rhs {
return true;
}
// Handle remaining infinities
if f32::is_infinite(lhs) || f32::is_infinite(rhs) {
return false;
}
let abs_diff = f32::abs(lhs - rhs);
// For when the numbers are really close together
if abs_diff <= epsilon {
return true;
}
let abs_lhs = f32::abs(lhs);
let abs_rhs = f32::abs(rhs);
let largest = if abs_rhs > abs_lhs { abs_rhs } else { abs_lhs };
println!("This part is never reached");
// Use a relative difference comparison
abs_diff <= largest * max_relative
}
fn main() {
let diff = 1.0e-7;
let x: f32 = 1.0e-5;
let y: f32 = x - diff;
println!(
"relative_eq!(x, y) = {}",
relative_eq(x, y, f32::EPSILON, f32::EPSILON) // true
);
println!(
"abs_diff <= largest * max_relative = {}",
(x - y).abs() <= 1.0e-5 * f32::EPSILON, // false
);
}
So one has to pass their own epsilon in order for it to work for small values? E.g:
use approx::relative_eq;
relative_eq!(x, y, epsilon = 1.0e-8f32)
from approx.
Oooh thanks for the report! I may have got something wrong in the implementation - it's been a long time since I looked inside. And to be honest floating point numerics are not something I'm an expert on. I'd be interested if we can find a fix for this though!
from approx.
Related Issues (20)
- Automatically deriving implementations HOT 9
- Adding messages to assert_* macros HOT 2
- Release another version HOT 2
- Overflow on absolute comparison of signed integers HOT 3
- Not working at all? HOT 3
- Improve Documentation HOT 2
- Support for slices? HOT 4
- Please tag releases HOT 1
- Unecessary std feature HOT 1
- Release a 1.0 version HOT 3
- Inconsistent temporary value behavior with `assert_eq!`
- clippy::if_then_panic warning (nightly) HOT 3
- Maintenance status HOT 3
- Transferral of repository HOT 1
- Give acceptable range on assertion failure
- Changelog for version updates would be nice
- `impl RelativeEq for Option<T: RelativeEq>`? HOT 1
- `assert_relative_eq!()` fails in compilation
- impl RelativeEq for NotNan and OrderedFloat from ordered_float
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from approx.