Results 1 to 9 of 9
  1. #1
    mikele83 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Question [SOLVED] floating point errors

    Hi to all! I want to convert a numeric degree (e.g. 10.4) in sexagesimal form (10 24')

    i do:

    float numdegree=10.4;
    int degree = (int) numdegree; // I obtain 10 ---OK
    float minute = (numdegree-degree)*60; // (10.4 - 10) * 60. I should have 24.00 but I get 23.999977!

    why? :(
    Last edited by mikele83; 04-24-2011 at 03:53 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default

    Read this document and you'll know.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That document is quite lengthy if it's the one im assuming it is. However; it's worth at least looking through to pick up some knowledge about why floating point numbers aren't exact. There are also two posts, one here, one at javaprogramming.com which have people giving a short breakdown of some stuff the link explains.

  4. #4
    mikele83 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    That document is quite lengthy if it's the one im assuming it is. However; it's worth at least looking through to pick up some knowledge about why floating point numbers aren't exact. There are also two posts, one here, one at javaprogramming.com which have people giving a short breakdown of some stuff the link explains.
    link to those 2 post?

    the initial link provided is too complex, I don't understand the reason of the floating point error.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sunde887 View Post
    That document is quite lengthy if it's the one im assuming it is. However; it's worth at least looking through to pick up some knowledge about why floating point numbers aren't exact. There are also two posts, one here, one at javaprogramming.com which have people giving a short breakdown of some stuff the link explains.
    Breaking news: reading doesn't make your eyeballs bleed. But for the lazy folks overhere: assume a deck of cards (say 52 of them) numbered 1/2, 1/4, 1/8, 1/16 etc. One card of each. Pick as many cards from the deck as you want, add up their numbers and try to form the number 1/10 (or 4/10 for that matter). You can't; nobody can and a computer using the IEEE754 floating point number system can't do it either.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mikele83 View Post
    link to those 2 post?

    the initial link provided is too complex, I don't understand the reason of the floating point error.
    ... and it's not an error. This is how floating points work, and is why you shouldn't use them for financial calculations. I suggest you reread the links critically, and if specific points confuse you, please ask about them here, and we'll be glad to help. The understanding that the articles can give you is priceless.

  7. #7
    mikele83 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    solved!

    thanks to you all.

    solution:

    double numdegree= 10.4;
    int degree = (int) numdegree; // I obtain 10 ---OK
    float minute = (float) (numdegree - Math.floor(numdegree));
    minute*=60; // = 24.0

    :D

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad you solved it, please mark your thread solved with the thread tools at the top. Also, that link will give you some nice knowledge so I suggest you read it over time to have a deeper understanding of problems that can occur.

  9. #9
    mikele83 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Glad you solved it, please mark your thread solved with the thread tools at the top. Also, that link will give you some nice knowledge so I suggest you read it over time to have a deeper understanding of problems that can occur.
    Of course, i will read it ;)

Similar Threads

  1. Displaying floating point value
    By NuxlyStardust in forum New To Java
    Replies: 11
    Last Post: 04-14-2011, 06:40 PM
  2. Floating point inaccuracy
    By Aga in forum Advanced Java
    Replies: 6
    Last Post: 02-04-2011, 05:07 AM
  3. if it is floating point number
    By Stephen Douglas in forum New To Java
    Replies: 26
    Last Post: 07-06-2010, 09:17 AM
  4. number of floating point
    By mohammad8065 in forum Advanced Java
    Replies: 5
    Last Post: 12-28-2008, 09:41 AM
  5. Floating point values in SWT Spinner
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:50 PM

Posting Permissions

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