Prolog实现通过“浮动”来表示什么? [英] What do Prolog implementations mean by "float"?

查看:155
本文介绍了Prolog实现通过“浮动”来表示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看SICStus手册的语法描述,并且有一个float的定义。然而,没有迹象表明浮动实际上是什么。 IEEE单精度或双精度?也许甚至是一个 BigDecimal

在SWI Prolog(或至少 SWISH ),浮动似乎是IEEE双精度,可以通过以下方式确定:

  planck_float(P): -  
planck_float_descent(1.0,P)。

planck_float_descent(X,P): -
Xhalf是X / 2.0,Xtest是1.0 + Xhalf,Xtest = \ = 1.0,!,
write(Xhalf), writeln(Xtest),
planck_float_descent(Xhalf,P)。
planck_float_descent(P,P): -
Xhalf是P / 2.0,Xtest是1.0 + Xhalf,Xtest == 1.0,
writeln(P)。

? - planck_float(P)。
P = 2.220446049250313e-16

2.22e-16 是最后一个值,添加到 1.0 仍然会产生大于 1.0 的声音用于IEEE 64位浮点运算。

其他Prolog具有哪些实现?

解决方案

SICStus的文档 4.3.1 阅读 a>:
$ b


浮动的范围是由C double 类型,
通常 [4.9e-324,1.8e + 308] (加号或减号)。如果发生溢出或
除以零,则会引发评估错误异常。浮点
用64位表示,符合IEEE 754标准。

结合ISO一致性声明,这不留下任何东西。请注意,没有任何进一步资格的IEEE 754标准没有多少说法,因为它可能意味着微型,小数和各种模式。此外,异常处理与延续值和其他建议使事情变得更加复杂和绝非易事。 Prolog标准要求在所有情况下产生Prolog异常(IEEE说法中的陷阱)。没有像NaN,+∞等连续值的规定。首先粗略看来,这表明这些值完全不符合ISO Prolog。但是,5.5节定义了对标准的可能扩展。特别是下面的子条款允许引入连续值。

lockquote

5.5.10可评估函子



处理器可能支持一个或多个额外的可评估
函子
(9)作为实现特定功能。一个
处理器可以支持
表达式的值,该表达式的值是一个附加类型

值,而不是一个例外值。


$ b $注:一个不使用扩展的程序应该不会依赖
来捕捉来自程序的错误,这些程序评估它们的参数
(比如是/ 2,8.6.1 ),除非它严格执行
模式(5.1 e)。

这个笔记揭示了背后的意图:在严格符合模式下,所有这些扩展都不存在,只有Prolog异常被发送。延伸应该如何精确到目前为止还不清楚。 @jschimpf的建议包含了一些有趣的观点,但是没有考虑到 William Kahan的文件。尤其是,IEEE-exception-flags完全缺失(或者相应的更好的作用域功能),这使得接下来的NaN无用。另外两个适当的代数完成不在那里。 (此外,该提案日期从2009年起,并没有采取)

ISO Prolog只提供一个浮点框架(见ISO / IEC 13211-1:1995 7.1.3浮点) ,二进制,十进制甚至任何正偶数基(radix)都适合。在二十世纪八十年代,一些系统(例如C-Prolog)曾经具有比单精度IEEE浮点数低一点的浮点数。在32位中,Prolog标签和浮点数都被挤压(尾数较小),而实际的计算是以双精度进行的,这是一个好的,不再有效的C默认值。我相信这种表示也适合于ISO。 ISO Prolog要求至少有6个十进制数字。然而,我不知道目前的系统使用任何东西比二进制IEEE双精度。



浮动在ISO Prolog基本上基于ISO LIA标准(语言独立算术 ) ISO / IEC 10967-1:1995 ,其中同时已经被兼容 ISO / IEC 10967-1:2012 符合ISO / IEC / IEEE 60559:2011,vulgo IEEE 754-2008。注意,IEEE和LIA有不同的用途:IEEE是关于浮点数的,只有少数几个函数,而LIA则包含更多的函数,整数算术和复数。 p>

为了给你一个想法,目前在各种Prolog系统中如何实现浮点运算,考虑一下目标:

pre> X是0 ** -1,write_canonical(X)。

应该产生 evaluation_error(undefined) 。三个系统符合(IF,SICStus,Prolog IV),其他系统除了两个以外都是不同的。


0 (+ inf) Infinity.0 inf.0 0.Inf inf inf


分别由


SWI,YAP,Minerva,XSB,Ciao,B,GNU

