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!



Philip Crotwell wrote:
| 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!

How do you know it's the appending that's the problem? Far more likely, it's the conversion of double to String. From the language spec (2nd ed.):

17.4 Nonatomic Treatment of double and long

If a double or long variable is not declared volatile, then for the
purposes of load, store, read, and write actions they are treated as if
they were two variables of 32 bits each: wherever the rules require one
of these actions, two such actions are performed, one for each 32-bit
half ... The load, store, read, and write actions on volatile variables
are atomic, even if the type of the variable is double or long.

This matters only because a read or write of a double or long variable
may be handled by an actual main memory as two 32-bit read or write
actions that may be separated in time, with other actions coming
between them. Consequently, if two threads concurrently assign distinct
values to the same shared non-volatile double or long variable, a
subsequent use of that variable may obtain a value that is not equal to
either of the assigned values, but some implementation-dependent
mixture of the two values.


Glen Fisher
_______________________________________________
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.