Dividing two integers to a double in java

This line here d = w[L] /v[L]; takes place over several steps

d = (int)w[L]  / (int)v[L]
d=(int)(w[L]/v[L])            //the integer result is calculated
d=(double)(int)(w[L]/v[L])    //the integer result is cast to double

In other words the precision is already gone before you cast to double, you need to cast to double first, so

d = ((double)w[L])  / (int)v[L];

This forces java to use double maths the whole way through rather than use integer maths and then cast to double at the end

Leave a Comment