News for hackers. Stuff that matters.
Re:Pretty.c by slimsag on Thursday October 24, 2024 @02:11AM (#41932438)
it uses absolute difference epsilon equality ('close enough to be considered equal'):

    static int pretty_float_equal (float a, float b) { return fabsf(a - b) < FLT_EPSILON; }
    static int pretty_double_equal (double a, double b) { return fabs(a - b) < DBL_EPSILON; }
    static int pretty_long_double_equal (long double a, long double b) { return fabsl(a - b) < LDBL_EPSILON; }
Re:Pretty.c by vbezhenar on Thursday October 24, 2024 @02:21AM (#41932502)
This is wrong code. It only works somewhat correctly when a and b around 1.
Re:Pretty.c by stabbles on Thursday October 24, 2024 @02:26AM (#41932525)
Yeah, should be scaled like |x - y| <= ε * max(|x|, |y|)
Re:Pretty.c by aartaka on Thursday October 24, 2024 @07:11AM (#41934350)
Will do.
Re:Pretty.c by jffhn on Thursday October 24, 2024 @08:01AM (#41934672)
If both terms are infinites and of same sign, subtraction will give NaN and it will fail.
Re:Pretty.c by listeria on Thursday October 24, 2024 @10:08AM (#41935646)
How is that a problem? infinities shouldn't be considered equal
Re:Pretty.c by jffhn on Thursday October 24, 2024 @12:12PM (#41936911)
For IEEE 754, and in Java for example, they are. Only NaN is not equal to itself (and different from itself).