Prolog中指数为负时计算数字幂的规则? [英] Rule to calculate power of a number when the exponent is Negative in Prolog?

查看:120
本文介绍了Prolog中指数为负时计算数字幂的规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个幂函数pow,试图将B的值计算为E的幂.到目前为止,我已经处理了案件-
1.指数为0
2.指数不为零

I have a power function pow that attempts to calculate the value of B to the power of E. So far I handle the cases-
1. exponent is 0
2. exponent is non-zero

pow(B,0,1).
pow(B,E,Result):-   E2 is E - 1,
                    pow(B,E2,Result2),
                    Result is B*Result2.

如何添加幂函数可以处理负指数的另一种情况?

How can I add another case where the power function can handle negative exponents?

推荐答案

首先,应该考虑如何定义0 0 .从形式上来说,这是不确定.它可以是零,也可以是1.正如Wolfram的Mathworld在其有关权力的文章中以及其文章为零:

First, one should consider how to define 00. Formally speaking it is indeterminate. It could be zero or it could be 1. As Wolfram's Mathworld says in its article on powers and in its article on zero:

0 0 (从零到零的幂)本身是未定义的.缺乏相互矛盾的事实是因为 a 0 始终为1,所以0 0 应该等于1,但0 a 始终为0(对于 a > 0),因此0 a 应该等于0.尽管定义0 0 = 1允许简单地表示一些公式,但通常定义为0 0 的定义选择是不确定的. 1992年Knuth; 1997年Knuth,第57页).
00 (zero to the zeroth power) itself is undefined. The lack of a well-defined meaning for this quantity follows from the mutually contradictory facts that a0 is always 1, so 00 should equal 1, but 0a is always 0 (for a > 0), so 0a should equal 0. The choice of definition for 00 is usually defined to be indeterminate, although defining 00 = 1 allows some formulas to be expressed simply (Knuth 1992; Knuth 1997, p. 57).

因此,您首先应该选择如何定义0 0 的特例:是0吗?是1吗?是未定义的吗?

So you should first choose how to define the special case of 00: Is it 0? Is it 1? Is it undefined?

我选择将其视为未定义.

I choose to look at it as being undefined.

话虽如此,您可以看到正指数,如所示的重复相乘(例如10 3 为10 * 10 * 10或1,000),可以看到负指数,如所示重复除法(例如10 -3 为(((1/10)/10)/10)或0.001).我的倾向,部分是因为我喜欢这种方法的对称性,部分是为了避免割伤(因为割伤通常是您未正确定义解决方案的信号),所以会像这样:

That being said, you can look at a positive exponent as indicated repeated multiplication (e.g. 103 is 10*10*10, or 1,000), and you can look at a negative exponent as indicating repeated division (e.g, 10-3 is (((1/10)/10)/10), or 0.001). My inclination, partly because I like the symmetry of this approach and partly to avoid the cuts (since a cut is often a signal that you've not defined the solution properly), would be something like this:

% -----------------------------
% The external/public predicate
% -----------------------------
pow( 0 , 0 , _ ) :- ! , fail .
pow( X , N , R ) :-
  pow( X , N , 1 , R )
  .

% -----------------------------------
% the tail-recursive worker predicate
% -----------------------------------
pow( _ , 0 , R , R  ).
pow( X , N , T , R  ) :-
  N > 0 ,
  T1 is T * X ,
  N1 is N-1   ,
  pow( X , N1 , T1 , R )
  .
pow( _ , 0 , R , R  ) :-
  N < 0 ,
  T1 is T / X ,
  N1 is N+1   ,
  pow( X , N1 , T1 , R )
  .

另一种方法,正如其他人指出的那样,是将正指数定义为表示重复乘法,而将负指数定义为表示正指数的倒数,因此10 3 为10 * 10 * 10或1,000,而10 -3 是1/(10 3 )或1/1,000或0.001.要使用此定义,我将再次避免削减并执行以下操作:

The other approach, as others have noted, is to define a positive exponent as indicating repeated multiplication, and a negative exponent as indicating the reciprocal of the positive exponent, so 103 is 10*10*10 or 1,000, and 10-3 is 1/(103), or 1/1,000 or 0.001. To use this definition, I'd again avoid the cuts and do something like this:

% -----------------------------
% the external/public predicate
% -----------------------------
pow( 0 , 0 , _ ) :-  % 0^0 is indeterminate. Is it 1? Is it 0? Could be either.
  ! ,
  fail
  .
pow( X , N , R ) :-
  N > 0 ,
  pow( X , N , 1 , R )
  .
pow( X , N , R ) :-
  N < 0 ,
  N1 = - N ,
  pow( X , N1 , 1 , R1 ) ,
  R is 1 / R1
  .

% -----------------------------------
% The tail-recursive worker predicate
% -----------------------------------
pow( _ , 0 , R , R  ).
pow( X , N , T , R  ) :-
  N > 0 ,
  T1 is T * X ,
  N1 is N-1   ,
  pow( X , N1 , T1 , R )
  .

这篇关于Prolog中指数为负时计算数字幂的规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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