是否指定足以保证相同结果的浮点类型? [英] Is specifying floating-point type sufficient to guarantee same results?

查看:134
本文介绍了是否指定足以保证相同结果的浮点类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个规范,描述一些将由软件执行的算术。我的意图是,我可以把这个规范交给两个不同的程序员(他们使用不同的语言和/或体系结构),当我向他们的程序中提供一些输入时,他们总是会吐出相同的结果。

例如,如果规范说将0.5加到结果,这可能是一个问题。取决于浮点存储方法,0.5可以表示为0.4999999135或0.500000138等。



这里指定规则的最好方法是什么全线?我只能说所有数字都必须以 IEEE 754 64位格式表示?还是说所有的数字必须先由1000缩放并使用定点算术计算这样的话会更好吗?



这与我所遇到的大多数浮点问题有一点不同,因为问题是跨平台的可重复性,而不是整体精度。 IEEE 754-2008第11章描述了可重复浮点结果的必要条件。这主要是:
$ b $ ul

  • 从编程语言到IEEE 754操作的绑定(例如, a * b 执行由IEEE 754规定的浮点乘法)。
  • 指定需要可重现结果的方法。例如,禁用默认的语言权限,以使用比名义类型的对象更宽的精度。
  • 准确地转换为外部的十进制字符序列。
  • 避免IEEE 754的一些特点。

    $ b

    这些要求在今天的编译器中得到了很好的支持。



    添加.5不会是一个问题。所有正常的浮点实现代表.5,并正确添加它。什么是问题是一个语言实现可能会添加.5,并精确地保留结果(比通常的 double 更精确),而另一个实现将结果舍入为。如果使用数学库例程(例如 cos log ),那是另一个问题,因为它们很难计算得很好,不同的实现提供了不同的近似值。

    <75> IEEE 754是一个很好的规范。理想情况下,您可以指定您的规范的实现符合IEEE 754。


    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.

    解决方案

    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.

    这篇关于是否指定足以保证相同结果的浮点类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