1. Member
Join Date
Jan 2011
Posts
4
Rep Power
0

## [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. Read this document and you'll know.

kind regards,

Jos

3. 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. Member
Join Date
Jan 2011
Posts
4
Rep Power
0
Originally Posted by sunde887
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.

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

5. Originally Posted by sunde887
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

6. Originally Posted by mikele83

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. Member
Join Date
Jan 2011
Posts
4
Rep Power
0
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. 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. Member
Join Date
Jan 2011
Posts
4
Rep Power
0
Originally Posted by sunde887
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 ;)

#### Posting Permissions

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