Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

double to string causes divide by zero!



Hi

Below is a test case that crashes with a divide by zero when appending
a double to a string. The code basically looks like this, with 2
threads doing it at the same time.

double d = some special double
String s = "";
s+=d;

The exception seems to happen with only certain doubles, one of which
is 14295.604991861095 and with multiple threads. I find it pretty
amazing that something as simple as string appending would not be
thread safe! And, because the vm handles the overloading of the +
operator, I don't think that there is any way for me to syncronize to
avoid this!

The error appears for me on both a 10.2.8 dual processor G5 and a
single processor 10.3.2 G5, but it doesn't seems to happen on 10.3.2 on
a G4. Very strange, could there be something different about the G5
with threads in java? I have put this into radar with bug number
3569904, but thought others might be interested as this is such a
strange bug to exist in the vm.

Here is the output on the 10.2.8 dual G5:
java -cp . TestDouble
double value is 14295.604991861095
/ by zero v value
was:Bits:
0100000011001011111010111100110101110000010111111001000011100000
os.name=Mac OS X
os.version=10.2.8
java.runtime.version=1.4.1_01-68
java.vm.version=1.4.1_01-23
java.vm.name=Java HotSpot(TM) Client VM
java.lang.ArithmeticException: / by zero
at java.lang.FloatingDecimal.dtoa(FloatingDecimal.java:780)
at java.lang.FloatingDecimal.<init>(FloatingDecimal.java:442)
at java.lang.Double.toString(Double.java:135)
at java.lang.String.valueOf(String.java:2326)
at java.lang.StringBuffer.append(StringBuffer.java:600)
at TestDouble$TestThread.doit(TestDouble.java:38)
at TestDouble$TestThread.run(TestDouble.java:30)

And here is the output on a single G5 10.3.2:
java -cp . TestDouble
double value is 14295.604991861095
/ by zero v value
was:Bits:
0100000011001011111010111100110101110000010111111001000011100000
os.name=Mac OS X
os.version=10.3.2
java.runtime.version=1.4.2_03-117.1
java.vm.version=1.4.2-34
java.vm.name=Java HotSpot(TM) Client VM
java.lang.ArithmeticException: / by zero
at java.lang.FloatingDecimal.dtoa(FloatingDecimal.java:767)
at java.lang.FloatingDecimal.<init>(FloatingDecimal.java:436)
at java.lang.StringBuffer.append(StringBuffer.java:630)
at TestDouble$TestThread.doit(TestDouble.java:38)
at TestDouble$TestThread.run(TestDouble.java:30)


Here is the code:
public class TestDouble {

public static void main(String[] args) {
String doubleBits = "0100000011001011111010111100110101110000010111111001000011100000";
long doubleLong = 0l;
for (int i = 0; i < doubleBits.length(); i++) {
if (doubleBits.substring(i, i+1).equals("1")) {
doubleLong += 1l << (63-i);
}
}
double d = Double.longBitsToDouble(doubleLong);
System.out.println("double value is "+d);

int numThreads = 2;
for (int i = 0; i < numThreads; i++) {
TestThread t = new TestDouble.TestThread(d);
t.start();
}
}

static class TestThread extends Thread {
TestThread(double val) {
this.val = val;
}
double val;
public void run() {
while(true){
doit(val);
}
}

public void doit(double v) {

try {
String s = "";
s+=v;
} catch (java.lang.ArithmeticException e) {
long doubleLong = Double.doubleToRawLongBits(v);
String bitString = "Bits:";
for (int i = 63; i >= 0; i--) {
long twoPower = 1l << i;
long tmp = twoPower & doubleLong;
if (tmp == 0l) {
bitString += "0";
} else {
bitString += "1";
}
}
System.err.println(e.getMessage()+" v value was:"+bitString);
System.err.println("os.name="+System.getProperty("os.name"));
System.err.println("os.version="+System.getProperty("os.version"));
System.err.println("java.runtime.version="+System.getProperty("java.runtime.version"));
System.err.println("java.vm.version="+System.getProperty("java.vm.version"));
System.err.println("java.vm.name="+System.getProperty("java.vm.name"));
e.printStackTrace();
System.exit(1);
}
}
}
}
thanks,
Philip
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.