Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

java - Double上的数学运算结果是否完全可重复?(Are results from math operations on Double exactly repeatable?)

I have a complex data processing algorithm implemented using Java 8 and using Double as data type.

(我有一个使用Java 8并使用Double作为数据类型实现的复杂数据处理算法。)

Given the same input (many hundred thousand database rows) this algorithm outputs different Double values.

(给定相同的输入(数十万个数据库行),此算法将输出不同的Double值。)

Sometimes value a is returned, sometimes value b .

(有时返回,有时值b。)

These two values alternate each execution.

(这两个值交替执行。)

The difference between a and b is ~0.0001.

(ab之间的差异为?0.0001。)

I am aware that Double datatype does not grant exact values like Decimal does.

(我知道Double数据类型不会像Decimal那样授予确切的值。)

I am not sure, however, if it grants repeatable results, assuming exactly same input.

(我不确定,但是,如果输入完全相同,它是否可以提供可重复的结果。)

Ie would it be possible that rounding policies are applied in a non-consistent way?

(是否有可能以不一致的方式应用舍入策略?)

My purpose here would be to explain why I get different values with same input.

(我的目的是解释为什么我用相同的输入得到不同的值。)

Other details: I am using Tomcat 8 runtime environment, deployment done on SAP HCP.

(其他详细信息:我正在使用Tomcat 8运行时环境,在SAP HCP上完成了部署。)

Datatype at database level is Decimal , we need Double at Java level for historical reasons.

(数据库级别的数据类型为Decimal ,出于历史原因,我们需要Java级别的Double。)

  ask by MoZZoZoZZo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Given the same input, floating point number calculations (on the same platform) will result in the same output.

(给定相同的输入,浮点数计算(在同一平台上)将得到相同的输出。)

However, you may get different results if you feed in the input numbers in different orders (even though that would not make a difference mathematically).

(但是,如果以不同的顺序输入输入数字,则可能会得到不同的结果(即使这在数学上不会有什么不同)。)

Maybe that is what is happening here (seeing that you pull the numbers out of a relational database, which has no defined order unless you explicitly sort).

(也许这就是这里发生的情况(看到您将数字从关系数据库中拉出,除非您明确排序,否则关系数据库没有定义的顺序)。)

Same thing can happen if you have parallel computations using multiple CPU and the data is divided and combined differently each time.

(如果您使用多个CPU进行并行计算,并且每次数据分割和组合的方式不同,则可能会发生相同的情况。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...