Comparing doubles
Alan Lehotsky
apl@alum.mit.edu
Mon Jul 1 12:18:00 GMT 2002
At 8:15 PM +0200 7/1/02, Lars Brinkhoff wrote:
>Does the C standard define exactly how floating-point comparisons
>should work? Sections 6.5.8 Relational operators and 6.5.9 Equality
>operators in C99 doesn't spell out any details.
>
>In particular, would it be acceptable for a GCC back end to compare
>two doubles by subtracting them, converting the result to float, and
>then compare the result against zero?
I don't quite remember what PDP10 doubles were like, but if you don't
have NANs, then using subtraction is probably a viable approach. You
might look at how this is implemented in the softfloat libraries. It
is probably reasonable to check the signs, check the exponents and
then as a last resort check the magnitudes. The problem with using
subtract is that you may trigger underflow exceptions or have an
undetected underflow interpreted as equality when it isn't.
If you can run paranoia successfully, you probably have dealt with
the whole class of comparison problems that are possible....
-- Al
--
Quality Software Management
http://home.earthlink.net/~qsmgmt
apl@alum.mit.edu
(978)287-0435 Voice
(978)808-6836 Cell
Software Process Improvement / Management Consulting
Language Design / Compiler Implementation
More information about the Gcc
mailing list