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
266 views
in Technique[技术] by (71.8m points)

precision - Is specifying floating-point type sufficient to guarantee same results?

I'm writing a specification that describes some arithmetic that will be performed by software. The intention is that I can hand this spec to two different programmers (who use potentially different languages and/or architectures) and when I feed some input into their programs, they will both always spit out the same result.

For instance, if the spec says "add 0.5 to the result", this can be a problem. Depending on the floating point storage method, 0.5 could be represented as 0.4999999135, or 0.500000138, etc.

What is the best way to specify the rules here so that things are consistent across the board? Can I just say "All numbers must be represented in IEEE 754 64-bit format"? Or is it better to say something like "All numbers must be first scaled by 1000 and computed using fixed-point arithmetic"?

This is a little different from most floating-point questions I've come across since the issue is repeatability across platforms, not the overall precision.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

IEEE 754-2008 clause 11 describes what is necessary for reproducible floating-point results. This is largely:

  • Bindings from the programming language to IEEE 754 operations (e.g., a*b performs the floating-point multiplication specified by IEEE 754).
  • Ways to specify that reproducible results are desired. E.g., disable default language permissions to use wider precision than the nominal types of objects.
  • Accurate conversions to and from external decimal character sequences.
  • Avoiding some of the fancier features of IEEE 754.

These requirements are poorly supported in today’s compilers.

Adding .5 will not be a problem. All normal floating-point implementations represent .5 exactly and add it correctly. What will be a problem is that a language implementation may add .5 and keep the result precisely (more precisely than a usual double) while another implementation rounds the result to double. If math library routines are used (such as cos and log), that is another problem, because they are hard to compute well, and different implementations provide different approximations.

IEEE 754 is a good specification. Ideally, you would specify that implementations of your specification conform to IEEE 754.


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

1.4m articles

1.4m replys

5 comments

57.0k users

...