Results 1 to 9 of 9
Thread: floating point errors
 04242011, 02:46 PM #1Member
 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 = (numdegreedegree)*60; // (10.4  10) * 60. I should have 24.00 but I get 23.999977!
why? :(Last edited by mikele83; 04242011 at 03:53 PM.
 04242011, 02:50 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,410
 Blog Entries
 7
 Rep Power
 25
Read this document and you'll know.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 04242011, 03:23 PM #3
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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.
 04242011, 03:27 PM #4Member
 Join Date
 Jan 2011
 Posts
 4
 Rep Power
 0
 04242011, 03:28 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,410
 Blog Entries
 7
 Rep Power
 25
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,
JosBuild a wall around Donald Trump; I'll pay for it.

... 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.
 04242011, 03:47 PM #7Member
 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
 04242011, 03:50 PM #8
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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.
 04242011, 03:52 PM #9Member
 Join Date
 Jan 2011
 Posts
 4
 Rep Power
 0
Similar Threads

Displaying floating point value
By NuxlyStardust in forum New To JavaReplies: 11Last Post: 04142011, 06:40 PM 
Floating point inaccuracy
By Aga in forum Advanced JavaReplies: 6Last Post: 02042011, 06:07 AM 
if it is floating point number
By Stephen Douglas in forum New To JavaReplies: 26Last Post: 07062010, 09:17 AM 
number of floating point
By mohammad8065 in forum Advanced JavaReplies: 5Last Post: 12282008, 10:41 AM 
Floating point values in SWT Spinner
By Java Tip in forum SWT TipsReplies: 0Last Post: 07072008, 04:50 PM
Bookmarks