Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Mathematical Evaluation  RSS feed

 
Joseph Mokenela
Ranch Hand
Posts: 71
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem here. I don't understand why the output of the following program is 7.0. Mathematically, its supposed to be 8.0 Am I missing something here?



Thanks in advance!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why using double?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are working with int's and expecting them to act like double's:

The evaluation goes like this:

y/z = 1 (1.5 double, but they are not doubles they are int's)

1 * k = 2

x + 2 = 7, so the final answer is 7.
 
Joseph Mokenela
Ranch Hand
Posts: 71
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, I had forgotten that the 0.5 will get truncated.

Much appreciated!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!