llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

fmod in APFloat is badly broken

Open llvmbot opened this issue 17 years ago • 4 comments

Bugzilla Link 3359
Resolution FIXED
Resolved on Feb 12, 2020 00:55
Version trunk
OS NetBSD
Reporter LLVM Bugzilla Contributor
CC @ekatz
Fixed by commit(s) rG9d0956ebd471

Extended Description

Just some of the things wrong with it:

a) It takes a rounding mode. The result is always exact, so a rounding mode should not be an argument.

b) It doesn't handle specials (NaN, zero, infinity).

c) It gets standard cases wrong, e.g.

#include <math.h> #include <stdio.h>

int main (void) { float x = 0x1.345672p30; float y = 0x1.56789ap-20; float z1 = fmod (x, y), z2 = remainder (x,y );

printf ("%a %a: fmod = %a remainder = %a\n", x, y, z1, z2);

return 0;
};

gives on NetBSD:

0x1.345672p+30 0x1.56789ap-20: fmod = 0x1.0f7dbap-20 remainder = -0x1.1beb8p-22

and for APFloat:

int main (void) { char dst[30];

APFloat x (APFloat::IEEEsingle, "0x1.345672p+30"); APFloat y (APFloat::IEEEsingle, "0x1.56789ap-20");

x.convertToHexString (dst, 0, false, APFloat::rmNearestTiesToEven); printf ("x: %s\n", dst); y.convertToHexString (dst, 0, false, APFloat::rmNearestTiesToEven); printf ("y: %s\n", dst); x.fmod(y, APFloat::rmNearestTiesToEven); x.convertToHexString (dst, 0, false, APFloat::rmNearestTiesToEven); printf ("mod: %s\n", dst);

return 0; }

gives:

x: 0x1.345672p30 y: 0x1.56789ap-20 mod: 0x0p0

llvmbot avatar Jan 20 '09 13:01 llvmbot

Fix, for the remainder() implementation, is available in review https://reviews.llvm.org/D69776.

ekatz avatar Nov 06 '19 09:11 ekatz

I've fixed this to handle the special cases. The rest of it is going to have to wait.

llvmbot avatar Jan 22 '09 00:01 llvmbot

OK, this should help. To clarify, we're talking about APFloat::mod which implements C fmod (or llvm frem), not IEEE remainder. I'm not sure I buy that it doesn't need a rounding mode. C99 F.9.7.1 defines it as IEEE remainder (which is always exact) conditionally followed by an add. Can't the rounding mode affect the add?

llvmbot avatar Jan 20 '09 20:01 llvmbot

assigned to @ekatz

llvmbot avatar Jan 20 '09 13:01 llvmbot