File: gprolog029.html

package info (click to toggle)
gprolog 1.3.0-6.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 13,512 kB
  • ctags: 8,954
  • sloc: ansic: 57,431; perl: 16,620; sh: 5,900; makefile: 1,284
file content (472 lines) | stat: -rw-r--r-- 22,135 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="gprolog.css">
<TITLE>
Arithmetic
</TITLE>
</HEAD>
<BODY TEXT=black BGCOLOR=white>
<A HREF="gprolog028.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="gprolog030.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H3 CLASS="subsection"><A NAME="htoc87">7.6</A>&nbsp;&nbsp;Arithmetic</H3><UL>
<LI><A HREF="gprolog029.html#toc60">Evaluation of an arithmetic expression</A>
<LI><A HREF="gprolog029.html#toc61"><TT>(is)/2</TT> - evaluate expression</A>
<LI><A HREF="gprolog029.html#toc62"><TT>(=:=)/2</TT> - arithmetic equal,
 <TT>(=\=)/2</TT> - arithmetic not equal,<BR>
<TT>(&lt;)/2</TT> - arithmetic less than,
 <TT>(=&lt;)/2</TT> - arithmetic less than or equal to,<BR>
<TT>(&gt;)/2</TT> - arithmetic greater than,
 <TT>(&gt;=)/2</TT> - arithmetic greater than or equal to</A>
</UL>

