Re:Mean of two floating point numbers can be dangerous
by CamperBob2 on Sunday June 05, 2016 @03:51PM
(#11842771)
Your fix requires a lot of mental CPU cycles on the part of whoever reads the code. Are you sure it wouldn't have been better to do it the simple, obvious way?
double a = *(double *) dpa;
double b = *(double *) dpb;
if (a > b) return 1;
if (b < a) return -1;
return 0;
Re:Mean of two floating point numbers can be dangerous
by sijoe on Monday June 06, 2016 @11:50AM
(#11847649)
Oh my gosh ... the fortran computed goto is back ... See http://www.lahey.com/docs/lfprohelp/F95ARComputed_GOTOStmt.h...
Re:Mean of two floating point numbers can be dangerous
by rwmj on Sunday June 05, 2016 @04:18PM
(#11842905)
Yup I think you are right -- the (fixed) code is very obscure now.
Re:Mean of two floating point numbers can be dangerous
by huhtenberg on Sunday June 05, 2016 @04:52PM
(#11843052)
I think it's actually quite elegant. The issue is that it always does two comparisons, even when one would suffice.
Re:Mean of two floating point numbers can be dangerous
by Someone on Sunday June 05, 2016 @08:53PM
(#11844004)
Depending on how the compiler computes a>b, it may be branchless, though, and I expect that to be more important on modern CPUs.
Re:Mean of two floating point numbers can be dangerous
by titzer on Sunday June 05, 2016 @05:24PM
(#11843213)
That code returns 0 for all NaN inputs.
Re:Mean of two floating point numbers can be dangerous
by CamperBob2 on Sunday June 05, 2016 @06:42PM
(#11843539)
True enough, but if you're passing NaNs to qsort(), something has already gone wrong in your professional life.
Re:Mean of two floating point numbers can be dangerous
by Bromskloss on Sunday June 05, 2016 @10:53AM
(#11841243)
How come the argument types are void* instead of double*?
(Spoiler alert) the fix was: https://github.com/libguestfs/libguestfs/commit/1f4a0bd90df3...