English 中文(简体)
浮动点业务的不同结果
原标题:Different results for the floating point operations
  • 时间:2012-04-15 22:34:25
  •  标签:
  • java

我的小米有小问题,我有一套从特定数据中计算平均值的代码,我从几台不同的计算机(有些有气球,有些有窗户,约32个轨道,其他64个轨道)这样做,但在我观察数据库时,某些应同等的数值略有不同。

This makes no difference for my program but I was wondering why it was this way, I was under the impression that floating point operations with the same word lenght should yield the same result (I was obviously wrong).

这是我写来的小型样本代码,用于检查:

public class Test{
    public static void main(String... args){
        double counter = 0, value = 1./10;

        for (int i = 0; i < 1000000; i++){
            counter += value;
        }

        System.out.println(counter);
    }
}

It basically calculates 1000000 / 10, I know that the result won t be exact but in some pcs it prints a number that is slightly less than 100000 and in other cases it prints a number slightly greater.

最佳回答
问题回答

我认为这取决于所使用的汇编技术。 例如,如果没有优化,你会增加很多内容,从而可能失去精确性,而汇编者可以理解所涉业务,整个航程可替换为<代码>counter = 价值*1000000<>。

<代码>杜布尔的精确度及其操作准确用文字确定

不同的板块形式具有代表浮动点的不同方式。

为了取得同样的结果,你应当把精确性限制在一定数量的精度上。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签