gpt4 book ai didi

java - 是转换整个表达式更好,还是只转换不同类型的变量更好

转载 作者:太空狗 更新时间:2023-10-29 13:41:45 24 4
gpt4 key购买 nike

我正在为一些 Android Java 游戏图形使用 float ,但数学库触发函数都返回 double 值,因此我必须显式转换它们。

我知道 float 比 double 处理起来更快,而且我不需要高精度答案。

例如哪个更好:

screenXf = (float) (shipXf + offsetXf * Math.sin(headingf) - screenMinXf);

screenXf = shipXf + offsetXf * (float) (Math.sin(headingf)) - floatScreenMinXf;

我想其他问题是“我如何在没有其他因素(例如 PC 服务)混淆问题的情况下在模拟器上测试它?”以及“它在不同的硬件上会有所不同吗?”

哦,天哪,这是三个问题。生活从来都不简单:-(

-弗林克

最佳答案

考虑使用 FloatMath.sin()相反。

float

Math routines similar to those found in Math. Performs computations on float values directly without incurring the overhead of conversions to and from double.


但请注意 android 文档中的这个简介:

http://developer.android.com/guide/practices/design/performance.html#avoidfloat

Designing for Performance
...
In speed terms, there's no difference between float and double on the more modern hardware. Space-wise, double is 2x larger. As with desktop machines, assuming space isn't an issue, you should prefer double to float.

虽然这个人@fadden,据称是编写 VM 的人之一,说:

Why are there so many floats in the Android API?

On devices without an FPU, the single-precision floating point ops are much faster than the double-precision equivalents. Because of this, the Android framework provides a FloatMath class that replicates some java.lang.Math functions, but with float arguments instead of double.

On recent Android devices with an FPU, the time required for single- and double-precision operations is about the same, and is significantly faster than the software implementation. (The "Designing for Performance" page was written for the G1, and needs to be updated to reflect various changes.)

他的最后一句话(“page ... need to be updated”)指的是我上面引用的页面,所以我想知道他是不是指我上面引用的关于“没有区别”的那句话。

关于java - 是转换整个表达式更好,还是只转换不同类型的变量更好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4903239/

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