<A NAME="toc60"></A>
<H4 CLASS="subsubsection"><A NAME="htoc88">7.6.1</A>&nbsp;&nbsp;Evaluation of an arithmetic expression</H4>
<A NAME="Evaluation-of-an-arithmetic-expression"></A>
An arithmetic expression is a Prolog term built from numbers,
variables, and functors (or operators) that represent arithmetic functions.
When an expression is evaluated each variable must be bound to a
non-variable expression. An expression evaluates to a number, which may be
an integer or a floating point number. The following table details the
components of an arithmetic expression, how they are evaluated, the types
expected/returned and if they are ISO or an extension:<BR>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1>
<TR><TD VALIGN=top ALIGN=left NOWRAP>Expression</TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">Result = <I>eval</I>(Expression)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>Signature</TD>
<TD VALIGN=top ALIGN=center NOWRAP>ISO</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP>Variable</TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">must be bound to a non-variable expression <TT>E</TT>. 
<BR>
The result is <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; IF</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP>integer number</TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">this number</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>I &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP>floating point number</TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">this number</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>F &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>+ E</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; IF</TD>
<TD VALIGN=top ALIGN=center NOWRAP>N</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>- E</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">- <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; IF</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>inc(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E</TT>) + 1</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; IF</TD>
<TD VALIGN=top ALIGN=center NOWRAP>N</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>dec(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E</TT>) - 1</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; IF</TD>
<TD VALIGN=top ALIGN=center NOWRAP>N</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 + E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) + <I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF, IF &rarr; IF</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 - E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) - <I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF, IF &rarr; IF</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 * E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) * <I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF, IF &rarr; IF</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 / E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) / <I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF, IF &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 // E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>rnd</I>(<I>eval</I>(<TT>E1</TT>) /
<I>eval</I>(<TT>E2</TT>))</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>I, I &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 rem E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) -
(<I>rnd</I>(<I>eval</I>(<TT>E1</TT>) /
<I>eval</I>(<TT>E2</TT>))*<I>eval</I>(<TT>E2</TT>))</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>I, I &rarr;
I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 mod E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) - (
&lfloor;<I>eval</I>(<TT>E1</TT>) / <I>eval</I>(<TT>E2</TT>)&rfloor;
*<I>eval</I>(<TT>E2</TT>))</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>I, I &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 /\ E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) bitwise_and
<I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>I, I &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 \/ E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) bitwise_or
<I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>I, I &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 ^ E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) bitwise_xor
<I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>I, I &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>N</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>\ E</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">bitwise_not <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>I &rarr;
I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 &lt;&lt; E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) integer_shift_left
<I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>I, I &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 &gt;&gt; E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) integer_shift_right
<I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>I, I &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>abs(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">absolute value of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF
&rarr; IF</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>sign(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">sign of <I>eval</I>(<TT>E</TT>) (-1 if &lt; 0, 0 if = 0,
+1 if &gt; 0)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; IF</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>min(E1,E2)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">minimal value between <I>eval</I>(<TT>E1</TT>) and
<I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF, IF &rarr; ?</TD>
<TD VALIGN=top ALIGN=center NOWRAP>N</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>max(E1,E2)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">maximal value between <I>eval</I>(<TT>E1</TT>) and
<I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF, IF &rarr; ?</TD>
<TD VALIGN=top ALIGN=center NOWRAP>N</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>E1 ** E2</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>eval</I>(<TT>E1</TT>) raised to the power of
<I>eval</I>(<TT>E2</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF, IF &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>sqrt(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">square root of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF
&rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>atan(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">arc tangent of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF
&rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>cos(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">cosine of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>acos(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">arc cosine of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>N</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>sin(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">sine of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>asin(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">arc sine of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>N</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>exp(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft"><I>e</I> raised to the power of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>log(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">natural logarithms of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF
&rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>float(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">the floating point number equal to
<I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>IF &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>ceiling(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">rounds <I>eval</I>(<TT>E</TT>) upward to the
nearest integer</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>F &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>floor(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">rounds <I>eval</I>(<TT>E</TT>) downward to the
nearest integer</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>F &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>round(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">rounds <I>eval</I>(<TT>E</TT>) to the nearest integer</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>F &rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>truncate(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">the integer value of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>F
&rarr; I</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>float_fractional_part(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">the float equal to the fractional part
of <I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>F &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR>
<TR><TD VALIGN=top ALIGN=left NOWRAP><TT>float_integer_part(E)</TT></TD>
<TD VALIGN=top ALIGN=left><DIV CLASS="flushleft">the float equal to the integer part of
<I>eval</I>(<TT>E</TT>)</DIV></TD>
<TD VALIGN=top ALIGN=center NOWRAP>F &rarr; F</TD>
<TD VALIGN=top ALIGN=center NOWRAP>Y</TD>
</TR></TABLE><BR>
The meaning of the signature field is as follows:
<UL CLASS="itemize"><LI CLASS="li-itemize">I &rarr; I: unary function, the operand must be an integer and
the result is an integer.<BR>
<BR>
<LI CLASS="li-itemize">F &rarr; F: unary function, the operand must be a floating
point number and the result is a floating point number.<BR>
<BR>
<LI CLASS="li-itemize">F &rarr; I: unary function, the operand must be a floating
point number and the result is an integer.<BR>
<BR>
<LI CLASS="li-itemize">IF &rarr; F: unary function, the operand can be an integer or a
floating point number and the result is a floating point number.<BR>
<BR>
<LI CLASS="li-itemize">IF &rarr; IF: unary function, the operand can be an integer or
a floating point number and the result has the same type as the operand.<BR>
<BR>
<LI CLASS="li-itemize">I, I &rarr; I: binary function: each operand must be an integer
and the result is an integer.<BR>
<BR>
<LI CLASS="li-itemize">IF, IF &rarr; IF: binary function: each operand can be an
integer or a floating point number and the result is a floating point number
if at least one operand is a floating point number, an integer otherwise.<BR>
<BR>
<LI CLASS="li-itemize">IF, IF &rarr; ?: binary function: each operand can be an
integer or a floating point number and the result has the same type as the
selected operand. This is used for <TT>min</TT> and <TT>max</TT>. Note that
in case of equality between an integer and a floating point number the
result is an integer.</UL>
<TT>is</TT>, <TT>+</TT>, <TT>-</TT>, <TT>*</TT>, <TT>//</TT>, <TT>/</TT>,
<TT>rem</TT>, and <TT>mod</TT> are predefined infix operators. <TT>+</TT>
and <TT>-</TT> are predefined prefix operators (section&nbsp;<A HREF="gprolog037.html#op/3:(Term-input/output)">7.14.10</A>).<BR>
<BR>
<B>Integer division rounding function</B>: the integer division rounding
function <TT><I>rnd</I>(X)</TT> rounds the floating point number
<TT>X</TT> to an integer. There are two possible definitions (depending on
the target machine) for this function which differ on negative numbers:
<UL CLASS="itemize"><LI CLASS="li-itemize"><TT><I>rnd</I>(X)</TT> = integer part of <TT>X</TT>, e.g.
<TT><I>rnd</I>(-1.5)</TT> = <TT>-1</TT> (round toward 0)<BR>
<BR>
<LI CLASS="li-itemize"><TT><I>rnd</I>(X)</TT> = &lfloor;<TT>X</TT>&rfloor;, e.g.
<TT><I>rnd</I>(-1.5)</TT> = <TT>-2</TT> (round toward &minus;&infin;)</UL>
The definition of this function determines the precise definition of the
integer division <TT>(//)/2</TT> and of the integer remainder
<TT>(rem)/2</TT>. Rounding toward zero is the most common case. In any case
it is possible to test the value (<TT>toward_zero</TT> or <TT>down</TT>) of
the <TT>integer_rounding_function</TT> Prolog flag to determine which
function being used (section&nbsp;<A HREF="gprolog045.html#set-prolog-flag/2">7.22.1</A>).<BR>
<BR>
<B>Fast mathematical mode</B>: in order to speed-up integer computations,
the GNU Prolog compiler can generate faster code when invoked with the
<TT>&ndash;fast-math</TT> option (section&nbsp;<A HREF="gprolog008.html#Using-the-compiler">3.4.3</A>). In this mode only
integer operations are allowed and a variable in an expression must be bound
at evaluation time to an integer. No type checking is done. <BR>
<BR>
<B>Errors</B><BR>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left>a sub-expression <TT>E</TT> is a variable</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>instantiation_error</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left>a sub-expression <TT>E</TT> is neither a number nor an evaluable
functor</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>type_error(evaluable, E)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left>a sub-expression <TT>E</TT> is a floating point number while an
integer is expected</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>type_error(integer, E)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left>a sub-expression <TT>E</TT> is an integer while a floating point
number is expected</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>type_error(float, E)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR>
<TR><TD VALIGN=top ALIGN=left>a division by zero occurs</TD>
<TD VALIGN=top ALIGN=center NOWRAP>&nbsp;&nbsp;</TD>
<TD VALIGN=top ALIGN=left><TT>evaluation_error(zero_divisor)</TT></TD>
</TR>
<TR><TD BGCOLOR=black COLSPAN=3><TABLE BORDER=0 WIDTH="100%" CELLSPACING=0 CELLPADDING=1><TR><TD></TD></TR></TABLE></TD>
</TR></TABLE><BR>
<B>Portability</B><BR>
<BR>
Refer to the above table to determine which evaluable functors are ISO
and which are GNU Prolog extensions. For efficiency reasons, GNU
Prolog does not detect the following ISO arithmetic errors:
<TT>float_overflow</TT>,
<TT>int_overflow, int_underflow</TT>, and <TT>undefined</TT>.<BR>
<BR>
<A NAME="toc61"></A>
<H4 CLASS="subsubsection"><A NAME="htoc89">7.6.2</A>&nbsp;&nbsp;<TT>(is)/2</TT> - evaluate expression</H4>
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
is(?term, +evaluable)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>Result is Expression</TT> succeeds if <TT>Result</TT> can be
unified with <I>eval</I>(<TT>Expression</TT>). Refer to the
evaluation of an arithmetic expression for the definition of the
<I>eval</I> function
(section&nbsp;<A HREF="#Evaluation-of-an-arithmetic-expression">7.6.1</A>).<BR>
<BR>
<TT>is</TT> is a predefined infix operator (section&nbsp;<A HREF="gprolog037.html#op/3:(Term-input/output)">7.14.10</A>).<BR>
<BR>
<B>Errors</B><BR>
<BR>
Refer to the evaluation of an arithmetic expression for possible errors
(section&nbsp;<A HREF="#Evaluation-of-an-arithmetic-expression">7.6.1</A>).<BR>
<BR>
<B>Portability</B><BR>
<BR>
ISO predicate.<BR>
<BR>
<A NAME="toc62"></A>
<H4 CLASS="subsubsection"><A NAME="htoc90">7.6.3</A>&nbsp;&nbsp;<TT>(=:=)/2</TT> - arithmetic equal,
 <TT>(=\=)/2</TT> - arithmetic not equal,<BR>
<TT>(&lt;)/2</TT> - arithmetic less than,
 <TT>(=&lt;)/2</TT> - arithmetic less than or equal to,<BR>
<TT>(&gt;)/2</TT> - arithmetic greater than,
 <TT>(&gt;=)/2</TT> - arithmetic greater than or equal to</H4>
<A NAME="(=:=)/2"></A> 
 
 
 
 
 
 
<B>Templates</B>
<DL CLASS="list" COMPACT="compact"><DT CLASS="dt-list"><DD CLASS="dd-list"><TT>
=:=(+evaluable, +evaluable)<BR>
=\=(+evaluable, +evaluable)<BR>
&lt;(+evaluable, +evaluable)<BR>
=&lt;(+evaluable, +evaluable)<BR>
&gt;(+evaluable, +evaluable)<BR>
&gt;=(+evaluable, +evaluable)</TT></DL>
<B>Description</B><BR>
<BR>
<TT>Expr1 =:= Expr2</TT> succeeds if <I>eval</I>(<TT>Expr1</TT>) =
<I>eval</I>(<TT>Expr2</TT>).<BR>
<BR>
<TT>Expr1 =\= Expr2</TT> succeeds if <I>eval</I>(<TT>Expr1</TT>)
&#8800; <I>eval</I>(<TT>Expr2</TT>).<BR>
<BR>
<TT>Expr1 &lt; Expr2</TT> succeeds if <I>eval</I>(<TT>Expr1</TT>) &lt;
<I>eval</I>(<TT>Expr2</TT>).<BR>
<BR>
<TT>Expr1 =&lt; Expr2</TT> succeeds if <I>eval</I>(<TT>Expr1</TT>)
&#8804; <I>eval</I>(<TT>Expr2</TT>).<BR>
<BR>
<TT>Expr1 &gt; Expr2</TT> succeeds if <I>eval</I>(<TT>Expr1</TT>) &gt;
<I>eval</I>(<TT>Expr2</TT>).<BR>
<BR>
<TT>Expr1 &gt;= Expr2</TT> succeeds if <I>eval</I>(<TT>Expr1</TT>)
&#8805; <I>eval</I>(<TT>Expr2</TT>).<BR>
<BR>
Refer to the evaluation of an arithmetic expression for the definition of
the <I>eval</I> function (section&nbsp;<A HREF="#Evaluation-of-an-arithmetic-expression">7.6.1</A>).<BR>
<BR>
<TT>=:=</TT>, <TT>=\=</TT>, <TT>&lt;</TT>, <TT>=&lt;</TT>,
<TT>&gt;</TT> and <TT>&gt;=</TT> are predefined infix operators
(section&nbsp;<A HREF="gprolog037.html#op/3:(Term-input/output)">7.14.10</A>).<BR>
<BR>
<B>Errors</B><BR>
<BR>
Refer to the evaluation of an arithmetic expression for possible errors
(section&nbsp;<A HREF="#Evaluation-of-an-arithmetic-expression">7.6.1</A>).<BR>
<BR>
<B>Portability</B><BR>
<BR>
ISO predicates.<BR>
<BR>

<HR SIZE=2>
Copyright (C) 1999-2007 Daniel Diaz
<BR>
<BR>
Verbatim copying and distribution of this entire article is permitted in any
medium, provided this notice is preserved. <BR>
<BR>
<A HREF="index.html#copyright">More about the copyright</A>
<HR>
<A HREF="gprolog028.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="gprolog023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="gprolog030.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>