1

So today at school, we were learning some of the math classes in java, but I don't particularly understand this why it automatically rounds from -11.87 to -12.

import java.util.*;
public class println{
 public static void main (String [] args){
 System.out.println(8 % 3 / 15 - 12);
 }
}
asked Jan 22, 2016 at 15:27

4 Answers 4

4

It does not "round up". The steps done here are pretty simple:

  • 8%3 is evaluated. the modulo operator % returns the rest of the integer-division 8/3 (so it returns 2)
  • 2 / 15 is evaluated. Both 2 and 15 are integers (int) in java. Integer division will cut off any decimal places. So this expression will be evaluated to 0!
  • 0 - 12 is evaluated. Result is -12
answered Jan 22, 2016 at 15:32
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the explanation helped me alot!
1

The reason for this is that all of the numbers you provided in the expression are in the form of non floating point numbers. Because of this, the JVM does not process the expressions with floating point numbers.

8 % 3 = 2
2 / 15 = 0
0 -ひく 12 = -ひく12

This is how the operation actually proceeds due to the fact that none of the numbers are floating point numbers (e.g. double).

answered Jan 22, 2016 at 15:31

5 Comments

please don't say it rounds anything up!!! Java doesn't round anything unless you tell it to (with Math.round()). It just cuts off the digits behind the decimal dot!
Sorry. Let me rephrase my answer then.
@ParkerHalo The exact result of this operation is -11.866666666666667. In your terms, the result should be -11, not -12.
@bernhardcolby no, % and / get evaluated before - !
He was referring to the fact that you mentioned that Java cuts off the decimal places. If you process the whole thing as a double then just cut off the decimal places rather than rounding them off, you get a floating point answer of -11.867 which according to your statement, should give a final int value of -11.
0

In order to understand this, you should understand the structure of integer and double in Java. For example, if you code like that, it will not "round up".

public class println {
public static void main(String[] args) {
 System.out.println(8.0 % 3.0 / 15.0 - 12.0);
}}

Because, the numbers, you used are integers (without any decimal). If you give the numbers in double form, the program gives you the exact result, which is -11.866666666666667.

answered Jan 22, 2016 at 15:34

2 Comments

You only need to force the first number to double. That would cause all remaining values to be widened to double implicitly.
You are right. I just wanted to clarify the answer.
0

Your expresion is ugual at this:

 System.out.println(((8 % 3) / 15) - 12);

First evaluated

 (8 % 3) = 2 

And

 (2/15) = 0

And finally

 0-12 =-12
answered Jan 22, 2016 at 15:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.