/usr/lib/swi-prolog/doc/Manual/arith.html is in swi-prolog-nox 5.10.4-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>SWI-Prolog 5.11.18 Reference Manual: Section 4.25</TITLE><LINK REL=home HREF="index.html">
<LINK REL=contents HREF="Contents.html">
<LINK REL=index HREF="DocIndex.html">
<LINK REL=summary HREF="summary.html">
<LINK REL=previous HREF="charconv.html">
<LINK REL=next HREF="extendarith.html">
<STYLE type="text/css">
/* Style sheet for SWI-Prolog latex2html
*/
dd.defbody
{ margin-bottom: 1em;
}
dt.pubdef
{ background-color: #c5e1ff;
}
dt.multidef
{ background-color: #c8ffc7;
}
.bib dd
{ margin-bottom: 1em;
}
.bib dt
{ float: left;
margin-right: 1.3ex;
}
pre.code
{ margin-left: 1.5em;
margin-right: 1.5em;
border: 1px dotted;
padding-top: 5px;
padding-left: 5px;
padding-bottom: 5px;
background-color: #f8f8f8;
}
div.navigate
{ text-align: center;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
}
div.title
{ text-align: center;
padding-bottom: 1em;
font-size: 200%;
font-weight: bold;
}
div.author
{ text-align: center;
font-style: italic;
}
div.abstract
{ margin-top: 2em;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
margin-left: 10%; margin-right:10%;
}
div.abstract-title
{ text-align: center;
padding: 5px;
font-size: 120%;
font-weight: bold;
}
div.toc-h1
{ font-size: 200%;
font-weight: bold;
}
div.toc-h2
{ font-size: 120%;
font-weight: bold;
margin-left: 2em;
}
div.toc-h3
{ font-size: 100%;
font-weight: bold;
margin-left: 4em;
}
div.toc-h4
{ font-size: 100%;
margin-left: 6em;
}
span.sec-nr
{
}
span.sec-title
{
}
span.pred-ext
{ font-weight: bold;
}
span.pred-tag
{ float: right;
padding-top: 0.2em;
font-size: 80%;
font-style: italic;
color: #202020;
}
/* Footnotes */
sup.fn { color: blue; text-decoration: underline; }
span.fn-text { display: none; }
sup.fn span {display: none;}
sup:hover span
{ display: block !important;
position: absolute; top: auto; left: auto; width: 80%;
color: #000; background: white;
border: 2px solid;
padding: 5px; margin: 10px; z-index: 100;
font-size: smaller;
}
</STYLE>
</HEAD>
<BODY BGCOLOR="white">
<DIV class="navigate"><A class="nav" href="index.html"><IMG SRC="home.gif" BORDER=0 ALT="Home"></A>
<A class="nav" href="Contents.html"><IMG SRC="index.gif" BORDER=0 ALT="Contents"></A>
<A class="nav" href="DocIndex.html"><IMG SRC="yellow_pages.gif" BORDER=0 ALT="Index"></A>
<A class="nav" href="summary.html"><IMG SRC="info.gif" BORDER=0 ALT="Summary"></A>
<A class="nav" href="charconv.html"><IMG SRC="prev.gif" BORDER=0 ALT="Previous"></A>
<A class="nav" href="extendarith.html"><IMG SRC="next.gif" BORDER=0 ALT="Next"></A>
</DIV>
<H2><A NAME="sec:4.25"><SPAN class="sec-nr">4.25</SPAN> <SPAN class="sec-title">Arithmetic</SPAN></A></H2>
<A NAME="sec:arith"></A>
<P>Arithmetic can be divided into some special purpose integer
predicates and a series of general predicates for integer, floating
point and rational arithmetic as appropriate. The general arithmetic
predicates all handle <VAR>expressions</VAR>. An expression is either a
simple number or a <VAR>function</VAR>. The arguments of a function are
expressions. The functions are described in <A class="sec" href="arith.html">section
4.25.2.3</A>.
<H3><A NAME="sec:4.25.1"><SPAN class="sec-nr">4.25.1</SPAN> <SPAN class="sec-title">Special
purpose integer arithmetic</SPAN></A></H3>
<P>The predicates in this section provide more logical operations
between integers. They are not covered by the ISO standard, although
they are `part of the community' and found as either library or built-in
in many other Prolog systems.
<DL class="latex">
<DT class="pubdef"><A NAME="between/3"><STRONG>between</STRONG>(<VAR>+Low,
+High, ?Value</VAR>)</A></DT>
<DD class="defbody">
<VAR>Low</VAR> and <VAR>High</VAR> are integers, <VAR><VAR>High</VAR> >=<VAR>Low</VAR></VAR>.
If
<VAR>Value</VAR> is an integer, <VAR><VAR>Low</VAR> =<<VAR>Value</VAR>
=<<VAR>High</VAR></VAR>. When <VAR>Value</VAR> is a variable it is
successively bound to all integers between <VAR>Low</VAR> and <VAR>High</VAR>.
If <VAR>High</VAR> is <CODE>inf</CODE> or
<CODE>infinite</CODE><SUP class="fn">57<SPAN class="fn-text">We prefer <CODE>infinite</CODE>,
but some other Prolog systems already use <CODE>inf</CODE> for infinity
we accept both for the time being.</SPAN></SUP>
<A NAME="idx:between3:1004"></A><A class="pred" href="arith.html#between/3">between/3</A>
is true iff <VAR><VAR>Value</VAR> >=<VAR>Low</VAR></VAR>, a feature
that is particularly interesting for generating integers from a certain
value.</DD>
<DT class="pubdef"><A NAME="succ/2"><STRONG>succ</STRONG>(<VAR>?Int1,
?Int2</VAR>)</A></DT>
<DD class="defbody">
True if <VAR><VAR>Int2</VAR> = <VAR>Int1</VAR> + 1</VAR> and <VAR><VAR>Int1</VAR>
>= 0</VAR>. At least one of the arguments must be instantiated to a
natural number. This predicate raises the domain-error <CODE>not_less_than_zero</CODE>
if called with a negative integer. E.g. <CODE>succ(X, 0)</CODE> fails
silently and <CODE>succ(X, -1)</CODE> raises a domain-error.<SUP class="fn">58<SPAN class="fn-text">The
behaviour to deal with natural numbers only was defined by Richard
O'Keefe to support the common count-down-to-zero in a natural way. Up-to
5.1.8 <A NAME="idx:succ2:1005"></A><A class="pred" href="arith.html#succ/2">succ/2</A>
also accepted negative integers.</SPAN></SUP></DD>
<DT class="pubdef"><A NAME="plus/3"><STRONG>plus</STRONG>(<VAR>?Int1,
?Int2, ?Int3</VAR>)</A></DT>
<DD class="defbody">
True if <VAR><VAR>Int3</VAR> = <VAR>Int1</VAR> + <VAR>Int2</VAR></VAR>.
At least two of the three arguments must be instantiated to integers.
</DD>
</DL>
<H3><A NAME="sec:4.25.2"><SPAN class="sec-nr">4.25.2</SPAN> <SPAN class="sec-title">General
purpose arithmetic</SPAN></A></H3>
<A NAME="sec:arithpreds"></A>
<P>The general arithmetic predicates are optionally compiled (see
<A NAME="idx:setprologflag2:1006"></A><A class="pred" href="flags.html#set_prolog_flag/2">set_prolog_flag/2</A>
and the <STRONG>-O</STRONG> command line option). Compiled arithmetic
reduces global stack requirements and improves performance.
Unfortunately compiled arithmetic cannot be traced, which is why it is
optional.
<DL class="latex">
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME=">/2"><VAR>+Expr1</VAR> <STRONG>></STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
True if expression <VAR>Expr1</VAR> evaluates to a larger number than <VAR>Expr2</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="</2"><VAR>+Expr1</VAR> <STRONG><</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
True if expression <VAR>Expr1</VAR> evaluates to a smaller number than <VAR>Expr2</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="=</2"><VAR>+Expr1</VAR> <STRONG>=<</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
True if expression <VAR>Expr1</VAR> evaluates to a smaller or equal
number to <VAR>Expr2</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME=">=/2"><VAR>+Expr1</VAR> <STRONG>>=</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
True if expression <VAR>Expr1</VAR> evaluates to a larger or equal
number to <VAR>Expr2</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="=\=/2"><VAR>+Expr1</VAR> <STRONG>=\=</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
True if expression <VAR>Expr1</VAR> evaluates to a number non-equal to
<VAR>Expr2</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="=:=/2"><VAR>+Expr1</VAR> <STRONG>=:=</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
True if expression <VAR>Expr1</VAR> evaluates to a number equal to <VAR>
Expr2</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="is/2"><VAR>-Number</VAR> <STRONG>is</STRONG> <VAR>+Expr</VAR></A></DT>
<DD class="defbody">
True when <VAR>Number</VAR> is the value to which <VAR>Expr</VAR>
evaluates. Typically, <A NAME="idx:is2:1007"></A><A class="pred" href="arith.html#is/2">is/2</A>
should be used with unbound left operand. If equality is to be tested,
=:=/2 should be used. For example:
<P>
<CENTER>
<TABLE BORDER=0 FRAME=void RULES=groups>
<TR VALIGN=top><TD><CODE>?- 1 is sin(pi/2).</CODE> </TD><TD>Fails!. sin(pi/2)
evaluates to the float 1.0, which does not unify with the integer 1. </TD></TR>
<TR VALIGN=top><TD><CODE>?- 1 =:= sin(pi/2).</CODE> </TD><TD>Succeeds as
expected.</TD></TR>
</TABLE>
</CENTER>
</DD>
</DL>
<H4><A NAME="sec:4.25.2.1"><SPAN class="sec-nr">4.25.2.1</SPAN> <SPAN class="sec-title">Arithmetic
types</SPAN></A></H4>
<A NAME="sec:artypes"></A>
<P><A NAME="idx:integerunbounded:1008"></A><A NAME="idx:rationalnumber:1009"></A><A NAME="idx:numberrational:1010"></A>SWI-Prolog
defines the following numeric types:
<P>
<UL class="latex">
<LI><I>integer</I><BR>
If SWI-Prolog is built using the <EM>GNU multiple precision arithmetic
library</EM> <A NAME="idx:GMP:1011"></A>(GMP), integer arithmetic is <EM>unbounded</EM>,
which means that the size of integers is limited by available memory
only. Without GMP, SWI-Prolog integers are 64-bits, regardless of the
native integer size of the platform. The type of integer support can be
detected using the Prolog flags <A class="flag" href="flags.html#flag:bounded">bounded</A>, <A class="flag" href="flags.html#flag:min_integer">min_integer</A>
and
<A class="flag" href="flags.html#flag:max_integer">max_integer</A>. As
the use of GMP is default, most of the following descriptions assume
unbounded integer arithmetic.
<P>Internally, SWI-Prolog has three integer representations. Small
integers (defined by the Prolog flag <A class="flag" href="flags.html#flag:max_tagged_integer">max_tagged_integer</A>)
are encoded directly. Larger integers are represented as 64-bit value on
the global stack. Integers that do not fit in 64-bit are represented as
serialised GNU MPZ structures on the global stack.
<P>
<LI><I>rational number</I><BR>
Rational numbers (<VAR>Q</VAR>) are quotients of two integers. Rational
arithmetic is only provided if GMP is used (see above). Rational numbers
are currently not supported by a Prolog type. They are represented by
the compound term <CODE>rdiv(N,M)</CODE>. Rational numbers that are
returned from <A NAME="idx:is2:1012"></A><A class="pred" href="arith.html#is/2">is/2</A>
are <EM>canonical</EM>, which means <VAR>M</VAR> is positive and <VAR>N</VAR>
and
<VAR>M</VAR> have no common divisors. Rational numbers are introduced in
the computation using the <A NAME="idx:rational1:1013"></A><A class="pred" href="typetest.html#rational/1">rational/1</A>, <A NAME="idx:rationalize1:1014"></A><A class="pred" href="arith.html#rationalize/1">rationalize/1</A>
or the <A NAME="idx:rdiv2:1015"></A><A class="pred" href="arith.html#rdiv/2">rdiv/2</A>
(rational division) function. Using the same functor for rational
division and representing rational numbers allow for passing rational
numbers between computations as well as to <A NAME="idx:format3:1016"></A><A class="pred" href="format.html#format/3">format/3</A>
for printing.
<P>On the long term it is likely that rational numbers will become
<EM>atomic</EM> as well as subtype of <EM>number</EM>. User code that
creates or inspects the <CODE>rdiv(M,N)</CODE> terms will not be
portable to future versions. Rationals are created using one of the
functions mentioned above and inspected using <A NAME="idx:rational3:1017"></A><A class="pred" href="typetest.html#rational/3">rational/3</A>.
<P>
<LI><I>float</I><BR>
Floating point numbers are represented using the C-type <CODE>double</CODE>.
On most today platforms these are 64-bit IEEE floating point numbers.
</UL>
<P>Arithmetic functions that require integer arguments accept, in
addition to integers, rational numbers with (canonical) denominator `1'.
If the required argument is a float the argument is converted to float.
Note that conversion of integers to floating point numbers may raise an
overflow exception. In all other cases, arguments are converted to the
same type using the order below.
<BLOCKQUOTE> integer <VAR>-></VAR> rational number <VAR>-></VAR>
floating point number
</BLOCKQUOTE>
<H4><A NAME="sec:4.25.2.2"><SPAN class="sec-nr">4.25.2.2</SPAN> <SPAN class="sec-title">Rational
number examples</SPAN></A></H4>
<A NAME="sec:rational"></A>
<P>The use of rational numbers with unbounded integers allows for exact
integer or <EM>fixed point</EM> arithmetic under the addition,
subtraction, multiplication and division. To exploit rational arithmetic <A NAME="idx:rdiv2:1018"></A><A class="pred" href="arith.html#rdiv/2">rdiv/2</A>
should be used instead of `/' and floating point numbers must be
converted to rational using <A NAME="idx:rational1:1019"></A><A class="pred" href="typetest.html#rational/1">rational/1</A>.
Omitting the
<A NAME="idx:rational1:1020"></A><A class="pred" href="typetest.html#rational/1">rational/1</A>
on floats will convert a rational operand to float and continue the
arithmetic using floating point numbers. Here are some examples.
<P>
<CENTER>
<TABLE BORDER=0 FRAME=void RULES=groups>
<TR VALIGN=top><TD>A is 2 rdiv 6</TD><TD>A = 1 rdiv 3 </TD></TR>
<TR VALIGN=top><TD>A is 4 rdiv 3 + 1</TD><TD>A = 7 rdiv 3 </TD></TR>
<TR VALIGN=top><TD>A is 4 rdiv 3 + 1.5</TD><TD>A = 2.83333 </TD></TR>
<TR VALIGN=top><TD>A is 4 rdiv 3 + rational(1.5)</TD><TD>A = 17 rdiv 6 </TD></TR>
</TABLE>
</CENTER>
<P>Note that floats cannot represent all decimal numbers exactly. The
function <A NAME="idx:rational1:1021"></A><A class="pred" href="typetest.html#rational/1">rational/1</A>
creates an <EM>exact</EM> equivalent of the float, while <A NAME="idx:rationalize1:1022"></A><A class="pred" href="arith.html#rationalize/1">rationalize/1</A>
creates a rational number that is within the float rounding error from
the original float. Please check the documentation of these functions
for details and examples.
<P>Rational numbers can be printed as decimal numbers with arbitrary
precision using the <A NAME="idx:format3:1023"></A><A class="pred" href="format.html#format/3">format/3</A>
floating point conversion:
<PRE class="code">
?- A is 4 rdiv 3 + rational(1.5),
format('~50f~n', [A]).
2.83333333333333333333333333333333333333333333333333
A = 17 rdiv 6
</PRE>
<H4><A NAME="sec:4.25.2.3"><SPAN class="sec-nr">4.25.2.3</SPAN> <SPAN class="sec-title">Arithmetic
Functions</SPAN></A></H4>
<A NAME="sec:functions"></A>
<P>Arithmetic functions are terms which are evaluated by the arithmetic
predicates described in <A class="sec" href="arith.html">section 4.25.2</A>.
There are four types of arguments to functions:
<P>
<CENTER>
<TABLE BORDER=0 FRAME=void RULES=groups>
<TR VALIGN=top><TD><VAR>Expr</VAR> </TD><TD>Arbitrary expression,
returning either a floating point value or an integer. </TD></TR>
<TR VALIGN=top><TD><VAR>IntExpr</VAR> </TD><TD>Arbitrary expression that
must evaluate into an integer. </TD></TR>
<TR VALIGN=top><TD><VAR>RatExpr</VAR> </TD><TD>Arbitrary expression that
must evaluate into a rational number. </TD></TR>
<TR VALIGN=top><TD><VAR>FloatExpr</VAR> </TD><TD>Arbitrary expression
that must evaluate into a floating point.</TD></TR>
</TABLE>
</CENTER>
<P>For systems using bounded integer arithmetic (default is unbounded,
see <A class="sec" href="arith.html">section 4.25.2.1</A> for details),
integer operations that would cause overflow automatically convert to
floating point arithmetic.
<DL class="latex">
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="-/1"><STRONG>-</STRONG> <VAR>+Expr</VAR></A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = -<VAR>Expr</VAR></VAR></DD>
<DT class="pubdef"><A NAME="+/1"><STRONG>+</STRONG> <VAR>+Expr</VAR></A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = <VAR>Expr</VAR></VAR>. Note that if <CODE><CODE>+</CODE></CODE>
is followed by a number the parser discards the <CODE><CODE>+</CODE></CODE>.
I.e. <CODE>?- integer(+1)</CODE> succeeds.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="+/2"><VAR>+Expr1</VAR> <STRONG>+</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = <VAR>Expr1</VAR> + <VAR>Expr2</VAR></VAR></DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="-/2"><VAR>+Expr1</VAR> <STRONG>-</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = <VAR>Expr1</VAR> - <VAR>Expr2</VAR></VAR></DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="*/2"><VAR>+Expr1</VAR> <STRONG>*</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = <VAR>Expr1</VAR> × <VAR>Expr2</VAR></VAR></DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="//2"><VAR>+Expr1</VAR> <STRONG>/</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = <VAR>Expr1</VAR>/<VAR>Expr2</VAR></VAR> The the
flag <A class="flag" href="flags.html#flag:iso">iso</A> is <CODE>true</CODE>,
both arguments are converted to float and the return value is a float.
Otherwise (default), if both arguments are integers the operation
returns an integer if the division is exact. If at least one of the
arguments is rational and the other argument is integer, the operation
returns a rational number. In all other cases the return value is a
float. See also <A class="pred" href="arith.html#///2">///2</A> and <A NAME="idx:rdiv2:1024"></A><A class="pred" href="arith.html#rdiv/2">rdiv/2</A>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="mod/2"><VAR>+IntExpr1</VAR> <STRONG>mod</STRONG> <VAR>+IntExpr2</VAR></A></DT>
<DD class="defbody">
Modulo, defined as <VAR>Result</VAR> = <VAR>IntExpr1</VAR> - (<VAR>IntExpr1</VAR>
div <VAR>IntExpr2</VAR>) <VAR> × </VAR> <VAR>IntExpr2</VAR>, where <CODE>div</CODE>
is
<EM>floored</EM> division.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="rem/2"><VAR>+IntExpr1</VAR> <STRONG>rem</STRONG> <VAR>+IntExpr2</VAR></A></DT>
<DD class="defbody">
Remainder of integer division. Behaves as if defined by
<VAR>Result</VAR> is <VAR>IntExpr1</VAR> - (<VAR>IntExpr1</VAR> // <VAR>IntExpr2</VAR>) <VAR> × </VAR> <VAR>IntExpr2</VAR></DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="///2"><VAR>+IntExpr1</VAR> <STRONG>//</STRONG> <VAR>+IntExpr2</VAR></A></DT>
<DD class="defbody">
Integer division, defined as <VAR>Result</VAR> is <VAR>rnd_I</VAR>(<VAR>Expr1</VAR>/<VAR>Expr2</VAR>)
. The function <VAR>rnd_I</VAR> is the default rounding used by the
C-compiler and available through the Prolog flag
<A class="flag" href="flags.html#flag:integer_rounding_function">integer_rounding_function</A>.
In the C99 standard, C-rounding is defined as <CODE>towards_zero</CODE>.<SUP class="fn">59<SPAN class="fn-text">Future
versions might guarantee rounding towards zero.</SPAN></SUP></DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="div/2"><STRONG>div</STRONG>(<VAR>+IntExpr1,
+IntExpr2</VAR>)</A></DT>
<DD class="defbody">
Integer division, defined as <VAR>Result</VAR> is <VAR>(IntExpr1 -
IntExpr1 mod IntExpr2) // IntExpr2</VAR>. In other words, this is
integer division that rounds towards -infinity. This function guarantees
behaviour that is consistent with <A NAME="idx:mod2:1025"></A><A class="pred" href="arith.html#mod/2">mod/2</A>,
i.e., the following holds for every pair of integers <VAR>X,Y</VAR>
where <CODE>Y =\= 0</CODE>.
<PRE class="code">
Q is div(X, Y),
M is mod(X, Y),
X =:= Y*Q+M.
</PRE>
</DD>
<DT class="pubdef"><A NAME="rdiv/2"><VAR>+RatExpr</VAR> <STRONG>rdiv</STRONG> <VAR>+RatExpr</VAR></A></DT>
<DD class="defbody">
Rational number division. This function is only available if SWI-Prolog
has been compiled with rational number support. See
<A class="sec" href="arith.html">section 4.25.2.2</A> for details.</DD>
<DT class="pubdef"><A NAME="gcd/2"><VAR>+IntExpr1</VAR> <STRONG>gcd</STRONG> <VAR>+IntExpr2</VAR></A></DT>
<DD class="defbody">
Result is the greatest common divisor of <VAR>IntExpr1</VAR>, <VAR>IntExpr2</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="abs/1"><STRONG>abs</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Evaluate <VAR>Expr</VAR> and return the absolute value of it.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="sign/1"><STRONG>sign</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Evaluate to -1 if <VAR><VAR>Expr</VAR> < 0</VAR>, 1 if <VAR><VAR>Expr</VAR>
> 0</VAR> and 0 if
<VAR><VAR>Expr</VAR> = 0</VAR>.</DD>
<DT class="pubdef"><A NAME="max/2"><STRONG>max</STRONG>(<VAR>+Expr1,
+Expr2</VAR>)</A></DT>
<DD class="defbody">
Evaluates to the largest of both <VAR>Expr1</VAR> and <VAR>Expr2</VAR>.
Both arguments are compared after converting to the same type, but the
return value is in the original type. For example, max(2.5, 3) compares
the two values after converting to float, but returns the integer 3.</DD>
<DT class="pubdef"><A NAME="min/2"><STRONG>min</STRONG>(<VAR>+Expr1,
+Expr2</VAR>)</A></DT>
<DD class="defbody">
Evaluates to the smallest of both <VAR>Expr1</VAR> and <VAR>Expr2</VAR>.
See
<A NAME="idx:max2:1026"></A><A class="pred" href="arith.html#max/2">max/2</A>
for a description of type-handling.</DD>
<DT class="pubdef"><A NAME="./2"><STRONG>.</STRONG>(<VAR>+Int,[]</VAR>)</A></DT>
<DD class="defbody">
A list of one element evaluates to the element. This implies <CODE>"a"</CODE>
evaluates to the character code of the letter `a' (97). This option is
available for compatibility only. It will not work if `<CODE>style_check(+string)</CODE>'
is active as <CODE>"a"</CODE> will then be transformed into a string
object. The recommended way to specify the character code of the letter
`a' is <CODE>0'a</CODE>.</DD>
<DT class="pubdef"><A NAME="random/1"><STRONG>random</STRONG>(<VAR>+IntExpr</VAR>)</A></DT>
<DD class="defbody">
Evaluates to a random integer <VAR>i</VAR> for which <VAR>0 =< i < <VAR>IntExpr</VAR></VAR>.
The system has two implementations. If it is compiled with support for
unbounded arithmetic (default) it uses the GMP-library random functions.
In this case, each thread keeps its own random state. The default
algorithm is the <EM>Mersenne Twister</EM> algorithm. The seed is set
when the first random number in a thread is generated. If available, it
is set from <CODE>/dev/random</CODE>. Otherwise it is set from the
system clock. If unbounded arithmetic is not supported, random numbers
are shared between threads and the seed is initialised from the clock
when SWI-Prolog was started. The predicate <A NAME="idx:setrandom1:1027"></A><A class="pred" href="miscarith.html#set_random/1">set_random/1</A>
can be used to control the random number generator.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="round/1"><STRONG>round</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Evaluates <VAR>Expr</VAR> and rounds the result to the nearest integer.</DD>
<DT class="pubdef"><A NAME="integer/1"><STRONG>integer</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Same as <A NAME="idx:round1:1028"></A><A class="pred" href="arith.html#round/1">round/1</A>
(backward compatibility).</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="float/1"><STRONG>float</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Translate the result to a floating point number. Normally, Prolog will
use integers whenever possible. When used around the 2nd argument of
<A NAME="idx:is2:1029"></A><A class="pred" href="arith.html#is/2">is/2</A>,
the result will be returned as a floating point number. In other
contexts, the operation has no effect.</DD>
<DT class="pubdef"><A NAME="rational/1"><STRONG>rational</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Convert the <VAR>Expr</VAR> to a rational number or integer. The
function returns the input on integers and rational numbers. For
floating point numbers, the returned rational number <EM>exactly</EM>
represents the float. As floats cannot exactly represent all decimal
numbers the results may be surprising. In the examples below, doubles
can represent 0.25 and the result is as expected, in contrast to the
result of <CODE>rational(0.1)</CODE>. The function <A NAME="idx:rationalize1:1030"></A><A class="pred" href="arith.html#rationalize/1">rationalize/1</A>
remedies this. See <A class="sec" href="arith.html">section 4.25.2.2</A>
for more information on rational number support.
<PRE class="code">
?- A is rational(0.25).
A is 1 rdiv 4
?- A is rational(0.1).
A = 3602879701896397 rdiv 36028797018963968
</PRE>
</DD>
<DT class="pubdef"><A NAME="rationalize/1"><STRONG>rationalize</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Convert the <VAR>Expr</VAR> to a rational number or integer. The
function is similar to <A NAME="idx:rational1:1031"></A><A class="pred" href="typetest.html#rational/1">rational/1</A>,
but the result is only accurate within the rounding error of floating
point numbers, generally producing a much smaller denominator.<SUP class="fn">60<SPAN class="fn-text">The
names <A NAME="idx:rational1:1032"></A><A class="pred" href="typetest.html#rational/1">rational/1</A>
and <A NAME="idx:rationalize1:1033"></A><A class="pred" href="arith.html#rationalize/1">rationalize/1</A>
as well as their semantics are inspired by Common Lisp.</SPAN></SUP>
<PRE class="code">
?- A is rationalize(0.25).
A = 1 rdiv 4
?- A is rationalize(0.1).
A = 1 rdiv 10
</PRE>
</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="float_fractional_part/1"><STRONG>float_fractional_part</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Fractional part of a floating-point number. Negative if <VAR>Expr</VAR>
is negative, rational if <VAR>Expr</VAR> is rational and 0 if <VAR>Expr</VAR>
is integer. The following relation is always true:
<VAR>X is float_fractional_part(X) + float_integer_part(X)</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="float_integer_part/1"><STRONG>float_integer_part</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Integer part of floating-point number. Negative if <VAR>Expr</VAR> is
negative, <VAR>Expr</VAR> if <VAR>Expr</VAR> is integer.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="truncate/1"><STRONG>truncate</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Truncate <VAR>Expr</VAR> to an integer. If <VAR><VAR>Expr</VAR> >= 0</VAR>
this is the same as <CODE>floor(Expr)</CODE>. For <VAR><VAR>Expr</VAR> <
0</VAR> this is the same as
<CODE>ceil(Expr)</CODE>. E.i. truncate rounds towards zero.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="floor/1"><STRONG>floor</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Evaluates <VAR>Expr</VAR> and returns the largest integer smaller or
equal to the result of the evaluation.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="ceiling/1"><STRONG>ceiling</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Evaluates <VAR>Expr</VAR> and returns the smallest integer larger or
equal to the result of the evaluation.</DD>
<DT class="pubdef"><A NAME="ceil/1"><STRONG>ceil</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Same as <A NAME="idx:ceiling1:1034"></A><A class="pred" href="arith.html#ceiling/1">ceiling/1</A>
(backward compatibility).</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME=">>/2"><VAR>+IntExpr</VAR> <STRONG>>></STRONG> <VAR>+IntExpr</VAR></A></DT>
<DD class="defbody">
Bitwise shift <VAR>IntExpr1</VAR> by <VAR>IntExpr2</VAR> bits to the
right. The operation performs <EM>arithmetic shift</EM>, which implies
that the inserted most significant bits are copies of the original most
significant bit.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="<</2"><VAR>+IntExpr</VAR> <STRONG><<</STRONG> <VAR>+IntExpr</VAR></A></DT>
<DD class="defbody">
Bitwise shift <VAR>IntExpr1</VAR> by <VAR>IntExpr2</VAR> bits to the
left.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="\//2"><VAR>+IntExpr</VAR> <STRONG>\/</STRONG> <VAR>+IntExpr</VAR></A></DT>
<DD class="defbody">
Bitwise `or' <VAR>IntExpr1</VAR> and <VAR>IntExpr2</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="/\/2"><VAR>+IntExpr</VAR> <STRONG>/\</STRONG> <VAR>+IntExpr</VAR></A></DT>
<DD class="defbody">
Bitwise `and' <VAR>IntExpr1</VAR> and <VAR>IntExpr2</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="xor/2"><VAR>+IntExpr</VAR> <STRONG>xor</STRONG> <VAR>+IntExpr</VAR></A></DT>
<DD class="defbody">
Bitwise `exclusive or' <VAR>IntExpr1</VAR> and <VAR>IntExpr2</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="\/1"><STRONG>\</STRONG> <VAR>+IntExpr</VAR></A></DT>
<DD class="defbody">
Bitwise negation. The returned value is the one's complement of
<VAR>IntExpr</VAR>.</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="sqrt/1"><STRONG>sqrt</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = sqrt(<VAR>Expr</VAR>)</VAR>
</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="sin/1"><STRONG>sin</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = sin(<VAR>Expr</VAR>)</VAR>. <VAR>Expr</VAR> is
the angle in radians.
</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="cos/1"><STRONG>cos</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = cos(<VAR>Expr</VAR>)</VAR>. <VAR>Expr</VAR> is
the angle in radians.
</DD>
<DT class="pubdef"><A NAME="tan/1"><STRONG>tan</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = tan(<VAR>Expr</VAR>)</VAR>. <VAR>Expr</VAR> is
the angle in radians.
</DD>
<DT class="pubdef"><A NAME="asin/1"><STRONG>asin</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = arcsin(<VAR>Expr</VAR>)</VAR>. <VAR>Result</VAR>
is the angle in radians.
</DD>
<DT class="pubdef"><A NAME="acos/1"><STRONG>acos</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = arccos(<VAR>Expr</VAR>)</VAR>. <VAR>Result</VAR>
is the angle in radians.
</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="atan/1"><STRONG>atan</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = arctan(<VAR>Expr</VAR>)</VAR>. <VAR>Result</VAR>
is the angle in radians.
</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="atan2/2"><STRONG>atan2</STRONG>(<VAR>+YExpr,
+XExpr</VAR>)</A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = arctan(<VAR>YExpr</VAR>/<VAR>XExpr</VAR>)</VAR>. <VAR>Result</VAR>
is the angle in radians. The return value is in the range <VAR>[- pi ...
pi ]</VAR>. Used to convert between rectangular and polar coordinate
system.
</DD>
<DT class="pubdef"><A NAME="atan/2"><STRONG>atan</STRONG>(<VAR>+YExpr,
+XExpr</VAR>)</A></DT>
<DD class="defbody">
Same as <A NAME="idx:atan22:1035"></A><A class="pred" href="arith.html#atan2/2">atan2/2</A>
(backward compatibility).
</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="log/1"><STRONG>log</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Natural logarithm. <VAR><VAR>Result</VAR> = ln(<VAR>Expr</VAR>)</VAR>
</DD>
<DT class="pubdef"><A NAME="log10/1"><STRONG>log10</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Base-10 logarithm. <VAR><VAR>Result</VAR> = log10(<VAR>Expr</VAR>)</VAR>
</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="exp/1"><STRONG>exp</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = e **<VAR>Expr</VAR></VAR>
</DD>
<DT class="pubdef"><span class="pred-tag">[ISO]</span><A NAME="**/2"><VAR>+Expr1</VAR> <STRONG>**</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = <VAR>Expr1</VAR>**<VAR>Expr2</VAR></VAR>. With
unbounded integers and integer values for <VAR>Expr1</VAR> and a
non-negative integer
<VAR>Expr2</VAR>, the result is always integer. The integer expressions
<VAR>0 ** I</VAR>, <VAR>1 ** I</VAR> and <VAR>-1 ** I</VAR> are
guaranteed to work for any integer <VAR>I</VAR>. Other integer base
values generate a <CODE>resource</CODE> error if the result does not fit
in memory.
</DD>
<DT class="pubdef"><A NAME="powm/3"><STRONG>powm</STRONG>(<VAR>+IntExprBase,
+IntExprExp, +IntExprMod</VAR>)</A></DT>
<DD class="defbody">
<VAR><VAR>Result</VAR> = (<VAR>IntExprBase</VAR>**<VAR>IntExprExp</VAR>)
modulo <VAR>IntExprMod</VAR></VAR>. Only available when compiled with
unbounded integer support. This formula is required for Diffie-Hellman
key-exchange, a technique where two parties can establish a secret key
over a public network.
</DD>
<DT class="pubdef"><A NAME="^/2"><VAR>+Expr1</VAR> <STRONG>^</STRONG> <VAR>+Expr2</VAR></A></DT>
<DD class="defbody">
Same as **/2 (backward compatibility).
</DD>
<DT class="pubdef"><A NAME="pi/0"><STRONG>pi</STRONG></A></DT>
<DD class="defbody">
Evaluates to the mathematical constant <VAR>pi</VAR> (3.14159 ... ).
</DD>
<DT class="pubdef"><A NAME="e/0"><STRONG>e</STRONG></A></DT>
<DD class="defbody">
Evaluates to the mathematical constant <VAR>e</VAR> (2.71828 ... ).
</DD>
<DT class="pubdef"><A NAME="epsilon/0"><STRONG>epsilon</STRONG></A></DT>
<DD class="defbody">
Evaluates to the the difference between the float 1.0 and the first
larger floating point number.</DD>
<DT class="pubdef"><A NAME="cputime/0"><STRONG>cputime</STRONG></A></DT>
<DD class="defbody">
Evaluates to a floating point number expressing the <font size=-1>CPU</font>
time (in seconds) used by Prolog up till now. See also <A NAME="idx:statistics2:1036"></A><A class="pred" href="statistics.html#statistics/2">statistics/2</A>
and <A NAME="idx:time1:1037"></A><A class="pred" href="statistics.html#time/1">time/1</A>.</DD>
<DT class="pubdef"><A NAME="eval/1"><STRONG>eval</STRONG>(<VAR>+Expr</VAR>)</A></DT>
<DD class="defbody">
Evaluate <VAR>Expr</VAR>. Although ISO standard dictates that <VAR>A</VAR>=1+2, <VAR>B</VAR>
is
<VAR>A</VAR> works and unifies <VAR>B</VAR> to 3, it is widely felt that
source-level variables in arithmetic expressions should have been
limited to numbers. In this view the eval function can be used to
evaluate arbitrary expressions.<SUP class="fn">61<SPAN class="fn-text">The <A NAME="idx:eval1:1038"></A><A class="pred" href="arith.html#eval/1">eval/1</A>
function was first introduced by ECLiPSe and is under consideration for
YAP.</SPAN></SUP>
</DD>
</DL>
<P><B>Bitvector functions</B>
<P>The functions below are not covered by the standard. The <A NAME="idx:msb1:1039"></A><A class="pred" href="arith.html#msb/1">msb/1</A>
function is compatible with hProlog. The others are private extensions
that improve handling of ---unbounded--- integers as bit-vectors.
<DL class="latex">
<DT class="pubdef"><A NAME="msb/1"><STRONG>msb</STRONG>(<VAR>+IntExpr</VAR>)</A></DT>
<DD class="defbody">
Return the largest integer <VAR>N</VAR> such that <CODE>(IntExpr >> N) /\ 1 =:= 1</CODE>.
This is the (zero-origin) index of the most significant 1 bit in the
value of <VAR>IntExpr</VAR>, which must evaluate to a positive integer.
Errors for 0, negative integers, and non-integers.</DD>
<DT class="pubdef"><A NAME="lsb/1"><STRONG>lsb</STRONG>(<VAR>+IntExpr</VAR>)</A></DT>
<DD class="defbody">
Return the smallest integer <VAR>N</VAR> such that <CODE>(IntExpr >> N) /\ 1 =:= 1</CODE>.
This is the (zero-origin) index of the least significant 1 bit in the
value of IntExpr, which must evaluate to a positive integer. Errors for
0, negative integers, and non-integers.</DD>
<DT class="pubdef"><A NAME="popcount/1"><STRONG>popcount</STRONG>(<VAR>+IntExpr</VAR>)</A></DT>
<DD class="defbody">
Return the number of 1s in the binary representation of the non-negative
integer <VAR>IntExpr</VAR>.
</DD>
</DL>
<P></BODY></HTML>
|