>> If all we have is strtod, there's no point doing a long-double compare.

True, UNLESS sscanf("%lg", &x) works: I've had to use that alternative
in the past.

>> ...
>> Even better, just compare the numbers as text, without converting them
>> to internal format.  This is already done for -n, and would be much
>> faster than any of the above approaches.  If done right it would be just
>> as accurate as gmp.  (It wouldn't be trivial, though.)
>> ...

Yes, that would work, but care needs to be taken to handle the formats
that strtold() supports (decimal 1.2345e+678 and C99 hexadecimal
0x1.f3dp-2047), and that essentially mentions having to convert from
hexadecimal to decimal, and effectively deal with many of the same
problems that strtold() handles.  The code also must deal with issues
of leading and trailing zeros in significand and exponent.

Quick: how are these ordered?

        1.00000000000000000000000000000000000000000000000000e-50
        0.00000000000000000000000000000000000000000000000001e+00

Answer: they are equal.

And these:

        +0.00000000000000000000000000000000000000000000000000e-50
        -0.00000000000000000000000000000000000000000000000000e+00

Answer: both are zero, but the negative one should precede the
positive one in sort (on the grounds that negative zero really means
tiny negative that was too small to represent).

So, indeed, a string-based comparison is not trivial.

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- University of Utah                    FAX: +1 801 581 4148                  -
- Department of Mathematics, 110 LCB    Internet e-mail: [email protected]  -
- 155 S 1400 E RM 233                       [email protected]  [email protected] -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe/ -
-------------------------------------------------------------------------------




Reply via email to