• Post Reply Bookmark Topic Watch Topic
  • New Topic

Remainder of a double  RSS feed

 
John Doyled
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've just started a computer science course and am studying java and was wondering about a program I have written.

My program is here...



Now the output of this will be 1.2. That's fine. But if I change the program to this...



adding one more line of 'result += num2 * num1;' the output becomes 1.7999999999999998

I can't understand why I have the remainder.

Thanks to anyone who can help.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John, welcome to the Ranch!

Your question is a common one, frequently asked in fact, so check out our Java Beginners FAQ. You might not recognize it right away, but #20 on that page is what you're looking for.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of people teach you to use doubles for arithmetic, but that only applies when you are an engineer. Engineers don't mind the difference between 3.99 and 4. Mathematicians and bankers do mind. You can find what you should really use for arithmetic in this thread, and what kinds of arithmetic there are here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!