numbers.texi 35.9 KB
 Glenn Morris committed Sep 06, 2007 1 2 @c -*-texinfo-*- @c This is part of the GNU Emacs Lisp Reference Manual.  Paul Eggert committed Jan 01, 2013 3 4 @c Copyright (C) 1990-1995, 1998-1999, 2001-2013 Free Software @c Foundation, Inc.  Glenn Morris committed Sep 06, 2007 5 @c See the file elisp.texi for copying conditions.  Glenn Morris committed May 26, 2012 6 @node Numbers  Glenn Morris committed Sep 06, 2007 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @chapter Numbers @cindex integers @cindex numbers GNU Emacs supports two numeric data types: @dfn{integers} and @dfn{floating point numbers}. Integers are whole numbers such as @minus{}3, 0, 7, 13, and 511. Their values are exact. Floating point numbers are numbers with fractional parts, such as @minus{}4.5, 0.0, or 2.71828. They can also be expressed in exponential notation: 1.5e2 equals 150; in this example, @samp{e2} stands for ten to the second power, and that is multiplied by 1.5. Floating point values are not exact; they have a fixed, limited amount of precision. @menu * Integer Basics:: Representation and range of integers.  Glenn Morris committed Jun 22, 2010 22 * Float Basics:: Representation and range of floating point.  Glenn Morris committed Sep 06, 2007 23 24 * Predicates on Numbers:: Testing for numbers. * Comparison of Numbers:: Equality and inequality predicates.  Glenn Morris committed Jun 22, 2010 25 * Numeric Conversions:: Converting float to integer and vice versa.  Glenn Morris committed Sep 06, 2007 26 27 28 29 30 31 32 33 34 35 36 * Arithmetic Operations:: How to add, subtract, multiply and divide. * Rounding Operations:: Explicitly rounding floating point numbers. * Bitwise Operations:: Logical and, or, not, shifting. * Math Functions:: Trig, exponential and logarithmic functions. * Random Numbers:: Obtaining random integers, predictable or not. @end menu @node Integer Basics @section Integer Basics The range of values for an integer depends on the machine. The  Glenn Morris committed Mar 02, 2010 37 minimum range is @minus{}536870912 to 536870911 (30 bits; i.e.,  Glenn Morris committed Sep 06, 2007 38 @ifnottex  Glenn Morris committed Mar 02, 2010 39 -2**29  Glenn Morris committed Sep 06, 2007 40 41 @end ifnottex @tex  Glenn Morris committed Mar 02, 2010 42 @math{-2^{29}}  Glenn Morris committed Sep 06, 2007 43 44 45 @end tex to @ifnottex  Paul Eggert committed Dec 22, 2012 46 2**29 @minus{} 1),  Glenn Morris committed Sep 06, 2007 47 48 @end ifnottex @tex  Glenn Morris committed Mar 02, 2010 49 @math{2^{29}-1}),  Glenn Morris committed Sep 06, 2007 50 @end tex  Chong Yidong committed Sep 30, 2012 51 52 but many machines provide a wider range. Many examples in this chapter assume the minimum integer width of 30 bits.  Glenn Morris committed Sep 06, 2007 53 54 55 @cindex overflow The Lisp reader reads an integer as a sequence of digits with optional  Paul Eggert committed May 03, 2011 56 57 initial sign and optional final period. An integer that is out of the Emacs range is treated as a floating-point number.  Glenn Morris committed Sep 06, 2007 58 59 60 61 62 63  @example 1 ; @r{The integer 1.} 1. ; @r{The integer 1.} +1 ; @r{Also the integer 1.} -1 ; @r{The integer @minus{}1.}  Paul Eggert committed May 03, 2011 64  1073741825 ; @r{The floating point number 1073741825.0.}  Glenn Morris committed Sep 06, 2007 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  0 ; @r{The integer 0.} -0 ; @r{The integer 0.} @end example @cindex integers in specific radix @cindex radix for reading an integer @cindex base for reading an integer @cindex hex numbers @cindex octal numbers @cindex reading numbers in hex, octal, and binary The syntax for integers in bases other than 10 uses @samp{#} followed by a letter that specifies the radix: @samp{b} for binary, @samp{o} for octal, @samp{x} for hex, or @samp{@var{radix}r} to specify radix @var{radix}. Case is not significant for the letter that specifies the radix. Thus, @samp{#b@var{integer}} reads @var{integer} in binary, and @samp{#@var{radix}r@var{integer}} reads @var{integer} in radix @var{radix}. Allowed values of @var{radix} run from 2 to 36. For example: @example #b101100 @result{} 44 #o54 @result{} 44 #x2c @result{} 44 #24r1k @result{} 44 @end example To understand how various functions work on integers, especially the bitwise operators (@pxref{Bitwise Operations}), it is often helpful to view the numbers in their binary form.  Glenn Morris committed Mar 02, 2010 95  In 30-bit binary, the decimal integer 5 looks like this:  Glenn Morris committed Sep 06, 2007 96 97  @example  Paul Eggert committed Jun 06, 2011 98 0000...000101 (30 bits total)  Glenn Morris committed Sep 06, 2007 99 100 101 @end example @noindent  Paul Eggert committed Jun 06, 2011 102 103 104 (The @samp{...} stands for enough bits to fill out a 30-bit word; in this case, @samp{...} stands for twenty 0 bits. Later examples also use the @samp{...} notation to make binary integers easier to read.)  Glenn Morris committed Sep 06, 2007 105 106 107 108  The integer @minus{}1 looks like this: @example  Paul Eggert committed Jun 06, 2011 109 1111...111111 (30 bits total)  Glenn Morris committed Sep 06, 2007 110 111 112 113 @end example @noindent @cindex two's complement  Glenn Morris committed Mar 02, 2010 114 @minus{}1 is represented as 30 ones. (This is called @dfn{two's  Glenn Morris committed Sep 06, 2007 115 116 117 118 119 120 121 complement} notation.) The negative integer, @minus{}5, is creating by subtracting 4 from @minus{}1. In binary, the decimal integer 4 is 100. Consequently, @minus{}5 looks like this: @example  Paul Eggert committed Jun 06, 2011 122 1111...111011 (30 bits total)  Glenn Morris committed Sep 06, 2007 123 124 @end example  Glenn Morris committed Mar 02, 2010 125 126  In this implementation, the largest 30-bit binary integer value is 536,870,911 in decimal. In binary, it looks like this:  Glenn Morris committed Sep 06, 2007 127 128  @example  Paul Eggert committed Jun 06, 2011 129 0111...111111 (30 bits total)  Glenn Morris committed Sep 06, 2007 130 131 132 @end example Since the arithmetic functions do not check whether integers go  Glenn Morris committed Mar 02, 2010 133 134 outside their range, when you add 1 to 536,870,911, the value is the negative integer @minus{}536,870,912:  Glenn Morris committed Sep 06, 2007 135 136  @example  Glenn Morris committed Mar 02, 2010 137 138 (+ 1 536870911) @result{} -536870912  Paul Eggert committed Jun 06, 2011 139  @result{} 1000...000000 (30 bits total)  Glenn Morris committed Sep 06, 2007 140 141 142 143 144 145 146 147 @end example Many of the functions described in this chapter accept markers for arguments in place of numbers. (@xref{Markers}.) Since the actual arguments to such functions may be either numbers or markers, we often give these arguments the name @var{number-or-marker}. When the argument value is a marker, its position value is used and its buffer is ignored.  Eli Zaretskii committed Sep 17, 2011 148 149 @cindex largest Lisp integer number @cindex maximum Lisp integer number  Glenn Morris committed Sep 06, 2007 150 151 152 153 154 @defvar most-positive-fixnum The value of this variable is the largest integer that Emacs Lisp can handle. @end defvar  Eli Zaretskii committed Sep 17, 2011 155 156 @cindex smallest Lisp integer number @cindex minimum Lisp integer number  Glenn Morris committed Sep 06, 2007 157 158 159 160 161 @defvar most-negative-fixnum The value of this variable is the smallest integer that Emacs Lisp can handle. It is negative. @end defvar  Chong Yidong committed Sep 30, 2012 162 163 164  In Emacs Lisp, text characters are represented by integers. Any integer between zero and the value of @code{max-char}, inclusive, is considered to be valid as a character. @xref{String Basics}.  Eli Zaretskii committed Nov 29, 2008 165   Glenn Morris committed Sep 06, 2007 166 167 168 @node Float Basics @section Floating Point Basics  Chong Yidong committed Jan 22, 2012 169 @cindex @acronym{IEEE} floating point  Glenn Morris committed Sep 06, 2007 170 171 172  Floating point numbers are useful for representing numbers that are not integral. The precise range of floating point numbers is machine-specific; it is the same as the range of the C data type  Chong Yidong committed Jan 22, 2012 173 @code{double} on the machine you are using. Emacs uses the  Chong Yidong committed Sep 30, 2012 174 175 @acronym{IEEE} floating point standard, which is supported by all modern computers.  Glenn Morris committed Sep 06, 2007 176   Chong Yidong committed Jan 22, 2012 177  The read syntax for floating point numbers requires either a decimal  Glenn Morris committed Sep 06, 2007 178 179 180 point (with at least one digit following), an exponent, or both. For example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2}, @samp{1.5e3}, and @samp{.15e4} are five ways of writing a floating point number whose  Chong Yidong committed Jan 22, 2012 181 182 183 184 185 186 value is 1500. They are all equivalent. You can also use a minus sign to write negative floating point numbers, as in @samp{-1.0}. Emacs Lisp treats @code{-0.0} as equal to ordinary zero (with respect to @code{equal} and @code{=}), even though the two are distinguishable in the @acronym{IEEE} floating point standard.  Glenn Morris committed Sep 06, 2007 187 188 189 190 191  @cindex positive infinity @cindex negative infinity @cindex infinity @cindex NaN  Chong Yidong committed Jan 22, 2012 192 193 194 195  The @acronym{IEEE} floating point standard supports positive infinity and negative infinity as floating point values. It also provides for a class of values called NaN or not-a-number''; numerical functions return such values in cases where there is no  Paul Eggert committed Dec 05, 2012 196 correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN@. (NaN  Chong Yidong committed Jan 22, 2012 197 198 values can also carry a sign, but for practical purposes there's no significant difference between different NaN values in Emacs Lisp.)  Paul Eggert committed Sep 10, 2012 199 200 201 202 203 204 205 206  When a function is documented to return a NaN, it returns an implementation-defined value when Emacs is running on one of the now-rare platforms that do not use @acronym{IEEE} floating point. For example, @code{(log -1.0)} typically returns a NaN, but on non-@acronym{IEEE} platforms it returns an implementation-defined value.  Chong Yidong committed Jan 22, 2012 207 Here are the read syntaxes for these special floating point values:  Glenn Morris committed Sep 06, 2007 208 209 210 211 212 213  @table @asis @item positive infinity @samp{1.0e+INF} @item negative infinity @samp{-1.0e+INF}  Paul Eggert committed May 03, 2011 214 @item Not-a-number  Glenn Morris committed Sep 06, 2007 215 216 217 @samp{0.0e+NaN} or @samp{-0.0e+NaN}. @end table  Chong Yidong committed Jan 22, 2012 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 @defun isnan number This predicate tests whether its argument is NaN, and returns @code{t} if so, @code{nil} otherwise. The argument must be a number. @end defun The following functions are specialized for handling floating point numbers: @defun frexp x This function returns a cons cell @code{(@var{sig} . @var{exp})}, where @var{sig} and @var{exp} are respectively the significand and exponent of the floating point number @var{x}: @smallexample @var{x} = @var{sig} * 2^@var{exp} @end smallexample @var{sig} is a floating point number between 0.5 (inclusive) and 1.0 (exclusive). If @var{x} is zero, the return value is @code{(0 . 0)}. @end defun  Glenn Morris committed Sep 06, 2007 238   Chong Yidong committed Jan 22, 2012 239 240 241 242 @defun ldexp sig &optional exp This function returns a floating point number corresponding to the significand @var{sig} and exponent @var{exp}. @end defun  Glenn Morris committed Sep 06, 2007 243   Chong Yidong committed Jan 22, 2012 244 245 246 247 248 @defun copysign x1 x2 This function copies the sign of @var{x2} to the value of @var{x1}, and returns the result. @var{x1} and @var{x2} must be floating point numbers. @end defun  Glenn Morris committed Sep 06, 2007 249 250 251  @defun logb number This function returns the binary exponent of @var{number}. More  Paul Eggert committed Sep 10, 2012 252 precisely, the value is the logarithm of |@var{number}| base 2, rounded  Glenn Morris committed Sep 06, 2007 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 down to an integer. @example (logb 10) @result{} 3 (logb 10.0e20) @result{} 69 @end example @end defun @node Predicates on Numbers @section Type Predicates for Numbers @cindex predicates for numbers The functions in this section test for numbers, or for a specific type of number. The functions @code{integerp} and @code{floatp} can take any type of Lisp object as argument (they would not be of much use otherwise), but the @code{zerop} predicate requires a number as its argument. See also @code{integer-or-marker-p} and @code{number-or-marker-p}, in @ref{Predicates on Markers}. @defun floatp object This predicate tests whether its argument is a floating point number and returns @code{t} if so, @code{nil} otherwise. @end defun @defun integerp object This predicate tests whether its argument is an integer, and returns @code{t} if so, @code{nil} otherwise. @end defun @defun numberp object This predicate tests whether its argument is a number (either integer or floating point), and returns @code{t} if so, @code{nil} otherwise. @end defun  Chong Yidong committed Jan 29, 2012 289 @defun natnump object  Glenn Morris committed Sep 06, 2007 290 @cindex natural numbers  Paul Eggert committed Jan 30, 2012 291 This predicate (whose name comes from the phrase natural number'')  Chong Yidong committed Jan 29, 2012 292 293 294 tests to see whether its argument is a nonnegative integer, and returns @code{t} if so, @code{nil} otherwise. 0 is considered non-negative.  Glenn Morris committed Sep 06, 2007 295   Chong Yidong committed Jan 29, 2012 296 297 @findex wholenump number This is a synonym for @code{natnump}.  Glenn Morris committed Sep 06, 2007 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 @end defun @defun zerop number This predicate tests whether its argument is zero, and returns @code{t} if so, @code{nil} otherwise. The argument must be a number. @code{(zerop x)} is equivalent to @code{(= x 0)}. @end defun @node Comparison of Numbers @section Comparison of Numbers @cindex number comparison @cindex comparing numbers To test numbers for numerical equality, you should normally use @code{=}, not @code{eq}. There can be many distinct floating point number objects with the same numeric value. If you use @code{eq} to compare them, then you test whether two values are the same @emph{object}. By contrast, @code{=} compares only the numeric values of the objects.  Chong Yidong committed Sep 30, 2012 319  In Emacs Lisp, each integer value is a unique Lisp object.  Glenn Morris committed Sep 06, 2007 320 Therefore, @code{eq} is equivalent to @code{=} where integers are  Chong Yidong committed Sep 30, 2012 321 322 323 324 325 326 327 328 concerned. It is sometimes convenient to use @code{eq} for comparing an unknown value with an integer, because @code{eq} does not report an error if the unknown value is not a number---it accepts arguments of any type. By contrast, @code{=} signals an error if the arguments are not numbers or markers. However, it is better programming practice to use @code{=} if you can, even for comparing integers. Sometimes it is useful to compare numbers with @code{equal}, which  Glenn Morris committed Sep 06, 2007 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 treats two numbers as equal if they have the same data type (both integers, or both floating point) and the same value. By contrast, @code{=} can treat an integer and a floating point number as equal. @xref{Equality Predicates}. There is another wrinkle: because floating point arithmetic is not exact, it is often a bad idea to check for equality of two floating point values. Usually it is better to test for approximate equality. Here's a function to do this: @example (defvar fuzz-factor 1.0e-6) (defun approx-equal (x y) (or (and (= x 0) (= y 0)) (< (/ (abs (- x y)) (max (abs x) (abs y))) fuzz-factor))) @end example @cindex CL note---integers vrs @code{eq} @quotation @b{Common Lisp note:} Comparing numbers in Common Lisp always requires @code{=} because Common Lisp implements multi-word integers, and two distinct integer objects can have the same numeric value. Emacs Lisp can have just one integer object for any given value because it has a limited range of integer values. @end quotation  Tassilo Horn committed Dec 20, 2013 357 358 359 @defun = number-or-marker &rest number-or-markers This function tests whether all its arguments are numerically equal, and returns @code{t} if so, @code{nil} otherwise.  Glenn Morris committed Sep 06, 2007 360 361 362 363 364 365 366 367 368 369 370 371 372 373 @end defun @defun eql value1 value2 This function acts like @code{eq} except when both arguments are numbers. It compares numbers by type and numeric value, so that @code{(eql 1.0 1)} returns @code{nil}, but @code{(eql 1.0 1.0)} and @code{(eql 1 1)} both return @code{t}. @end defun @defun /= number-or-marker1 number-or-marker2 This function tests whether its arguments are numerically equal, and returns @code{t} if they are not, and @code{nil} if they are. @end defun  Tassilo Horn committed Dec 20, 2013 374 375 376 377 @defun < number-or-marker &rest number-or-markers This function tests whether every argument is strictly less than the respective next argument. It returns @code{t} if so, @code{nil} otherwise.  Glenn Morris committed Sep 06, 2007 378 379 @end defun  Tassilo Horn committed Dec 20, 2013 380 381 382 @defun <= number-or-marker &rest number-or-markers This function tests whether every argument is less than or equal to the respective next argument. It returns @code{t} if so, @code{nil}  Glenn Morris committed Sep 06, 2007 383 384 385 otherwise. @end defun  Tassilo Horn committed Dec 20, 2013 386 387 388 @defun > number-or-marker &rest number-or-markers This function tests whether every argument is strictly greater than the respective next argument. It returns @code{t} if so, @code{nil}  Glenn Morris committed Sep 06, 2007 389 390 391 otherwise. @end defun  Tassilo Horn committed Dec 20, 2013 392 393 394 @defun >= number-or-marker &rest number-or-markers This function tests whether every argument is greater than or equal to the respective next argument. It returns @code{t} if so, @code{nil}  Glenn Morris committed Sep 06, 2007 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 otherwise. @end defun @defun max number-or-marker &rest numbers-or-markers This function returns the largest of its arguments. If any of the arguments is floating-point, the value is returned as floating point, even if it was given as an integer. @example (max 20) @result{} 20 (max 1 2.5) @result{} 2.5 (max 1 3 2.5) @result{} 3.0 @end example @end defun @defun min number-or-marker &rest numbers-or-markers This function returns the smallest of its arguments. If any of the arguments is floating-point, the value is returned as floating point, even if it was given as an integer. @example (min -4 1) @result{} -4 @end example @end defun @defun abs number This function returns the absolute value of @var{number}. @end defun @node Numeric Conversions @section Numeric Conversions @cindex rounding in conversions @cindex number conversions @cindex converting numbers To convert an integer to floating point, use the function @code{float}. @defun float number This returns @var{number} converted to floating point. If @var{number} is already a floating point number, @code{float} returns it unchanged. @end defun  Chong Yidong committed Sep 30, 2012 442 443 444 445  There are four functions to convert floating point numbers to integers; they differ in how they round. All accept an argument @var{number} and an optional argument @var{divisor}. Both arguments may be integers or floating point numbers. @var{divisor} may also be  Glenn Morris committed Sep 06, 2007 446 447 448 449 @code{nil}. If @var{divisor} is @code{nil} or omitted, these functions convert @var{number} to an integer, or return it unchanged if it already is an integer. If @var{divisor} is non-@code{nil}, they divide @var{number} by @var{divisor} and convert the result to an  Chong Yidong committed Sep 30, 2012 450 451 integer. integer. If @var{divisor} is zero (whether integer or floating-point), Emacs signals an @code{arith-error} error.  Glenn Morris committed Sep 06, 2007 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  @defun truncate number &optional divisor This returns @var{number}, converted to an integer by rounding towards zero. @example (truncate 1.2) @result{} 1 (truncate 1.7) @result{} 1 (truncate -1.2) @result{} -1 (truncate -1.7) @result{} -1 @end example @end defun @defun floor number &optional divisor This returns @var{number}, converted to an integer by rounding downward (towards negative infinity). If @var{divisor} is specified, this uses the kind of division operation that corresponds to @code{mod}, rounding downward. @example (floor 1.2) @result{} 1 (floor 1.7) @result{} 1 (floor -1.2) @result{} -2 (floor -1.7) @result{} -2 (floor 5.99 3) @result{} 1 @end example @end defun @defun ceiling number &optional divisor This returns @var{number}, converted to an integer by rounding upward (towards positive infinity). @example (ceiling 1.2) @result{} 2 (ceiling 1.7) @result{} 2 (ceiling -1.2) @result{} -1 (ceiling -1.7) @result{} -1 @end example @end defun @defun round number &optional divisor This returns @var{number}, converted to an integer by rounding towards the nearest integer. Rounding a value equidistant between two integers may choose the integer closer to zero, or it may prefer an even integer, depending on your machine. @example (round 1.2) @result{} 1 (round 1.7) @result{} 2 (round -1.2) @result{} -1 (round -1.7) @result{} -2 @end example @end defun @node Arithmetic Operations @section Arithmetic Operations @cindex arithmetic operations  Chong Yidong committed Sep 30, 2012 528 529 530 531 532 533  Emacs Lisp provides the traditional four arithmetic operations (addition, subtraction, multiplication, and division), as well as remainder and modulus functions, and functions to add or subtract 1. Except for @code{%}, each of these functions accepts both integer and floating point arguments, and returns a floating point number if any argument is a floating point number.  Glenn Morris committed Sep 06, 2007 534   Paul Eggert committed May 04, 2011 535  It is important to note that in Emacs Lisp, arithmetic functions  Paul Eggert committed Jun 06, 2011 536 537 do not check for overflow. Thus @code{(1+ 536870911)} may evaluate to @minus{}536870912, depending on your hardware.  Glenn Morris committed Sep 06, 2007 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  @defun 1+ number-or-marker This function returns @var{number-or-marker} plus 1. For example, @example (setq foo 4) @result{} 4 (1+ foo) @result{} 5 @end example This function is not analogous to the C operator @code{++}---it does not increment a variable. It just computes a sum. Thus, if we continue, @example foo @result{} 4 @end example If you want to increment the variable, you must use @code{setq}, like this: @example (setq foo (1+ foo)) @result{} 5 @end example @end defun @defun 1- number-or-marker This function returns @var{number-or-marker} minus 1. @end defun @defun + &rest numbers-or-markers This function adds its arguments together. When given no arguments, @code{+} returns 0. @example (+) @result{} 0 (+ 1) @result{} 1 (+ 1 2 3 4) @result{} 10 @end example @end defun @defun - &optional number-or-marker &rest more-numbers-or-markers The @code{-} function serves two purposes: negation and subtraction. When @code{-} has a single argument, the value is the negative of the argument. When there are multiple arguments, @code{-} subtracts each of the @var{more-numbers-or-markers} from @var{number-or-marker}, cumulatively. If there are no arguments, the result is 0. @example (- 10 1 2 3 4) @result{} 0 (- 10) @result{} -10 (-) @result{} 0 @end example @end defun @defun * &rest numbers-or-markers This function multiplies its arguments together, and returns the product. When given no arguments, @code{*} returns 1. @example (*) @result{} 1 (* 1) @result{} 1 (* 1 2 3 4) @result{} 24 @end example @end defun @defun / dividend divisor &rest divisors This function divides @var{dividend} by @var{divisor} and returns the quotient. If there are additional arguments @var{divisors}, then it divides @var{dividend} by each divisor in turn. Each argument may be a number or a marker.  Chong Yidong committed Sep 30, 2012 622 623 624 625 626 627 If all the arguments are integers, the result is an integer, obtained by rounding the quotient towards zero after each division. (Hypothetically, some machines may have different rounding behavior for negative arguments, because @code{/} is implemented using the C division operator, which permits machine-dependent rounding; but this does not happen in practice.)  Glenn Morris committed Sep 06, 2007 628 629 630 631 632 633  @example @group (/ 6 2) @result{} 3 @end group  Chong Yidong committed Sep 30, 2012 634 @group  Glenn Morris committed Sep 06, 2007 635 636 (/ 5 2) @result{} 2  Chong Yidong committed Sep 30, 2012 637 638 @end group @group  Glenn Morris committed Sep 06, 2007 639 640 (/ 5.0 2) @result{} 2.5  Chong Yidong committed Sep 30, 2012 641 642 @end group @group  Glenn Morris committed Sep 06, 2007 643 644 (/ 5 2.0) @result{} 2.5  Chong Yidong committed Sep 30, 2012 645 646 @end group @group  Glenn Morris committed Sep 06, 2007 647 648 (/ 5.0 2.0) @result{} 2.5  Chong Yidong committed Sep 30, 2012 649 650 @end group @group  Glenn Morris committed Sep 06, 2007 651 652 (/ 25 3 2) @result{} 4  Chong Yidong committed Sep 30, 2012 653 @end group  Glenn Morris committed Sep 06, 2007 654 655 @group (/ -17 6)  Chong Yidong committed Sep 30, 2012 656  @result{} -2  Glenn Morris committed Sep 06, 2007 657 658 @end group @end example  Chong Yidong committed Sep 30, 2012 659 660 661 662 663 664  @cindex @code{arith-error} in division If you divide an integer by the integer 0, Emacs signals an @code{arith-error} error (@pxref{Errors}). If you divide a floating point number by 0, or divide by the floating point number 0.0, the result is either positive or negative infinity (@pxref{Float Basics}).  Glenn Morris committed Sep 06, 2007 665 666 667 668 669 670 671 @end defun @defun % dividend divisor @cindex remainder This function returns the integer remainder after division of @var{dividend} by @var{divisor}. The arguments must be integers or markers.  Chong Yidong committed Sep 30, 2012 672 673 674 675 676 677 678 679 For any two integers @var{dividend} and @var{divisor}, @example @group (+ (% @var{dividend} @var{divisor}) (* (/ @var{dividend} @var{divisor}) @var{divisor})) @end group @end example  Glenn Morris committed Sep 06, 2007 680   Chong Yidong committed Sep 30, 2012 681 682 683 @noindent always equals @var{dividend}. If @var{divisor} is zero, Emacs signals an @code{arith-error} error.  Glenn Morris committed Sep 06, 2007 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703  @example (% 9 4) @result{} 1 (% -9 4) @result{} -1 (% 9 -4) @result{} 1 (% -9 -4) @result{} -1 @end example @end defun @defun mod dividend divisor @cindex modulus This function returns the value of @var{dividend} modulo @var{divisor}; in other words, the remainder after division of @var{dividend} by @var{divisor}, but with the same sign as @var{divisor}. The arguments must be numbers or markers.  Chong Yidong committed Sep 30, 2012 704 705 706 Unlike @code{%}, @code{mod} permits floating point arguments; it rounds the quotient downward (towards minus infinity) to an integer, and uses that quotient to compute the remainder.  Glenn Morris committed Sep 06, 2007 707   Paul Eggert committed Sep 10, 2012 708 709 If @var{divisor} is zero, @code{mod} signals an @code{arith-error} error if both arguments are integers, and returns a NaN otherwise.  Glenn Morris committed Sep 06, 2007 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 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788  @example @group (mod 9 4) @result{} 1 @end group @group (mod -9 4) @result{} 3 @end group @group (mod 9 -4) @result{} -3 @end group @group (mod -9 -4) @result{} -1 @end group @group (mod 5.5 2.5) @result{} .5 @end group @end example For any two numbers @var{dividend} and @var{divisor}, @example @group (+ (mod @var{dividend} @var{divisor}) (* (floor @var{dividend} @var{divisor}) @var{divisor})) @end group @end example @noindent always equals @var{dividend}, subject to rounding error if either argument is floating point. For @code{floor}, see @ref{Numeric Conversions}. @end defun @node Rounding Operations @section Rounding Operations @cindex rounding without conversion The functions @code{ffloor}, @code{fceiling}, @code{fround}, and @code{ftruncate} take a floating point argument and return a floating point result whose value is a nearby integer. @code{ffloor} returns the nearest integer below; @code{fceiling}, the nearest integer above; @code{ftruncate}, the nearest integer in the direction towards zero; @code{fround}, the nearest integer. @defun ffloor float This function rounds @var{float} to the next lower integral value, and returns that value as a floating point number. @end defun @defun fceiling float This function rounds @var{float} to the next higher integral value, and returns that value as a floating point number. @end defun @defun ftruncate float This function rounds @var{float} towards zero to an integral value, and returns that value as a floating point number. @end defun @defun fround float This function rounds @var{float} to the nearest integral value, and returns that value as a floating point number. @end defun @node Bitwise Operations @section Bitwise Operations on Integers @cindex bitwise arithmetic @cindex logical arithmetic In a computer, an integer is represented as a binary number, a sequence of @dfn{bits} (digits which are either zero or one). A bitwise operation acts on the individual bits of such a sequence. For example, @dfn{shifting} moves the whole sequence left or right one or more places,  Glenn Morris committed Apr 25, 2012 789 reproducing the same pattern moved over''.  Glenn Morris committed Sep 06, 2007 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858  The bitwise operations in Emacs Lisp apply only to integers. @defun lsh integer1 count @cindex logical shift @code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the bits in @var{integer1} to the left @var{count} places, or to the right if @var{count} is negative, bringing zeros into the vacated bits. If @var{count} is negative, @code{lsh} shifts zeros into the leftmost (most-significant) bit, producing a positive result even if @var{integer1} is negative. Contrast this with @code{ash}, below. Here are two examples of @code{lsh}, shifting a pattern of bits one place to the left. We show only the low-order eight bits of the binary pattern; the rest are all zero. @example @group (lsh 5 1) @result{} 10 ;; @r{Decimal 5 becomes decimal 10.} 00000101 @result{} 00001010 (lsh 7 1) @result{} 14 ;; @r{Decimal 7 becomes decimal 14.} 00000111 @result{} 00001110 @end group @end example @noindent As the examples illustrate, shifting the pattern of bits one place to the left produces a number that is twice the value of the previous number. Shifting a pattern of bits two places to the left produces results like this (with 8-bit binary numbers): @example @group (lsh 3 2) @result{} 12 ;; @r{Decimal 3 becomes decimal 12.} 00000011 @result{} 00001100 @end group @end example On the other hand, shifting one place to the right looks like this: @example @group (lsh 6 -1) @result{} 3 ;; @r{Decimal 6 becomes decimal 3.} 00000110 @result{} 00000011 @end group @group (lsh 5 -1) @result{} 2 ;; @r{Decimal 5 becomes decimal 2.} 00000101 @result{} 00000010 @end group @end example @noindent As the example illustrates, shifting one place to the right divides the value of a positive integer by two, rounding downward.  Paul Eggert committed May 04, 2011 859 The function @code{lsh}, like all Emacs Lisp arithmetic functions, does  Glenn Morris committed Sep 06, 2007 860 861 not check for overflow, so shifting left can discard significant bits and change the sign of the number. For example, left shifting  Paul Eggert committed Jun 06, 2011 862 536,870,911 produces @minus{}2 in the 30-bit implementation:  Glenn Morris committed Sep 06, 2007 863 864  @example  Glenn Morris committed Mar 02, 2010 865 (lsh 536870911 1) ; @r{left shift}  Glenn Morris committed Sep 06, 2007 866 867 868  @result{} -2 @end example  Paul Eggert committed Jun 06, 2011 869 In binary, the argument looks like this:  Glenn Morris committed Sep 06, 2007 870 871 872  @example @group  Glenn Morris committed Mar 02, 2010 873 ;; @r{Decimal 536,870,911}  Paul Eggert committed Jun 06, 2011 874 0111...111111 (30 bits total)  Glenn Morris committed Sep 06, 2007 875 876 877 878 879 880 881 882 883 @end group @end example @noindent which becomes the following when left shifted: @example @group ;; @r{Decimal @minus{}2}  Paul Eggert committed Jun 06, 2011 884 1111...111110 (30 bits total)  Glenn Morris committed Sep 06, 2007 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 @end group @end example @end defun @defun ash integer1 count @cindex arithmetic shift @code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1} to the left @var{count} places, or to the right if @var{count} is negative. @code{ash} gives the same results as @code{lsh} except when @var{integer1} and @var{count} are both negative. In that case, @code{ash} puts ones in the empty bit positions on the left, while @code{lsh} puts zeros in those bit positions. Thus, with @code{ash}, shifting the pattern of bits one place to the right looks like this: @example @group (ash -6 -1) @result{} -3 ;; @r{Decimal @minus{}6 becomes decimal @minus{}3.}  Paul Eggert committed Jun 06, 2011 907 1111...111010 (30 bits total)  Glenn Morris committed Sep 06, 2007 908  @result{}  Paul Eggert committed Jun 06, 2011 909 1111...111101 (30 bits total)  Glenn Morris committed Sep 06, 2007 910 911 912 913 914 915 916 917 @end group @end example In contrast, shifting the pattern of bits one place to the right with @code{lsh} looks like this: @example @group  Glenn Morris committed Mar 02, 2010 918 919 (lsh -6 -1) @result{} 536870909 ;; @r{Decimal @minus{}6 becomes decimal 536,870,909.}  Paul Eggert committed Jun 06, 2011 920 1111...111010 (30 bits total)  Glenn Morris committed Sep 06, 2007 921  @result{}  Paul Eggert committed Jun 06, 2011 922 0111...111101 (30 bits total)  Glenn Morris committed Sep 06, 2007 923 924 925 926 927 928 929 930 931 @end group @end example Here are other examples: @c !!! Check if lined up in smallbook format! XDVI shows problem @c with smallbook but not with regular book! --rjc 16mar92 @smallexample @group  Paul Eggert committed Jun 06, 2011 932  ; @r{ 30-bit binary values}  Glenn Morris committed Sep 06, 2007 933   Paul Eggert committed Jun 03, 2011 934 935 (lsh 5 2) ; 5 = @r{0000...000101} @result{} 20 ; = @r{0000...010100}  Glenn Morris committed Sep 06, 2007 936 937 938 939 @end group @group (ash 5 2) @result{} 20  Paul Eggert committed Jun 03, 2011 940 941 (lsh -5 2) ; -5 = @r{1111...111011} @result{} -20 ; = @r{1111...101100}  Glenn Morris committed Sep 06, 2007 942 943 944 945 (ash -5 2) @result{} -20 @end group @group  Paul Eggert committed Jun 03, 2011 946 947 (lsh 5 -2) ; 5 = @r{0000...000101} @result{} 1 ; = @r{0000...000001}  Glenn Morris committed Sep 06, 2007 948 949 950 951 952 953 @end group @group (ash 5 -2) @result{} 1 @end group @group  Paul Eggert committed Jun 03, 2011 954 (lsh -5 -2) ; -5 = @r{1111...111011}  Paul Eggert committed Jun 06, 2011 955  @result{} 268435454  Paul Eggert committed Jun 03, 2011 956  ; = @r{0011...111110}  Glenn Morris committed Sep 06, 2007 957 958 @end group @group  Paul Eggert committed Jun 03, 2011 959 960 (ash -5 -2) ; -5 = @r{1111...111011} @result{} -2 ; = @r{1111...111110}  Glenn Morris committed Sep 06, 2007 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 @end group @end smallexample @end defun @defun logand &rest ints-or-markers This function returns the logical and'' of the arguments: the @var{n}th bit is set in the result if, and only if, the @var{n}th bit is set in all the arguments. (Set'' means that the value of the bit is 1 rather than 0.) For example, using 4-bit binary numbers, the logical and'' of 13 and 12 is 12: 1101 combined with 1100 produces 1100. In both the binary numbers, the leftmost two bits are set (i.e., they are 1's), so the leftmost two bits of the returned value are set. However, for the rightmost two bits, each is zero in at least one of the arguments, so the rightmost two bits of the returned value are 0's. @noindent Therefore, @example @group (logand 13 12) @result{} 12 @end group @end example If @code{logand} is not passed any argument, it returns a value of @minus{}1. This number is an identity element for @code{logand} because its binary representation consists entirely of ones. If @code{logand} is passed just one argument, it returns that argument. @smallexample @group  Paul Eggert committed Jun 06, 2011 995  ; @r{ 30-bit binary values}  Glenn Morris committed Sep 06, 2007 996   Paul Eggert committed Jun 03, 2011 997 998 999 (logand 14 13) ; 14 = @r{0000...001110} ; 13 = @r{0000...001101} @result{} 12 ; 12 = @r{0000...001100}  Glenn Morris committed Sep 06, 2007 1000 1001 1002 @end group @group  Paul Eggert committed Jun 03, 2011 1003 1004 1005 1006 (logand 14 13 4) ; 14 = @r{0000...001110} ; 13 = @r{0000...001101} ; 4 = @r{0000...000100} @result{} 4 ; 4 = @r{0000...000100}  Glenn Morris committed Sep 06, 2007 1007 1008 1009 1010 @end group @group (logand)  Paul Eggert committed Jun 03, 2011 1011  @result{} -1 ; -1 = @r{1111...111111}  Glenn Morris committed Sep 06, 2007 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 @end group @end smallexample @end defun @defun logior &rest ints-or-markers This function returns the inclusive or'' of its arguments: the @var{n}th bit is set in the result if, and only if, the @var{n}th bit is set in at least one of the arguments. If there are no arguments, the result is zero, which is an identity element for this operation. If @code{logior} is passed just one argument, it returns that argument. @smallexample @group  Paul Eggert committed Jun 06, 2011 1025  ; @r{ 30-bit binary values}  Glenn Morris committed Sep 06, 2007 1026   Paul Eggert committed Jun 03, 2011 1027 1028 1029 (logior 12 5) ; 12 = @r{0000...001100} ; 5 = @r{0000...000101} @result{} 13 ; 13 = @r{0000...001101}  Glenn Morris committed Sep 06, 2007 1030 1031 1032 @end group @group  Paul Eggert committed Jun 03, 2011 1033 1034 1035 1036 (logior 12 5 7) ; 12 = @r{0000...001100} ; 5 = @r{0000...000101} ; 7 = @r{0000...000111} @result{} 15 ; 15 = @r{0000...001111}  Glenn Morris committed Sep 06, 2007 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 @end group @end smallexample @end defun @defun logxor &rest ints-or-markers This function returns the exclusive or'' of its arguments: the @var{n}th bit is set in the result if, and only if, the @var{n}th bit is set in an odd number of the arguments. If there are no arguments, the result is 0, which is an identity element for this operation. If @code{logxor} is passed just one argument, it returns that argument. @smallexample @group  Paul Eggert committed Jun 06, 2011 1050  ; @r{ 30-bit binary values}  Glenn Morris committed Sep 06, 2007 1051   Paul Eggert committed Jun 03, 2011 1052 1053 1054 (logxor 12 5) ; 12 = @r{0000...001100} ; 5 = @r{0000...000101} @result{} 9 ; 9 = @r{0000...001001}  Glenn Morris committed Sep 06, 2007 1055 1056 1057 @end group @group  Paul Eggert committed Jun 03, 2011 1058 1059 1060 1061 (logxor 12 5 7) ; 12 = @r{0000...001100} ; 5 = @r{0000...000101} ; 7 = @r{0000...000111} @result{} 14 ; 14 = @r{0000...001110}  Glenn Morris committed Sep 06, 2007 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 @end group @end smallexample @end defun @defun lognot integer This function returns the logical complement of its argument: the @var{n}th bit is one in the result if, and only if, the @var{n}th bit is zero in @var{integer}, and vice-versa. @example (lognot 5) @result{} -6  Paul Eggert committed Jun 06, 2011 1074 ;; 5 = @r{0000...000101} (30 bits total)  Glenn Morris committed Sep 06, 2007 1075 ;; @r{becomes}  Paul Eggert committed Jun 06, 2011 1076 ;; -6 = @r{1111...111010} (30 bits total)  Glenn Morris committed Sep 06, 2007 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 @end example @end defun @node Math Functions @section Standard Mathematical Functions @cindex transcendental functions @cindex mathematical functions @cindex floating-point functions These mathematical functions allow integers as well as floating point numbers as arguments. @defun sin arg @defunx cos arg @defunx tan arg  Chong Yidong committed Sep 30, 2012 1092 1093 These are the basic trigonometric functions, with argument @var{arg} measured in radians.  Glenn Morris committed Sep 06, 2007 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 @end defun @defun asin arg The value of @code{(asin @var{arg})} is a number between @ifnottex @minus{}pi/2 @end ifnottex @tex @math{-\pi/2} @end tex and @ifnottex pi/2 @end ifnottex @tex @math{\pi/2} @end tex  Paul Eggert committed Sep 10, 2012 1111 1112 (inclusive) whose sine is @var{arg}. If @var{arg} is out of range (outside [@minus{}1, 1]), @code{asin} returns a NaN.  Glenn Morris committed Sep 06, 2007 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 @end defun @defun acos arg The value of @code{(acos @var{arg})} is a number between 0 and @ifnottex pi @end ifnottex @tex @math{\pi} @end tex  Paul Eggert committed Sep 10, 2012 1123 1124 (inclusive) whose cosine is @var{arg}. If @var{arg} is out of range (outside [@minus{}1, 1]), @code{acos} returns a NaN.  Glenn Morris committed Sep 06, 2007 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 @end defun @defun atan y &optional x The value of @code{(atan @var{y})} is a number between @ifnottex @minus{}pi/2 @end ifnottex @tex @math{-\pi/2} @end tex and @ifnottex pi/2 @end ifnottex @tex @math{\pi/2} @end tex (exclusive) whose tangent is @var{y}. If the optional second argument @var{x} is given, the value of @code{(atan y x)} is the angle in radians between the vector @code{[@var{x}, @var{y}]} and the @code{X} axis. @end defun @defun exp arg  Chong Yidong committed Jan 22, 2012 1149 1150 This is the exponential function; it returns @math{e} to the power @var{arg}.  Glenn Morris committed Sep 06, 2007 1151 1152 1153 @end defun @defun log arg &optional base  Chong Yidong committed Jan 22, 2012 1154 1155 This function returns the logarithm of @var{arg}, with base @var{base}. If you don't specify @var{base}, the natural base  Paul Eggert committed Sep 10, 2012 1156 1157 @math{e} is used. If @var{arg} or @var{base} is negative, @code{log} returns a NaN.  Glenn Morris committed Sep 06, 2007 1158 1159 1160 1161 @end defun @defun expt x y This function returns @var{x} raised to power @var{y}. If both  Paul Eggert committed May 04, 2011 1162 1163 arguments are integers and @var{y} is positive, the result is an integer; in this case, overflow causes truncation, so watch out.  Paul Eggert committed Sep 10, 2012 1164 1165 If @var{x} is a finite negative number and @var{y} is a finite non-integer, @code{expt} returns a NaN.  Glenn Morris committed Sep 06, 2007 1166 1167 1168 1169 @end defun @defun sqrt arg This returns the square root of @var{arg}. If @var{arg} is negative,  Paul Eggert committed Sep 10, 2012 1170 @code{sqrt} returns a NaN.  Glenn Morris committed Sep 06, 2007 1171 1172 @end defun  Chong Yidong committed Jan 22, 2012 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 In addition, Emacs defines the following common mathematical constants: @defvar float-e The mathematical constant @math{e} (2.71828@dots{}). @end defvar @defvar float-pi The mathematical constant @math{pi} (3.14159@dots{}). @end defvar  Glenn Morris committed Sep 06, 2007 1184 1185 1186 1187 @node Random Numbers @section Random Numbers @cindex random numbers  Chong Yidong committed Sep 30, 2012 1188 1189 1190 1191 1192 1193  A deterministic computer program cannot generate true random numbers. For most purposes, @dfn{pseudo-random numbers} suffice. A series of pseudo-random numbers is generated in a deterministic fashion. The numbers are not truly random, but they have certain properties that mimic a random series. For example, all possible values occur equally often in a pseudo-random series.  Glenn Morris committed Sep 06, 2007 1194   Chong Yidong committed Sep 30, 2012 1195 1196 1197 1198 1199  Pseudo-random numbers are generated from a seed''. Starting from any given seed, the @code{random} function always generates the same sequence of numbers. By default, Emacs initializes the random seed at startup, in such a way that the sequence of values of @code{random} (with overwhelming likelihood) differs in each Emacs run.  Paul Eggert committed Aug 31, 2012 1200   Chong Yidong committed Sep 30, 2012 1201  Sometimes you want the random number sequence to be repeatable. For  Paul Eggert committed Aug 31, 2012 1202 1203 1204 1205 1206 1207 example, when debugging a program whose behavior depends on the random number sequence, it is helpful to get the same behavior in each program run. To make the sequence repeat, execute @code{(random "")}. This sets the seed to a constant value for your particular Emacs executable (though it may differ for other Emacs builds). You can use other strings to choose various seed values.  Glenn Morris committed Sep 06, 2007 1208 1209 1210 1211 1212 1213  @defun random &optional limit This function returns a pseudo-random integer. Repeated calls return a series of pseudo-random integers. If @var{limit} is a positive integer, the value is chosen to be  Chong Yidong committed Sep 30, 2012 1214 nonnegative and less than @var{limit}. Otherwise, the value might be  Paul Eggert committed Dec 05, 2012 1215 any integer representable in Lisp, i.e., an integer between  Chong Yidong committed Sep 30, 2012 1216 1217 @code{most-negative-fixnum} and @code{most-positive-fixnum} (@pxref{Integer Basics}).  Glenn Morris committed Sep 06, 2007 1218 1219 1220 1221  If @var{limit} is @code{t}, it means to choose a new seed based on the current time of day and on Emacs's process @acronym{ID} number.  Paul Eggert committed Aug 31, 2012 1222 1223 1224 If @var{limit} is a string, it means to choose a new seed based on the string's contents.  Glenn Morris committed Sep 06, 2007 1225 @end defun