You are performing integer division in Java, where math done using int
s must yield an int
, so the division truncates anything beyond the decimal point. This means that in Java, -2 / 5
is 0
. It's not enough that the target of the assignment is a double
. One of the operands must be double
(or float
) to force floating-point arithmetic.
Cast one of the operands to the /
operator to a double
to force floating-point arithmetic.
slope2 = ( (double) (yVals[i+2] - originY) / (xVals[i+2] - originX));