Results 1 to 7 of 7
 05242012, 03:10 AM #1Senior Member
 Join Date
 Apr 2012
 Posts
 129
 Rep Power
 0
a little confused on floating points...
Java 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); } }
Java 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 floatingpoint 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):
•
float: The float data type is a singleprecision 32bit 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 doubleprecision 64bit 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
float
The type float specifies a singleprecision 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 highspeed 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 largevalued 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 javasamples.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!
 05242012, 03:38 AM #2
Re: a little confused on floating points...
Google "what every computer scientist needs to know about floating point arithmetic"
 05242012, 05:43 AM #3
Re: a little confused on floating points...
Correct.
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 advertisementdriven, privatelyrun 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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 05242012, 08:21 AM #4Senior Member
 Join Date
 Apr 2012
 Posts
 129
 Rep Power
 0
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 endallbeall, but i'm looking for something more... explanitory like textbook style i guess).
thanks for any pointers...
 05242012, 09:07 AM #5
 05242012, 10:17 AM #6Moderator
 Join Date
 Apr 2009
 Posts
 12,401
 Rep Power
 21
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.Please do not ask for code as refusal often offends.
** This space for rent **
 05242012, 06:02 PM #7Senior Member
 Join Date
 Apr 2012
 Posts
 129
 Rep Power
 0
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 javasamples.com site lol)  i'll be sure to check it out.
thanks again guys
Similar Threads

[jFreeChart] Converting screenpoints to points, which are relative to the axis
By fyaxic in forum AWT / SwingReplies: 1Last Post: 08112011, 10:46 AM 
Graphics with floating points instead of Integers
By rdjava in forum Java 2DReplies: 5Last Post: 05102011, 10:55 PM 
The code isnt working unless I add print statements at diffrent points!:confused:
By Addez in forum New To JavaReplies: 6Last Post: 11122009, 11:50 AM 
a navigation bar that has floating images
By shayz in forum Advanced JavaReplies: 3Last Post: 02072009, 02:52 PM 
given number of points(cordinates) , find max points lie on the same line ?
By Hayzam in forum New To JavaReplies: 2Last Post: 08242008, 12:30 AM
Bookmarks