且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在java中将两个整数除以一个double

更新时间:2022-12-09 17:12:19

这里的这一行 d = w[L]/v[L]; 发生在几个步骤中

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];

这会强制 java 在整个过程中使用双精度数学,而不是使用整数数学,然后在最后强制转换为双精度

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