首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓基础数学不及格

安卓基础数学不及格
EN

Stack Overflow用户
提问于 2012-06-18 07:56:17
回答 2查看 275关注 0票数 0

可能重复:

Java float division precision

我正在尝试修复我的游戏中的一个bug,并需要计算出最佳的瓷砖大小。为此,我获取设备的屏幕分辨率、该级别的行数和列数,然后计算允许我的游戏使用最大屏幕空间的瓷砖大小。然而,Android在基本数学上似乎失败了,在简单的除法操作上失去了精确性。下面是适当的代码:

代码语言:javascript
复制
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×800):

代码语言:javascript
复制
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!!

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-18 08:02:22

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

既然您正在比较一个比另一个大,为什么不直接比较displayAreatileArea呢?这比玩浮点操作要好得多。

票数 2
EN

Stack Overflow用户

发布于 2012-06-18 08:02:38

似乎类型转换问题,尝试抛出您的displayArea浮动。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11078879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档