2

The following simple floating point arithmetic operation is not working as expected.

double den = (1+j);
System.out.println(den);
den = 1/den;
System.out.println(den);

double newden = 1/(1+j);
System.out.println(newden);

The above code gives the following output.

7.0
0.14285714285714285
0.0

As shown above, the first two operations work as expected but the last doesn't. I suppose that it has something to do with the variable type but still haven't figured out the problem.

Can you please explain the behaviour of arithmetic operations in Java?

Tom
  • 16,842
  • 17
  • 45
  • 54
user3540466
  • 303
  • 1
  • 2
  • 13
  • 2
    `1/(1+j)` is an `int` calculation and will leave you with an `int` result, while `1/den` is a `double` calculation (because `den` is a `double`), which will leave you with a `double`. (Just guessing for `j` beeing an `int` as it´s not visible what it represents here) – SomeJavaGuy Oct 06 '16 at 13:02
  • I guess j is an integer variable. So the whole expression `1/(1+j)` is threated to be an integer too. You can cast `j` to double to get the expected result. `newden = 1/(1+((double)j))` – kaetzacoatl Oct 06 '16 at 13:03
  • ahhh got it `j` is just a counter. But why is `1/(1+j)` is an int calculation if I am declaring a double? – user3540466 Oct 06 '16 at 13:05
  • okay thanks! got it – user3540466 Oct 06 '16 at 13:05
  • @user3540466 because `1` is an `int` aswell as `j`, so the result of the calculation is an `int`, which will then be assigned to the `double`. The types of the calculation decides the output, not the type you are assigning to. – SomeJavaGuy Oct 06 '16 at 13:06

3 Answers3

8

An arithmetic operation in java is done in int if there is no float/double/long involved. Hence, change one of the arg to float/double and it works as expected.

The following code would work:

double newden = 1d/(i+j);
System.out.println(newden);

From the java spec:

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules, in order:

  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.
Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86
  • 3
    why using floats when you want a double? You can simply use `1d/(1+j)` – kaetzacoatl Oct 06 '16 at 13:05
  • @kaetzacoatl changed – Pankaj Singhal Oct 06 '16 at 13:06
  • Minor correcture: "The arithmetic operations in java is done in int if there is no float/double involved" - AND no `long`. This statement is then pretty accurate since every type < `int` is promoted to int. Even `byte + byte` will yield an `int`. – Turing85 Oct 06 '16 at 13:11
  • @Turing85 thanks for suggesting the change. updated the answer. – Pankaj Singhal Oct 06 '16 at 13:14
  • Please post text as text, and not as an image. Also, the first point is misleading, and possibly wrong altogether; an operation on two floats does not promote them and in fact returns a float, even if the unseen intermediate operation may convert the values to doubles under the hood. – VGR Oct 06 '16 at 16:54
  • @VGR updated with appropriate reference. – Pankaj Singhal Oct 06 '16 at 17:39
3

This is working. If you try this by putting (double) 1/(1+j); there, you can see the result:

int j = 1;
        double den = (1+j);
        System.out.println(den);
        den = 1/den;
        System.out.println(den);

        double newden = (double) 1/(1+j);
        System.out.println(newden);

Here, your output will be:

2.0
0.5
0.5
Vijaya Pandey
  • 4,252
  • 5
  • 32
  • 57
2

i guess j value is 6 so

1/(1+6) will give you 0 considering int and assigning it to double convert it into 0.0

Solution : cast any of value to double

double newden = (double)1/(1+j);

or

double newden = 1.0/(1+j);

or without casting

double newden =1;
newden /=(1+j);

Read the awesome official doc about narrowing and Widening primitive conversion

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68