the above code outputs:Code:`class Sum {`

public static void main (String[] args) {

double sum = 0.0d;

boolean isEqualSum = false;

boolean isEqualLess = false;

boolean isEqualMore = false;

for (int count = 1; count <= 1000; count++) {

sum += 0.1;

}

if (sum == 100) {

isEqualSum = true;

}

if (sum < 100) {

isEqualLess = true;

}

if (sum > 100) {

isEqualMore = true;

}

System.out.println("The sum is: " + sum);

System.out.println("Is the sum equal to 100? " + isEqualSum);

System.out.println("Is the sum equal to < 100? " + isEqualLess);

System.out.println("Is the sum equal to > 100? " + isEqualMore);

}

}

Shouldn't the sum equal 100.00 if for a thousand times we add 0.1 from a base value of 0.0?Code:`C:\JavaSpace>java Sum`

The sum is: 99.9999999999986

Is the sum equal to 100? false

Is the sum equal to < 100? true

Is the sum equal to > 100? false

I've tried to understand the floating-point number concept and the double and float types in java... however i'm afraid i've only become more confused...

from oracle.com (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html):

but this website seems to contradict the information listed above, however their code example works out to the same value I calculate on my calculator.Quote:

•

float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

•

double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

float vs double data types in Java

Quote:

float

The type float specifies a single-precision value that uses 32 bits of storage. Single precision is faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small. Variables of type float are useful when you need a fractional component, but don't require a large degree of precision. For example, float can be useful when representing dollars and cents.

Here are some example float variable declarations:

float hightemp, lowtemp;

double

Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. When you need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers, double is the best choice.

~~~~~

So the oracle site says use of floating point numbers is innacurate (and should not be used for currency especially) -- then the java-samples.com website says they are accurate (and should be used for things such as currency)... which is it? And why is my simple addition equation not equaling what i'm expecting? - I've heard a little about BigDecimal class and that it may be better to use for accuracy... but i'd still like to understand why.

Thanks!