gpt4 book ai didi

java - Android 基础数学不及格

转载 作者:行者123 更新时间:2023-12-02 07:44:31 26 4
gpt4 key购买 nike

Possible Duplicate:
Java float division precision

我正在尝试修复我的一款游戏中的错误,并且需要计算最佳的图 block 大小。为此,我获取设备的屏幕分辨率、该级别的行数和列数,然后计算允许我的游戏使用最大屏幕空间的图 block 大小。然而,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!!

在计算器上计算比率会导致精度损失。有人知道这是为什么吗?我的实现有问题吗?

最佳答案

从打印输出来看,displayAreatileArea似乎被声明为intlong。在将结果提升为 float 类型并分配给 ratio 之前,您正在执行整数除法 (displayArea/tileArea) >.

既然要比较一个是否比另一个大,为什么不直接比较 displayAreatileArea 呢?这比玩弄浮点运算要好得多。

关于java - Android 基础数学不及格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11078879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com