janlindso
janlindso

Reputation: 1239

Calculation gives different results

I'm trying to calculate a formula in Android:

percent = absence/(week*(37/period))*100;

The value for absence is 4, week is 2, and period is 2. percent returns 11.111111.

But if I do the same in Excel with formula 4/(2*(37/2))*100, it returns 10.81.

Can anyone explain me what might be wrong?

Upvotes: 0

Views: 228

Answers (2)

Andrew Li
Andrew Li

Reputation: 57964

It's because of integer division.

The value for absence is 4, week is 2, and period is 2. percent returns 11.111111.

This is because 37 / 2 is 18.5, and the fractional part is thrown out because of integer division. That gives 18, which multiplied by 2 is 36. Next we divide 4 by 36, which is 0.111... That multiplied by 100 is 11.111....

But if I do the same in Excel with formula 4/(2*(37/2))*100, it returns 10.81.

This is because Excel doesn't use integer division. 37 / 2 is 18.5, 18.5 * 2 is 37. 4 / 37 is 0.108108108... Thus multiplying by 100 gives approximately 10.81.


Explanation

The reason it's doing integer division is because you have an integer period. Java sees that there are two integers and are being divided so it uses integer division. This causes all fractional parts to be erased, meaning the 0.5 from 18.5 resulting from 37 / 2 is removed, making the results different.

Fix

You can 'fix' this by making at least one of the operands in the division float or double:

double percent = absence/(week*((double) 37/period))*100;

What this will do is achieve floating-point division giving 10.8108...

Upvotes: 1

anacron
anacron

Reputation: 6721

Declare all of the variables as double and it will work as you expect:

    public static void main(String[] args) {
        double period = 2;
        double absence = 4;
        double week = 2;
        double percent = absence /(week *(37/period ))*100;
        System.out.println("percent: " + percent);
    }

Gives the output:

percent: 10.81081081081081

If your period variable is declared as an int, your output will change to:

percent: 11.11111111111111

This is becuase you're losing precision due to rounding down when you divide 37 by the int period. If you change it to double, it will give you the expected result.

Upvotes: 0

Related Questions