# a little confused on floating points...

• 05-24-2012, 03:10 AM
SnakeDoc
a little confused on floating points...
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); } }```
the above code outputs:

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```
Shouldn't the sum equal 100.00 if for a thousand times we add 0.1 from a base value of 0.0?

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):

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

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!
• 05-24-2012, 03:38 AM
Junky
Re: a little confused on floating points...
Google "what every computer scientist needs to know about floating point arithmetic"
• 05-24-2012, 05:43 AM
DarrylBurke
Re: a little confused on floating points...
Quote:

Originally Posted by SnakeDoc
So the oracle site says use of floating point numbers is innacurate (and should not be used for currency especially)

Correct.

Quote:

Originally Posted by SnakeDoc
then the java-samples.com website says they are accurate (and should be used for things such as currency)...

Never heard of that site, and if that's what they say I hope I never hear of it again.

Why do you feel you should be influenced by a clearly advertisement-driven, privately-run site of little or no significance? Most of those sites are pure garbage, except for those that steal copyright content and publish it as their own. There are more than enough reputed sites where you can find the information you need.

db
• 05-24-2012, 08:21 AM
SnakeDoc
Re: a little confused on floating points...
ok thanks... i thought that it was a little funny lol. can you recommend any sites in particular that are good for newbies? (besides oracle's site... i know its the end-all-be-all, but i'm looking for something more... explanitory like textbook style i guess).

thanks for any pointers...
• 05-24-2012, 09:07 AM
Junky
Re: a little confused on floating points...
Quote:

Originally Posted by SnakeDoc
can you recommend any sites in particular that are good for newbies?

I did in my first post.
• 05-24-2012, 10:17 AM
Tolls
Re: a little confused on floating points...
0.1 cannot be represented precisely in binary, in the same way 1/3 cannot be represented precisely in decimal.
So, if I have a decimal 1/3 to 3 decimal places (as an example) and add three of those together I get 0.999.
It's exactly the same concept with your binary float.
But do Junky's Google, because the article that comes up is the one often referred to whenever this topic comes up.
• 05-24-2012, 06:02 PM
SnakeDoc
Re: a little confused on floating points...
ok thanks Tolls! your explanation is precisely what i was looking for. (and sorry Junky, didn't realize you were actually referring me to an article, I wrongly assumed you were just telling me to "Go Google It" lol, which I have and got that awful java-samples.com site lol) -- i'll be sure to check it out.

thanks again guys