Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertions for floating-point comparisons #69

Open
ghost opened this issue Jul 26, 2015 · 3 comments
Open

Assertions for floating-point comparisons #69

ghost opened this issue Jul 26, 2015 · 3 comments

Comments

@ghost
Copy link

ghost commented Jul 26, 2015

It would be nice if testworks provide at least one method to compare floating-point numbers

@waywardmonkeys
Copy link
Member

I need this as well now.

Previously, @niko2342 had found https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ which seems like a good start.

@waywardmonkeys
Copy link
Member

To compare ULPs, you can use decode-single-float or decode-double-float from dylan-extensions:

let desired-lo = decode-double-float(desired);
let calc-lo = decode-double-float(calc);
assert-true(abs(as(<integer>, desired-lo) - as(<integer>, calc-lo)) < 2)

@waywardmonkeys
Copy link
Member

@housel pointed me at this code from Gwydion Dylan:

define constant $max-ulps = 3.0d0;

define method almost-equal
    (x :: <float>, y :: <real>)
 => (result :: <boolean>)
  if (y.zero?)
    abs(x) < scale-float($max-ulps, 1 - float-digits(x));
  else
    let (x-sig, x-exp, x-sign) = decode-float(x);
    let scaled-diff = abs(scale-float(x, - x-exp) - scale-float(y, - x-exp));
    let ulp-diff = scale-float(scaled-diff, float-digits(x) - 1);

    ulp-diff < $max-ulps;
  end if;
end method almost-equal;

@cgay cgay added this to the Prioritization Test milestone Dec 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants