• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Math Craziness

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do the following two lines of code equate to 0, vs 5.7 or 5?

System.out.println((12 / 21) * 10);

- and -

int = (int) ((12 /21) * 10);
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Birmingham wrote:Why do the following two lines of code equate to 0, vs 5.7 or 5?

System.out.println((12 / 21) * 10);

- and -

int = (int) ((12 /21) * 10);




Please read about "BODMAS". I am sure you will find your answer yourself.

- (12 /21) -- return 0.57 which is converted to 0 because it is int calculation by default.
- Then 0 will be multiplied by 10 which equals to 0.
- Therefore the answer will be Zero (0)

Note:
- If you change your code to......

System.out.println((12.0 /21) * 10);

Then the result will be what you were expecting.........


 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bhagat Singh Rawat wrote:
- (12 /21) -- return 0.57 which is converted to 0 because it is int calculation by default.



All of your post is correct except for this; 12/21 in int arithmetic is 0, straight off; there's no conversion or rounding involved.
 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:

Bhagat Singh Rawat wrote:
- (12 /21) -- return 0.57 which is converted to 0 because it is int calculation by default.



All of your post is correct except for this; 12/21 in int arithmetic is 0, straight off; there's no conversion or rounding involved.




Yeah! absolutely correct. I couldn't explain it in correct way
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah!!! I didn't realize that Java treated numbers as specific primitive types before we assign them variables and therefore cast them.

Now I know why l or L must be added to longs, and f or F to floats.

Much appreciated.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Birmingham wrote:Ah!!! I didn't realize that Java treated numbers as specific primitive types before we assign them variables and therefore cast them.

Now I know why l or L must be added to longs, and f or F to floats.

Much appreciated.



Just remember that in arithmatic calculations such as that all types are promoted to the largest type in the calculation. So in the following code all numbers are promoted to a double.



Also take notice that if you are going to assign that value to a variable you may need an explicit cast (if the cast is to a smaller primitive type) like so:



Even though the value is small enough to fit in an int, technically, the value is going to be a double and it cannot be directly placed in an int without an explicit cast.
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

W. Joe Smith wrote:Just remember that in arithmatic calculations such as that all types are promoted to the largest type in the calculation.


But they are never smaller than int. For instance:
But:


Also take notice that if you are going to assign that value to a variable you may need an explicit cast (if the cast is to a smaller primitive type) like so:


Surely you mean
Casts go for mathematical operations, so your example would cast 4.5 to int (making it 4), then adding 5.6 to make the result 9.6, which is a double.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:

W. Joe Smith wrote:Just remember that in arithmatic calculations such as that all types are promoted to the largest type in the calculation.


But they are never smaller than int. For instance:
But:


Also take notice that if you are going to assign that value to a variable you may need an explicit cast (if the cast is to a smaller primitive type) like so:


Surely you mean
Casts go for mathematical operations, so your example would cast 4.5 to int (making it 4), then adding 5.6 to make the result 9.6, which is a double.



True. This is why I should never post before I drink at least 1 cup of coffee.
 
You get good luck from rubbing the belly of a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic