# floating point errors

• 04-24-2011, 02:46 PM
mikele83
[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? :(
• 04-24-2011, 02:50 PM
JosAH
Read this document and you'll know.

kind regards,

Jos
• 04-24-2011, 03:23 PM
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.
• 04-24-2011, 03:27 PM
mikele83
Quote:

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.

link to those 2 post?

the initial link provided is too complex, I don't understand the reason of the floating point error.
• 04-24-2011, 03:28 PM
JosAH
Quote:

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
• 04-24-2011, 03:36 PM
Fubarable
Quote:

Originally Posted by mikele83
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.
• 04-24-2011, 03:47 PM
mikele83
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
• 04-24-2011, 03:50 PM
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.
• 04-24-2011, 03:52 PM
mikele83
Quote:

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