I have method that takes 2 integers as arguments and returns:
public int method(int a, int b){
return Math.round((Math.abs(a-b)/3) - (Math.min(a,b)-1500)/10)
}
I have created unit test, which passes for values 1400 and 1500 (expected value is 43), however it fails for 1459 and 1500. The expected output is 18, however my method returns 17.
I believe this might have something to do with rounding, however i cannot see any obvious error. There should not be any problems with rounding 17.7(6) to 18.
EDIT:
the real function was slighty different (I did not have Math.abs(a-b) but instead I had defined “diff” variable as a result of this. I could promise you guys that i declared it as double diff; – I have no idea why it become int diff 🙂 SOLVED thanks 🙂
Math.absandMath.minboth returnints when passedints as arguments, so your code is doing integer division instead of the double division that you are expecting. If you replace the3and the10with3.0and10.0, your code should work as you expect.