可能重复:
Java 浮点除法精度
我正在尝试修复我的一个游戏中的错误,并且需要为此计算最佳图块大小。为此,我获取设备的屏幕分辨率、该级别的行数和列数,然后计算允许我的游戏使用最大屏幕空间的图块大小。然而,Android 似乎在基本数学上失败了,在简单的除法运算上失去了精度。这是适当的代码:
while(!foundIt)
{
int tileArea = boxes*i*i; //Boxes = 20*26
float ratio = displayArea/tileArea; //displayArea = width*height
Log.d("Balance", displayArea + "/" + tileArea);
Log.d("Balance", boxes + ", " + i);
Log.d("Balance", "Ratio: " + ratio);
int last = 0;
if(ratio>1)
{
if(last==LAST_WAS_LESS_THAN_ONE)
{
i--;
foundIt=true;
}
last = LAST_WAS_MORE_THAN_ONE;
}
else if(ratio==1)
{
Log.d("Balance", "Found a perfect fit!!");
foundIt=true;
}
else if(ratio<1)
{
if(last==LAST_WAS_MORE_THAN_ONE)
{
i--;
foundIt=true;
}
last = LAST_WAS_LESS_THAN_ONE;
}
if(!foundIt)
{
i++;
}
}
这是在我的 Nexus S 上运行的示例(分辨率:480 x 800):
06-18 13:20:45.808: D/Balance(1069): Width: 480 Height: 800
06-18 13:20:45.808: D/Balance(1069): 384000/520
06-18 13:20:45.808: D/Balance(1069): 520, 1
06-18 13:20:45.808: D/Balance(1069): Ratio: 738.0
06-18 13:20:45.808: D/Balance(1069): 384000/2080
06-18 13:20:45.808: D/Balance(1069): 520, 2
06-18 13:20:45.808: D/Balance(1069): Ratio: 184.0
06-18 13:20:45.808: D/Balance(1069): 384000/4680
06-18 13:20:45.808: D/Balance(1069): 520, 3
06-18 13:20:45.808: D/Balance(1069): Ratio: 82.0
06-18 13:20:45.808: D/Balance(1069): 384000/8320
06-18 13:20:45.808: D/Balance(1069): 520, 4
06-18 13:20:45.808: D/Balance(1069): Ratio: 46.0
06-18 13:20:45.808: D/Balance(1069): 384000/13000
06-18 13:20:45.808: D/Balance(1069): 520, 5
06-18 13:20:45.808: D/Balance(1069): Ratio: 29.0
06-18 13:20:45.808: D/Balance(1069): 384000/18720
06-18 13:20:45.808: D/Balance(1069): 520, 6
06-18 13:20:45.808: D/Balance(1069): Ratio: 20.0
06-18 13:20:45.808: D/Balance(1069): 384000/25480
06-18 13:20:45.808: D/Balance(1069): 520, 7
06-18 13:20:45.808: D/Balance(1069): Ratio: 15.0
06-18 13:20:45.808: D/Balance(1069): 384000/33280
06-18 13:20:45.808: D/Balance(1069): 520, 8
06-18 13:20:45.808: D/Balance(1069): Ratio: 11.0
06-18 13:20:45.808: D/Balance(1069): 384000/42120
06-18 13:20:45.808: D/Balance(1069): 520, 9
06-18 13:20:45.808: D/Balance(1069): Ratio: 9.0
06-18 13:20:45.808: D/Balance(1069): 384000/52000
06-18 13:20:45.808: D/Balance(1069): 520, 10
06-18 13:20:45.808: D/Balance(1069): Ratio: 7.0
06-18 13:20:45.808: D/Balance(1069): 384000/62920
06-18 13:20:45.808: D/Balance(1069): 520, 11
06-18 13:20:45.808: D/Balance(1069): Ratio: 6.0
06-18 13:20:45.808: D/Balance(1069): 384000/74880
06-18 13:20:45.808: D/Balance(1069): 520, 12
06-18 13:20:45.808: D/Balance(1069): Ratio: 5.0
06-18 13:20:45.808: D/Balance(1069): 384000/87880
06-18 13:20:45.808: D/Balance(1069): 520, 13
06-18 13:20:45.808: D/Balance(1069): Ratio: 4.0
06-18 13:20:45.808: D/Balance(1069): 384000/101920
06-18 13:20:45.808: D/Balance(1069): 520, 14
06-18 13:20:45.808: D/Balance(1069): Ratio: 3.0
06-18 13:20:45.808: D/Balance(1069): 384000/117000
06-18 13:20:45.808: D/Balance(1069): 520, 15
06-18 13:20:45.808: D/Balance(1069): Ratio: 3.0
06-18 13:20:45.808: D/Balance(1069): 384000/133120
06-18 13:20:45.812: D/Balance(1069): 520, 16
06-18 13:20:45.812: D/Balance(1069): Ratio: 2.0
06-18 13:20:45.816: D/Balance(1069): 384000/150280
06-18 13:20:45.816: D/Balance(1069): 520, 17
06-18 13:20:45.816: D/Balance(1069): Ratio: 2.0
06-18 13:20:45.816: D/Balance(1069): 384000/168480
06-18 13:20:45.816: D/Balance(1069): 520, 18
06-18 13:20:45.816: D/Balance(1069): Ratio: 2.0
06-18 13:20:45.816: D/Balance(1069): 384000/187720
06-18 13:20:45.816: D/Balance(1069): 520, 19
06-18 13:20:45.816: D/Balance(1069): Ratio: 2.0
06-18 13:20:45.816: D/Balance(1069): 384000/208000
06-18 13:20:45.816: D/Balance(1069): 520, 20
06-18 13:20:45.816: D/Balance(1069): Ratio: 1.0
06-18 13:20:45.816: D/Balance(1069): Found a perfect fit!!
在计算器上计算比率会导致精度下降。有人知道这是为什么吗?我的实现有问题吗?