Prolog中指数为负时计算幂的规则?

2023-11-30

我有幂函数pow试图计算的值B的力量E。到目前为止我处理的案件-
1.指数为0
2. 指数非零

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

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


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:

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).

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

我选择将其视为未定义。

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 )
  .

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 )
  .
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Prolog中指数为负时计算幂的规则? 的相关文章

  • Prolog 中的否定作为失败是一种程序行为吗?

    我有一个关于否定即失败在 Prolog 语言中 这是一个理论性多于实践性的问题 因为我清楚这个例子是如何工作的 所以我有以下 Prolog 程序 Fatti che specificano quali esseri sono degli a
  • 在 Prolog 中解答爱因斯坦之谜

    我正在尝试解决爱因斯坦之谜 https www wikiwand com en Zebra Puzzle在序言中 我在编写程序时遇到了困难 基本方法是添加所有约束并让 Prolog 找出唯一可能的解决方案 问题是 Prolog 找到了 0
  • Smullyan 数值机的解决方案

    在这里我建议找到 Smullyan 数值机的解决方案 此处定义 http heras gilsanz com manuel smullyan machines html 问题陈述 它们是接受数字列表作为输入 并根据输入模式遵循一些规则将其转
  • 如何确定矩阵的所有给定坐标都是相连的?

    给定一个网格 我如何确定网格的元素是否都在单个区域中 在下面的情况下是正确的 因为矩阵中的每个元素都有一个邻居 示例1 gridneighbours 1 1 1 2 1 3 2 1 2 2 2 3 3 1 4 1 4 2 true 然而在我
  • 如何使用 Prolog 查找二叉树的深度

    我正在学习 Prolog 并试图找到一个深度二叉树使用 Prolog 我代表一棵树是这样的 nil is a tree tree 1 nil nil this is a leaf tree 1 tree 1 nil nil nil this
  • Prolog 追加与剪切运算符

    当我们使用append和cut操作符时会出现什么问题 append2 L L append2 H T L H TL append2 T L TL 我尝试了几种不同的输入 但总是成功 append2 1 2 5 L L 1 2 5 appen
  • Ruby 中的大乘法输出结果为负

    我写了一些代码 应该对 1 sum 0 1 1000 each do n sum n n puts n n sum sum end 由于某种原因 在数字 28 之后输出为负值 n 29 sum 20154009777005735238923
  • 如何在 swi-prolog 的 prolog 文件中运行 prolog 查询?

    如果我有一个定义规则的 prolog 文件 并在 Windows 中的 prolog 终端中打开它 它会加载事实 然而 然后它显示 提示我手动输入一些内容 如何将代码添加到文件中 以便它实际上会评估这些特定的语句 就像我输入它们一样 像这样
  • 如何实现 not_all_equal/1 谓词

    如何实施not all equal 1谓词 如果给定列表包含至少 2 个不同的元素 则该谓词成功 否则失败 这是我的尝试 不是很纯粹的尝试 not all equal L member H1 L member H2 L H1 H2 gt t
  • 寻找最大最小值集合

    我正在尝试编写一个 天真的或半天真的 程序 给定一组元素和许多玩家将其划分为这个数量的玩家 并且对于每个这样的划分取最小值 按总和 子集 然后 我想计算所有这些最小除法的最大值 这被称为https en wikipedia org wiki
  • Prolog 管线任务

    我有一项任务是在序言中制作一张简化的地铁地图 其中一部分要求制定一项规则来检查两个车站是否在同一条线上 我有一条规则 但它似乎不起作用 这就是我到目前为止所拥有的 adjacent nh lg central 4 adjacent lg o
  • Prolog 同构图

    这里尝试解决同构图问题 作业信息 判断2个无向图是否同构 没有孤立的顶点 顶点数小于30 图的边作为谓词给出 即 e 1 2 f 1 2 我正在尝试使用以下方法 对于每对边 即图 1 和图 2 中的每条边 Try to bind the v
  • Prolog 中的匹配元组

    为什么Prolog匹配 X Xs 包含更多元素的元组 一个例子 test2 X Xs write X nl test2 Xs test2 X write X nl test
  • 为什么在具体化中将 clpfd 变量分配给实际值?

    我正在开发一个 SWI Prolog 程序 该程序使用 CLP FD 约束来找到特定问题的解决方案 为此 我碰巧需要两个列表的 未定位 重叠 那是 List La长度为A List Lb长度为 B A gt B 未定位的重叠列表是La Lb
  • 非成员规则在 Prolog 中无法按预期工作

    我正在尝试在 Prolog 中创建一个迷宫程序 其目的是找到一条从迷宫起点到迷宫中心点 m 的路线 迷宫由使用四种颜色之一连接的正方形组成 蓝色 绿色 紫色或橙色 从起点到中心的路线遵循四种颜色的重复图案 我创建了以下代码 link2 A
  • 求解序言中极其简单的方程:A = B + C?

    我有一个非常简单的方程 我希望能够在序言中求解 A B C 我希望能够编写一个谓词来表达这种关系 它可以处理任何一个未实例化的参数 无需推广到更复杂的关系或方程 myEquation A B C something 我可以使用以下语义进行调
  • Prolog 罗马数字(属性语法)

    我正在做一项作业prolog questions tagged prolog扫描数字列表并应返回该列表是否是有效的罗马数字以及数字的十进制值 前任 1 roman N I N 1 true 2 当我运行我认为应该工作的程序时 十进制值总是正
  • 如何为有效号码指定 DCG?

    我正在尝试为有效数字指定 DCG 如下所示 value Number gt valid number Number 基本上检查指定的值是否是数字 它也可能是变量 因此有必要检查 我不知道如何构建这个valid number不过 DCG 谓词
  • 如何在 Prolog 中解决这个算术表达式难题?

    我有一个编程问题 https blog svpino com 2015 05 08 solution to problem 5 and some other thoughts about this type of questions htt
  • 斜线(/)在序言中做什么?

    我有这个代码 set value X Value X T X Value T set value X Value Y V T Y V NewT X Y set value X Value T NewT set value X Value X

随机推荐