Results 1 to 7 of 7
  1. #1
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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);
    }
    }
    the above code outputs:

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


    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

    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!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: a little confused on floating points...

    Google "what every computer scientist needs to know about floating point arithmetic"

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: a little confused on floating points...

    Quote Originally Posted by SnakeDoc View Post
    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 View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

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

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: a little confused on floating points...

    Quote Originally Posted by SnakeDoc View Post
    can you recommend any sites in particular that are good for newbies?
    I did in my first post.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default 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 **

  7. #7
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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

Similar Threads

  1. Replies: 1
    Last Post: 08-11-2011, 10:46 AM
  2. Replies: 5
    Last Post: 05-10-2011, 10:55 PM
  3. Replies: 6
    Last Post: 11-12-2009, 10:50 AM
  4. a navigation bar that has floating images
    By shayz in forum Advanced Java
    Replies: 3
    Last Post: 02-07-2009, 01:52 PM
  5. Replies: 2
    Last Post: 08-24-2008, 12:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •