Re: Sorting numeric strings
On Wed, 02 May 2012 14:36:29 -0700, Roedy Green
<see_website@mindprod.com.invalid> wrote:
On Tue, 01 May 2012 15:02:09 -0700, Patricia Shanahan <pats@acm.org>
wrote, quoted or indirectly quoted someone who said :
Whether comparing doubles gives the correct result does depend on the
maximum number of significant digits. BigDecimal is always safe for
this, no matter how many significant digits.
A float will do for about 7 significant digits. A double will do for
16.
A currency amount can be shown with a double to the penny up to 99
trillion dollars
99,000,000,000,000.00,
If you are thinking of IEEE 754 64-bit floating point, well, it
has ~15.95 decimal digits of precision so it is not quite good enough
for 16 digits. 15 digits is fine though. (The 32-bit vesion is good
for ~7.22 digits.)
Here is a counterexample in JavaScript. Adding 8765432101234567
and 1234567890123456 gives 9999999991358024 which is one too high. I
get the same result in Java (except that it is in E-notation) with:
***** Start of Code *****
class tmp
{
public static void main(String [] args)
{
double a=8765432101234567d;
double b=1234567890123456d;
System.out.println(a);
System.out.println(b);
System.out.println(a+b);
}
}
***** End of Code *****
I researched in this area when I was figuring out how to handle
fixed-point numbers using JavaScript's number object which uses IEEE
754 64-bit floating point.
Sincerely,
Gene Wirchenko