• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Remainder of a double

 
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.
 
Marshal
Posts: 24594
55
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.
 
Marshal
Posts: 64710
226
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!