Mailing Lists: Apple Mailing Lists

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

RE: double to string causes divide by zero!



> -----Original Message-----
> From: Philip Crotwell [mailto:email@hidden]
> Sent: Friday, February 27, 2004 7:02 AM
> To: email@hidden
> Subject: double to string causes divide by zero!
>
>...

> 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!

It's a simple *looking* expression, but it's not that simple to the
compiler. The compiler basically translates s+=d to...

s = s + new Double(d).toString();



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!

You can use a synchronized block:

synchronized (this) {
double d = some special double
String s = "";
s+=d;
}

Rob
_______________________________________________
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.


References: 
 >double to string causes divide by zero! (From: Philip Crotwell <email@hidden>)



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.