[Java]double初始化问题
如下:
1. 直接初始化
double[][] embossFilter = {{-1/9, 0, 1/9}, {-1/9, 1/9, 1/9}, {-1/9, 0, 1/9}};
2. 赋值初始化
double l = 1/9;
double[][] linearFilter = {{l, l, l}, {l, l, l}, {l, l, l}};
以上两种初始化结果均得到0.0,正确方法如下:
double l = (double)1/9, b1 = (double)1/13, b2 = (double)5/13;
double[][] linearFilter = {{l, l, l}, {l, l, l}, {l, l, l}};
注意:两个整数无论如何运算,在赋值给double时一定要强制类型转换!否则两个数的计算结果首先保存为int型,然后才会被转化成double!这样的double只是int的另一种存储类型而已!