Comments on Java Numerics
Fred J. Tydeman
tydemanatybor.com
Sun Feb 23 21:17:40 PST 1997
On Tue, 4 Feb 97 21:36:10 PST, David G. Hough at validgh wrote:
> Java conversions from binary floating-point to decimal string
>representations are intended to be correctly rounded but are somewhat limited
>in releases so far. Correctly-rounded base conversion is an important
>aspect of portable identical results.
The following little test shows just how limited those conversions are.
The three implementations where this has been run all print out '1' for
both '1' and '1+eps'.
/*
* The Java API documentation says that a floating-point value will be
* converted to a string with enough digits so that it can be distinguished
* from all other floating-point values with different bit patterns, in
* particular, the adjacent floating-point values.
*
* This program tests that requirement by printing the adjacent values
* 1.0 and 1.0+eps. eps is about 2.220446e-16
*
* -- Fred Tydeman, tydeman a tybor.com
*/
class testprnt {
public static void main(String args[]){
double one;
double onep1;
double eps;
long li;
one = 1.0;
li = Double.doubleToLongBits(one);
li++; /* add one to unit in last place to get to adjacent number */
onep1 = Double.longBitsToDouble(li);
eps = onep1 - one;
System.out.println("1 is " + Double.toString(one));
System.out.println("eps is " + Double.toString(eps));
System.out.println("1+eps is " + Double.toString(onep1));
System.out.println("Did the above line print '1' or '1.00000000000000022e0'?");
}/* main */
}/* class testprnt */
---
Fred J. Tydeman +49 (7031) 288-964 Tydeman Consulting
Meisenweg 20 tydemanatybor.com Programming, testing, C/C++ training
D-71032 Boeblingen Voting member of X3J11 (ANSI "C")
Germany Sample FPCE tests: ftp://ftp.netcom.com/pub/ty/tydeman
More information about the Numeric-interest
mailing list