安卓(大)失败的浮点值的产品 [英] Android (big) fail on float values product
问题描述
情况drammatic ......它的500天,我必须解决这个问题,我无法脱身。
the situation is drammatic... It's 5 days that i must resolve this problem and i can't get out.
问题:通过简单的操作为一个产品做总是错误的结果。为什么呢?
The problem: a simple operation as a product make always wrong result. Why?
的code萃取:
//all vars are float
// resultVec is a vector [3]
// Rs is a vector [3]
// accelerationvalues is a vector [3]
resultVec[0]=Rs[0]*accelerationvalues[0]+Rs[1]*accelerationvalues[1]+Rs[2]*accelerationvalues[2];
//debug to print result
Log.d("e", "("+Rs[0]+")*("+accelerationvalues[0]+")+("+Rs[1]+")*("+accelerationvalues[1]+")+("+Rs[2]+")*("+accelerationvalues[2]+")="+(resultVec[0]));
这是登录猫的结果: 但是,你可以简单地尝试,这是错误的:搜索谷歌
And this is the Log Cat result: But you can simply try that this is wrong: search on google
(0.040147018)*(-0.9942854)+(0.9984244)*(-0.32688835)+(0.039202508)*(9.343558)
,你会发现,真正的结果是 8.67678679×10-9 这是非常不同于other..This错误总是重复我执行PROGRAMM,一段时间不同的是在标志呢!
And you'll find that the true result is 8.67678679 × 10-9 that is very different from the other..This error is repeated always i execute the programm, some time the difference is in the sign too!
这是什么问题?
我已经尝试了所有解决的方式! (有些是贴在下面):
I've tried all the way to solve it! (some are posted below):
<一个href="http://stackoverflow.com/questions/8030358/android-my-app-mistakes-simple-multiplication/8030571#8030571">You可以在这里找到完整的源代码。
- 保存卢比和accelerationvalues在一个ArrayList和听者之外进行计算。没有结果。
- 转换浮动翻番,没有结果。
- 在许多其他方面
P.S。这个问题occour仅适用于 resultVec [0]
和 resultVec [1]
,而不是 resultVec [ 2]
很好计算。
P.S. This problem occour only for resultVec[0]
and resultVec[1]
, instead resultVec[2]
is well calculated.
推荐答案
这是不是Android的错,它是如何设计的应用程序。
This is not android's fault, it is how you designed the app.
在一个普通的Java应用程序执行这样的:
Execute this in a plain Java application:
public class ArithmTest {
public static void main(String[] args) {
double d1 = (0.040147018)*(-0.9942854)+(0.9984244)*(-0.32688835)+(0.039202508)*(9.343558);
System.out.println(d1);
float f1 = 0.040147018f;
float f2 = -0.9942854f;
float f3 = 0.9984244f;
float f4 = -0.32688835f;
float f5 = 0.039202508f;
float f6 = 9.343558f;
System.out.println(f1*f2 + f3*f4 + f5*f6);
}
}
正如你所看到的,第一个是相同的谷歌的,第二个是打印你的应用程序的价值。
为了解决这个问题,我认为你应该使用浮动双,而不是在你声明的每一个变量,例如: accelerationvalues
和 resultVec
As you can see, the first one is the same as Google's, and the second printout is your app's value.
To solve this, I think you should use double instead of float in every variable you declared, e.g.: accelerationvalues
and resultVec
.
这篇关于安卓(大)失败的浮点值的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!