Double.toString()

Steve Ratcliffe sterat at dial.pipex.com
Sat Oct 24 05:06:39 PDT 1998


Hi,

I believe that in 1.0 Double.toString() was defined to be what %g did,
and that is what kaffe does now.  In 1.1 there is a new definition and
so I wrote some test cases for it.

However I found that even the JDK didn't pass them all so I am asking for
input from the list on what is correct, or any more information.

When I run the following tests under JDK 1.1.6 under linux, there
are two issues:

The first test has an extra 0 at the end.  Perhaps that is correct,
but I don't see why.  Why does it just happen with numbers such
as 0.001 and not 0.01?

The second issue is that the doc that I have says <= 1E7 but actual
behaviour is < 1E7.

..Steve
------

import java.io.PrintStream;

/**
 * Tests Double.toString() against the 1.1 spec.
 * <p>
 * According to spec:
 * 1E-3 <= |d| <= 1E7 are printed in [-]ddd.ddd style with just
 * enough digits after the decimal point to distiguish
 * from adjacent values of type double but at least one digit.
 * Outside that range, in the style [-]m.dddE+-xx
 *
 * (Before 1.1 jdk it was %g this is what kaffe does now)
 */
public class TestDoubleString {

	static PrintStream o = System.out;

	/**
	 * Takes a double, formats it to a string and compares
	 * with the expected string.
	 */
	static void testIt(double d, String expected) {
		String ds;
		boolean passed = false;

		ds = Double.toString(d);
		if (expected.equals(ds) == true) {
			o.print("PASS  ");
			passed = true;
		} else
			o.print("FAIL  ");
		o.print(" Got " + ds);
		if (!passed)
			o.print(", Expected " + expected);
		o.println();
	}

	public static void main(String [] argv) {
		double d;

		//
		// According to spec:
		// 1E-3 <= |d| <= 1E7 are printed in [-]ddd.ddd style with just
		// enough digits after the decimal point to distiguish
		// from adjacent values of type double but at least one digit.
		// Outside that range, in the style [-]m.dddE+-xx
		//
		// (Before 1.1 jdk it was %g this is what kaffe does now)

		// 1. The extremes
		testIt(1E-3, "0.001");
		testIt(1E7, "10000000");

		// 2. Just inside
		testIt(1.01E-3, "0.00101");
		testIt(9.9909123E6, "9990912.3");

		// 3. Just outside
		testIt(9.99E-4, "9.99E-4");
		testIt(1.01E7, "1.01E7");

		// 3b. Well outside
		testIt(3.23e-30, "3.23E-30");
		testIt(3.23e30, "3.23E30");

		// 4. Always at least one digit after point
		testIt(100, "100.0");
		testIt(2E20, "2.0E20");

		// 5. Miscellaneous
		testIt(3.00000001, "3.00000001");
		testIt(3.00000000001, "3.00000000001");
		testIt(3.000000000000001, "3.000000000000001");
		testIt(0.02, "0.02");
	}
}


More information about the kaffe mailing list