Bug#192035: gcj-3.2: Numerical computation differs from Sun javac/java
Package: gcj-3.2
Version: 1:3.2.3-0pre9
Severity: important
I have the following program:
/**
* @author Jens M%/1??iso8859-15üller
*
*/
/*
* Created on 05.05.2003
*
*/
/**
* @author Jens M%/1??iso8859-15üller
*
*/
public class PiApproximationRecursive {
/**
* This variable specifies the default number of recursion steps, if none was given on the
* command line.
*/
public static int DefaultNumberOfRecursionSteps = 1000;
/**
* @param numberOfSteps Number of steps for approximation.
* @return The approximated value of the number pi.
*/
public static double calculatePi(int numberOfSteps) {
return 4 * (doPositiveRecursionStep(0, 1, numberOfSteps));
}
private static double doPositiveRecursionStep(
double currentValue,
int currentDivisor,
int stepsToGo) {
if (stepsToGo == 0) {
return currentValue;
} else {
double newValue = currentValue + 1.0 / currentDivisor;
int newDivisor = currentDivisor + 2;
return doNegativeRecursionStep(newValue, newDivisor, stepsToGo - 1);
}
}
private static double doNegativeRecursionStep(
double currentValue,
int currentDivisor,
int stepsToGo) {
if (stepsToGo == 0) {
return currentValue;
} else {
double newValue = currentValue - 1.0 / currentDivisor;
int newDivisor = currentDivisor + 2;
return doPositiveRecursionStep(newValue, newDivisor, stepsToGo - 1);
}
}
/**
* @param args The first command line argument, if given, specifies the number of recursion steps.
* Otherwise, the default value is 2000.
*
*/
public static void main(String[] args) {
int numberOfRecursions = DefaultNumberOfRecursionSteps;
if (args.length >= 1) {
try {
numberOfRecursions = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
// do nothing, keep default value
}
double pi = calculatePi(numberOfRecursions);
System.out.println(pi);
System.out.println("Approximierter Wert f%/1??iso8859-15ür pi: "+pi);
}
}
}
I compiled it with java from Sun (Blackdown) JDK 1.4.1, and ran it
with java from the same package:
jens@debian:~/studium/semester2/info2/ue1java$ java PiApproximationRecursive 20000
3.1415426535898248
Approximierter Wert für pi: 3.1415426535898248
I also compiled it with
gcj --main=PiApproximationRecursive PiApproximationRecursive.java
I got this result:
jens@debian:~/studium/semester2/info2/ue1java$ ./a.out 20000
3.1415426535898208
Approximierter Wert für pi: 3.1415426535898208
I think that the Java Language Standard clearly specifies the order in
which expressions are evaluated. The result of a single floating point
operation is specified in the respective IEEE standards.
So either gcj's or JDK's behavior is broken here ...
I suppose the former.
-- System Information
Debian Release: testing/unstable
Kernel Version: Linux debian 2.4.20-k7 #1 Tue Jan 14 00:29:06 EST 2003 i686 unknown unknown GNU/Linux
Versions of the packages gcj-3.2 depends on:
ii gcc-3.2 3.2.3-0pre9 The GNU C compiler
ii gcc-3.2-base 3.2.3-0pre9 The GNU Compiler Collection (base package)
ii java-common 0.19 Base of all Java packages
ii libc6 2.3.1-16 GNU C Library: Shared libraries and Timezone
ii libgcj3 3.2.3-0pre9 Java runtime library for use with gcj
ii libgcj3-dev 3.2.3-0pre9 Java development headers and static library
ii zlib1g 1.1.4-11 compression library - runtime
Reply to: