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

前端 未结 1 1695
夕颜
夕颜 2020-12-12 04:52

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

相关标签:
1条回答
  • 2020-12-12 05:38

    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.

    0 讨论(0)
提交回复
热议问题