(有些需要中缀),它们都是无效的扩展,因为它们重新定义了现有的Prolog语法的含义。


I was looking through the SICStus manual's syntax description and there is a definition of "float". However, there is no indication of what the implementation of "float" actually is. IEEE single or double precision? Maybe even a BigDecimal?

In SWI Prolog (or at least SWISH), the "float" seems to be IEEE double precision, as can be determined via:

planck_float(P) :-
   planck_float_descent(1.0,P).

planck_float_descent(X,P) :-
   Xhalf is X / 2.0, Xtest is 1.0 + Xhalf, Xtest =\= 1.0, !,
   write(Xhalf),writeln(Xtest),
   planck_float_descent(Xhalf,P).
planck_float_descent(P,P) :-
   Xhalf is P / 2.0, Xtest is 1.0 + Xhalf, Xtest == 1.0,
   writeln(P).

?- planck_float(P).
P = 2.220446049250313e-16

2.22e-16 being the last value that, added to 1.0 still yields something larger than 1.0 sounds about right for IEEE 64-bit floating point arithmetic.

What implementation do other Prologs have?

解决方案

The documentation of SICStus 4.3.1 reads:

The range of floats is the one provided by the C double type, typically [4.9e-324, 1.8e+308] (plus or minus). In case of overflow or division by zero, an evaluation error exception will be raised. Floats are represented by 64 bits and they conform to the IEEE 754 standard.

Together with the declaration of ISO conformance, this does not leave anything open. Note that "the IEEE 754 standard" alone without any further qualification does not say much, as it might mean minifloats, decimals, and various modes. Also, exception handling vs continuation values and other recommendations make things much more complex and by no means easy.

The ISO Prolog standard requires to produce Prolog-exceptions ("traps" in IEEE parlance) in all cases. There is no provision of continuation values like NaN, +∞ and the like. At first cursory sight this suggests that those values are entirely incompatible to ISO Prolog. However, subclause 5.5 defines the possible extensions to the standard. There is in particular the following subclause which permits the introduction of continuation values.

5.5.10 Evaluable functors

A processor may support one or more additional evaluable
functors (9) as an implementation specific feature. A
processor may support the value of an expression being a
value of an additional type instead of an exceptional value.

NOTE - A program that makes no use of extensions should
not rely on catching errors from procedures that evaluate their
arguments (such as is/2, 8.6.1) unless it is executed in strictly
conforming mode (5.1 e).

This note reveals the intention behind: In strictly conforming mode all those extensions are absent and only Prolog-exceptions are signaled. How precisely an extension should look like is by far not clear. The proposal by @jschimpf contains some interesting points, but it does not take into account the intention of William Kahan's documents. In particular, IEEE-exception-flags are missing completely (or a corresponding better scoped functionality), which renders NaNs next to useless. The other two proper algebraic completions are not there. (Further, that proposal dates from 2009 and has not taken into account Cor.2:2012.)

ISO Prolog does only provide a framework for floating points (see ISO/IEC 13211-1:1995 7.1.3 Floating point), both binary, decimal and even any positive even base (radix) would fit. In the 1980s, some systems (e.g., C-Prolog) used to have floats with a precision a bit lower than single precision IEEE floats. In 32 bits both the Prolog tag and the float was squeezed (with a smaller mantissa), while actual computations were carried out in double precision, the good olde, no longer valid, C default. I believe that that representation fits into ISO, too. ISO Prolog requires at least 6 decimal digits. However, I am unaware of current systems using anything else than binary IEEE double precision.

Floats in ISO Prolog are based essentially on the ISO LIA standards ("language independent arithmetics") ISO/IEC 10967-1:1995, which in the meantime has been replaced by ISO/IEC 10967-1:2012 which is compatible with ISO/IEC/IEEE 60559:2011, vulgo IEEE 754-2008.

Note that IEEE and LIA serve different purposes: IEEE is about floats and only a few functions, whereas LIA includes more functions, integer arithmetics and complex numbers, too.

To give you an idea, how floating point operations are currently implemented in various Prolog systems, consider the goal

X is 0** -1, write_canonical(X).

which should produce an evaluation_error(undefined). Three systems conform (IF, SICStus, Prolog IV), and the other systems are all different except for two.

0, (+inf), Infinity.0, inf.0, 0.Inf, inf, inf

is produced respectively by

SWI, YAP, Minerva, XSB, Ciao, B, GNU

As all those outputs constitute valid Prolog text (some need an infix .), they are all invalid extensions since they redefine the meaning of existing Prolog syntax.

这篇关于Prolog实现通过“浮动”来表示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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