Giter Club home page Giter Club logo

Comments (3)

Stargateur avatar Stargateur commented on May 29, 2024

Rust follow IEEE 754 all is specified here. (but I have no idea if this will answer you)

from approx.

imjasonmiller avatar imjasonmiller commented on May 29, 2024

@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.

brendanzab avatar brendanzab commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.