numbers.texi 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990-1995, 1998-1999, 2001-2015 Free Software
  4. @c Foundation, Inc.
  5. @c See the file elisp.texi for copying conditions.
  6. @node Numbers
  7. @chapter Numbers
  8. @cindex integers
  9. @cindex numbers
  10. GNU Emacs supports two numeric data types: @dfn{integers} and
  11. @dfn{floating-point numbers}. Integers are whole numbers such as
  12. @minus{}3, 0, 7, 13, and 511. Floating-point numbers are numbers with
  13. fractional parts, such as @minus{}4.5, 0.0, and 2.71828. They can
  14. also be expressed in exponential notation: @samp{1.5e2} is the same as
  15. @samp{150.0}; here, @samp{e2} stands for ten to the second power, and
  16. that is multiplied by 1.5. Integer computations are exact, though
  17. they may overflow. Floating-point computations often involve rounding
  18. errors, as the numbers have a fixed amount of precision.
  19. @menu
  20. * Integer Basics:: Representation and range of integers.
  21. * Float Basics:: Representation and range of floating point.
  22. * Predicates on Numbers:: Testing for numbers.
  23. * Comparison of Numbers:: Equality and inequality predicates.
  24. * Numeric Conversions:: Converting float to integer and vice versa.
  25. * Arithmetic Operations:: How to add, subtract, multiply and divide.
  26. * Rounding Operations:: Explicitly rounding floating-point numbers.
  27. * Bitwise Operations:: Logical and, or, not, shifting.
  28. * Math Functions:: Trig, exponential and logarithmic functions.
  29. * Random Numbers:: Obtaining random integers, predictable or not.
  30. @end menu
  31. @node Integer Basics
  32. @section Integer Basics
  33. The range of values for an integer depends on the machine. The
  34. minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e.,
  35. @ifnottex
  36. @minus{}2**29
  37. @end ifnottex
  38. @tex
  39. @math{-2^{29}}
  40. @end tex
  41. to
  42. @ifnottex
  43. 2**29 @minus{} 1),
  44. @end ifnottex
  45. @tex
  46. @math{2^{29}-1}),
  47. @end tex
  48. but many machines provide a wider range. Many examples in this
  49. chapter assume the minimum integer width of 30 bits.
  50. @cindex overflow
  51. The Lisp reader reads an integer as a sequence of digits with optional
  52. initial sign and optional final period. An integer that is out of the
  53. Emacs range is treated as a floating-point number.
  54. @example
  55. 1 ; @r{The integer 1.}
  56. 1. ; @r{The integer 1.}
  57. +1 ; @r{Also the integer 1.}
  58. -1 ; @r{The integer @minus{}1.}
  59. 9000000000000000000
  60. ; @r{The floating-point number 9e18.}
  61. 0 ; @r{The integer 0.}
  62. -0 ; @r{The integer 0.}
  63. @end example
  64. @cindex integers in specific radix
  65. @cindex radix for reading an integer
  66. @cindex base for reading an integer
  67. @cindex hex numbers
  68. @cindex octal numbers
  69. @cindex reading numbers in hex, octal, and binary
  70. The syntax for integers in bases other than 10 uses @samp{#}
  71. followed by a letter that specifies the radix: @samp{b} for binary,
  72. @samp{o} for octal, @samp{x} for hex, or @samp{@var{radix}r} to
  73. specify radix @var{radix}. Case is not significant for the letter
  74. that specifies the radix. Thus, @samp{#b@var{integer}} reads
  75. @var{integer} in binary, and @samp{#@var{radix}r@var{integer}} reads
  76. @var{integer} in radix @var{radix}. Allowed values of @var{radix} run
  77. from 2 to 36. For example:
  78. @example
  79. #b101100 @result{} 44
  80. #o54 @result{} 44
  81. #x2c @result{} 44
  82. #24r1k @result{} 44
  83. @end example
  84. To understand how various functions work on integers, especially the
  85. bitwise operators (@pxref{Bitwise Operations}), it is often helpful to
  86. view the numbers in their binary form.
  87. In 30-bit binary, the decimal integer 5 looks like this:
  88. @example
  89. 0000...000101 (30 bits total)
  90. @end example
  91. @noindent
  92. (The @samp{...} stands for enough bits to fill out a 30-bit word; in
  93. this case, @samp{...} stands for twenty 0 bits. Later examples also
  94. use the @samp{...} notation to make binary integers easier to read.)
  95. The integer @minus{}1 looks like this:
  96. @example
  97. 1111...111111 (30 bits total)
  98. @end example
  99. @noindent
  100. @cindex two's complement
  101. @minus{}1 is represented as 30 ones. (This is called @dfn{two's
  102. complement} notation.)
  103. Subtracting 4 from @minus{}1 returns the negative integer @minus{}5.
  104. In binary, the decimal integer 4 is 100. Consequently,
  105. @minus{}5 looks like this:
  106. @example
  107. 1111...111011 (30 bits total)
  108. @end example
  109. In this implementation, the largest 30-bit binary integer is
  110. 536,870,911 in decimal. In binary, it looks like this:
  111. @example
  112. 0111...111111 (30 bits total)
  113. @end example
  114. Since the arithmetic functions do not check whether integers go
  115. outside their range, when you add 1 to 536,870,911, the value is the
  116. negative integer @minus{}536,870,912:
  117. @example
  118. (+ 1 536870911)
  119. @result{} -536870912
  120. @result{} 1000...000000 (30 bits total)
  121. @end example
  122. Many of the functions described in this chapter accept markers for
  123. arguments in place of numbers. (@xref{Markers}.) Since the actual
  124. arguments to such functions may be either numbers or markers, we often
  125. give these arguments the name @var{number-or-marker}. When the argument
  126. value is a marker, its position value is used and its buffer is ignored.
  127. @cindex largest Lisp integer
  128. @cindex maximum Lisp integer
  129. @defvar most-positive-fixnum
  130. The value of this variable is the largest integer that Emacs Lisp can
  131. handle. Typical values are
  132. @ifnottex
  133. 2**29 @minus{} 1
  134. @end ifnottex
  135. @tex
  136. @math{2^{29}-1}
  137. @end tex
  138. on 32-bit and
  139. @ifnottex
  140. 2**61 @minus{} 1
  141. @end ifnottex
  142. @tex
  143. @math{2^{61}-1}
  144. @end tex
  145. on 64-bit platforms.
  146. @end defvar
  147. @cindex smallest Lisp integer
  148. @cindex minimum Lisp integer
  149. @defvar most-negative-fixnum
  150. The value of this variable is the smallest integer that Emacs Lisp can
  151. handle. It is negative. Typical values are
  152. @ifnottex
  153. @minus{}2**29
  154. @end ifnottex
  155. @tex
  156. @math{-2^{29}}
  157. @end tex
  158. on 32-bit and
  159. @ifnottex
  160. @minus{}2**61
  161. @end ifnottex
  162. @tex
  163. @math{-2^{61}}
  164. @end tex
  165. on 64-bit platforms.
  166. @end defvar
  167. In Emacs Lisp, text characters are represented by integers. Any
  168. integer between zero and the value of @code{(max-char)}, inclusive, is
  169. considered to be valid as a character. @xref{Character Codes}.
  170. @node Float Basics
  171. @section Floating-Point Basics
  172. @cindex @acronym{IEEE} floating point
  173. Floating-point numbers are useful for representing numbers that are
  174. not integral. The range of floating-point numbers is
  175. the same as the range of the C data type @code{double} on the machine
  176. you are using. On all computers currently supported by Emacs, this is
  177. double-precision @acronym{IEEE} floating point.
  178. The read syntax for floating-point numbers requires either a decimal
  179. point, an exponent, or both. Optional signs (@samp{+} or @samp{-})
  180. precede the number and its exponent. For example, @samp{1500.0},
  181. @samp{+15e2}, @samp{15.0e+2}, @samp{+1500000e-3}, and @samp{.15e4} are
  182. five ways of writing a floating-point number whose value is 1500.
  183. They are all equivalent. Like Common Lisp, Emacs Lisp requires at
  184. least one digit after any decimal point in a floating-point number;
  185. @samp{1500.} is an integer, not a floating-point number.
  186. Emacs Lisp treats @code{-0.0} as numerically equal to ordinary zero
  187. with respect to @code{equal} and @code{=}. This follows the
  188. @acronym{IEEE} floating-point standard, which says @code{-0.0} and
  189. @code{0.0} are numerically equal even though other operations can
  190. distinguish them.
  191. @cindex positive infinity
  192. @cindex negative infinity
  193. @cindex infinity
  194. @cindex NaN
  195. The @acronym{IEEE} floating-point standard supports positive
  196. infinity and negative infinity as floating-point values. It also
  197. provides for a class of values called NaN or ``not-a-number'';
  198. numerical functions return such values in cases where there is no
  199. correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN@.
  200. Although NaN values carry a sign, for practical purposes there is no other
  201. significant difference between different NaN values in Emacs Lisp.
  202. Here are read syntaxes for these special floating-point values:
  203. @table @asis
  204. @item infinity
  205. @samp{1.0e+INF} and @samp{-1.0e+INF}
  206. @item not-a-number
  207. @samp{0.0e+NaN} and @samp{-0.0e+NaN}
  208. @end table
  209. The following functions are specialized for handling floating-point
  210. numbers:
  211. @defun isnan x
  212. This predicate returns @code{t} if its floating-point argument is a NaN,
  213. @code{nil} otherwise.
  214. @end defun
  215. @defun frexp x
  216. This function returns a cons cell @code{(@var{s} . @var{e})},
  217. where @var{s} and @var{e} are respectively the significand and
  218. exponent of the floating-point number @var{x}.
  219. If @var{x} is finite, then @var{s} is a floating-point number between 0.5
  220. (inclusive) and 1.0 (exclusive), @var{e} is an integer, and
  221. @ifnottex
  222. @var{x} = @var{s} * 2**@var{e}.
  223. @end ifnottex
  224. @tex
  225. @math{x = s 2^e}.
  226. @end tex
  227. If @var{x} is zero or infinity, then @var{s} is the same as @var{x}.
  228. If @var{x} is a NaN, then @var{s} is also a NaN@.
  229. If @var{x} is zero, then @var{e} is 0.
  230. @end defun
  231. @defun ldexp s e
  232. Given a numeric significand @var{s} and an integer exponent @var{e},
  233. this function returns the floating point number
  234. @ifnottex
  235. @var{s} * 2**@var{e}.
  236. @end ifnottex
  237. @tex
  238. @math{s 2^e}.
  239. @end tex
  240. @end defun
  241. @defun copysign x1 x2
  242. This function copies the sign of @var{x2} to the value of @var{x1},
  243. and returns the result. @var{x1} and @var{x2} must be floating point.
  244. @end defun
  245. @defun logb x
  246. This function returns the binary exponent of @var{x}. More
  247. precisely, the value is the logarithm base 2 of @math{|x|}, rounded
  248. down to an integer.
  249. @example
  250. (logb 10)
  251. @result{} 3
  252. (logb 10.0e20)
  253. @result{} 69
  254. @end example
  255. @end defun
  256. @node Predicates on Numbers
  257. @section Type Predicates for Numbers
  258. @cindex predicates for numbers
  259. The functions in this section test for numbers, or for a specific
  260. type of number. The functions @code{integerp} and @code{floatp} can
  261. take any type of Lisp object as argument (they would not be of much
  262. use otherwise), but the @code{zerop} predicate requires a number as
  263. its argument. See also @code{integer-or-marker-p} and
  264. @code{number-or-marker-p}, in @ref{Predicates on Markers}.
  265. @defun floatp object
  266. This predicate tests whether its argument is floating point
  267. and returns @code{t} if so, @code{nil} otherwise.
  268. @end defun
  269. @defun integerp object
  270. This predicate tests whether its argument is an integer, and returns
  271. @code{t} if so, @code{nil} otherwise.
  272. @end defun
  273. @defun numberp object
  274. This predicate tests whether its argument is a number (either integer or
  275. floating point), and returns @code{t} if so, @code{nil} otherwise.
  276. @end defun
  277. @defun natnump object
  278. @cindex natural numbers
  279. This predicate (whose name comes from the phrase ``natural number'')
  280. tests to see whether its argument is a nonnegative integer, and
  281. returns @code{t} if so, @code{nil} otherwise. 0 is considered
  282. non-negative.
  283. @findex wholenump
  284. @code{wholenump} is a synonym for @code{natnump}.
  285. @end defun
  286. @defun zerop number
  287. This predicate tests whether its argument is zero, and returns @code{t}
  288. if so, @code{nil} otherwise. The argument must be a number.
  289. @code{(zerop x)} is equivalent to @code{(= x 0)}.
  290. @end defun
  291. @node Comparison of Numbers
  292. @section Comparison of Numbers
  293. @cindex number comparison
  294. @cindex comparing numbers
  295. To test numbers for numerical equality, you should normally use
  296. @code{=}, not @code{eq}. There can be many distinct floating-point
  297. objects with the same numeric value. If you use @code{eq} to
  298. compare them, then you test whether two values are the same
  299. @emph{object}. By contrast, @code{=} compares only the numeric values
  300. of the objects.
  301. In Emacs Lisp, each integer is a unique Lisp object.
  302. Therefore, @code{eq} is equivalent to @code{=} where integers are
  303. concerned. It is sometimes convenient to use @code{eq} for comparing
  304. an unknown value with an integer, because @code{eq} does not report an
  305. error if the unknown value is not a number---it accepts arguments of
  306. any type. By contrast, @code{=} signals an error if the arguments are
  307. not numbers or markers. However, it is better programming practice to
  308. use @code{=} if you can, even for comparing integers.
  309. Sometimes it is useful to compare numbers with @code{equal}, which
  310. treats two numbers as equal if they have the same data type (both
  311. integers, or both floating point) and the same value. By contrast,
  312. @code{=} can treat an integer and a floating-point number as equal.
  313. @xref{Equality Predicates}.
  314. There is another wrinkle: because floating-point arithmetic is not
  315. exact, it is often a bad idea to check for equality of floating-point
  316. values. Usually it is better to test for approximate equality.
  317. Here's a function to do this:
  318. @example
  319. (defvar fuzz-factor 1.0e-6)
  320. (defun approx-equal (x y)
  321. (or (= x y)
  322. (< (/ (abs (- x y))
  323. (max (abs x) (abs y)))
  324. fuzz-factor)))
  325. @end example
  326. @cindex CL note---integers vrs @code{eq}
  327. @quotation
  328. @b{Common Lisp note:} Comparing numbers in Common Lisp always requires
  329. @code{=} because Common Lisp implements multi-word integers, and two
  330. distinct integer objects can have the same numeric value. Emacs Lisp
  331. can have just one integer object for any given value because it has a
  332. limited range of integers.
  333. @end quotation
  334. @defun = number-or-marker &rest number-or-markers
  335. This function tests whether all its arguments are numerically equal,
  336. and returns @code{t} if so, @code{nil} otherwise.
  337. @end defun
  338. @defun eql value1 value2
  339. This function acts like @code{eq} except when both arguments are
  340. numbers. It compares numbers by type and numeric value, so that
  341. @code{(eql 1.0 1)} returns @code{nil}, but @code{(eql 1.0 1.0)} and
  342. @code{(eql 1 1)} both return @code{t}.
  343. @end defun
  344. @defun /= number-or-marker1 number-or-marker2
  345. This function tests whether its arguments are numerically equal, and
  346. returns @code{t} if they are not, and @code{nil} if they are.
  347. @end defun
  348. @defun < number-or-marker &rest number-or-markers
  349. This function tests whether each argument is strictly less than the
  350. following argument. It returns @code{t} if so, @code{nil} otherwise.
  351. @end defun
  352. @defun <= number-or-marker &rest number-or-markers
  353. This function tests whether each argument is less than or equal to
  354. the following argument. It returns @code{t} if so, @code{nil} otherwise.
  355. @end defun
  356. @defun > number-or-marker &rest number-or-markers
  357. This function tests whether each argument is strictly greater than
  358. the following argument. It returns @code{t} if so, @code{nil} otherwise.
  359. @end defun
  360. @defun >= number-or-marker &rest number-or-markers
  361. This function tests whether each argument is greater than or equal to
  362. the following argument. It returns @code{t} if so, @code{nil} otherwise.
  363. @end defun
  364. @defun max number-or-marker &rest numbers-or-markers
  365. This function returns the largest of its arguments.
  366. If any of the arguments is floating point, the value is returned
  367. as floating point, even if it was given as an integer.
  368. @example
  369. (max 20)
  370. @result{} 20
  371. (max 1 2.5)
  372. @result{} 2.5
  373. (max 1 3 2.5)
  374. @result{} 3.0
  375. @end example
  376. @end defun
  377. @defun min number-or-marker &rest numbers-or-markers
  378. This function returns the smallest of its arguments.
  379. If any of the arguments is floating point, the value is returned
  380. as floating point, even if it was given as an integer.
  381. @example
  382. (min -4 1)
  383. @result{} -4
  384. @end example
  385. @end defun
  386. @defun abs number
  387. This function returns the absolute value of @var{number}.
  388. @end defun
  389. @node Numeric Conversions
  390. @section Numeric Conversions
  391. @cindex rounding in conversions
  392. @cindex number conversions
  393. @cindex converting numbers
  394. To convert an integer to floating point, use the function @code{float}.
  395. @defun float number
  396. This returns @var{number} converted to floating point.
  397. If @var{number} is already floating point, @code{float} returns
  398. it unchanged.
  399. @end defun
  400. There are four functions to convert floating-point numbers to
  401. integers; they differ in how they round. All accept an argument
  402. @var{number} and an optional argument @var{divisor}. Both arguments
  403. may be integers or floating-point numbers. @var{divisor} may also be
  404. @code{nil}. If @var{divisor} is @code{nil} or omitted, these
  405. functions convert @var{number} to an integer, or return it unchanged
  406. if it already is an integer. If @var{divisor} is non-@code{nil}, they
  407. divide @var{number} by @var{divisor} and convert the result to an
  408. integer. If @var{divisor} is zero (whether integer or
  409. floating point), Emacs signals an @code{arith-error} error.
  410. @defun truncate number &optional divisor
  411. This returns @var{number}, converted to an integer by rounding towards
  412. zero.
  413. @example
  414. (truncate 1.2)
  415. @result{} 1
  416. (truncate 1.7)
  417. @result{} 1
  418. (truncate -1.2)
  419. @result{} -1
  420. (truncate -1.7)
  421. @result{} -1
  422. @end example
  423. @end defun
  424. @defun floor number &optional divisor
  425. This returns @var{number}, converted to an integer by rounding downward
  426. (towards negative infinity).
  427. If @var{divisor} is specified, this uses the kind of division
  428. operation that corresponds to @code{mod}, rounding downward.
  429. @example
  430. (floor 1.2)
  431. @result{} 1
  432. (floor 1.7)
  433. @result{} 1
  434. (floor -1.2)
  435. @result{} -2
  436. (floor -1.7)
  437. @result{} -2
  438. (floor 5.99 3)
  439. @result{} 1
  440. @end example
  441. @end defun
  442. @defun ceiling number &optional divisor
  443. This returns @var{number}, converted to an integer by rounding upward
  444. (towards positive infinity).
  445. @example
  446. (ceiling 1.2)
  447. @result{} 2
  448. (ceiling 1.7)
  449. @result{} 2
  450. (ceiling -1.2)
  451. @result{} -1
  452. (ceiling -1.7)
  453. @result{} -1
  454. @end example
  455. @end defun
  456. @defun round number &optional divisor
  457. This returns @var{number}, converted to an integer by rounding towards the
  458. nearest integer. Rounding a value equidistant between two integers
  459. returns the even integer.
  460. @example
  461. (round 1.2)
  462. @result{} 1
  463. (round 1.7)
  464. @result{} 2
  465. (round -1.2)
  466. @result{} -1
  467. (round -1.7)
  468. @result{} -2
  469. @end example
  470. @end defun
  471. @node Arithmetic Operations
  472. @section Arithmetic Operations
  473. @cindex arithmetic operations
  474. Emacs Lisp provides the traditional four arithmetic operations
  475. (addition, subtraction, multiplication, and division), as well as
  476. remainder and modulus functions, and functions to add or subtract 1.
  477. Except for @code{%}, each of these functions accepts both integer and
  478. floating-point arguments, and returns a floating-point number if any
  479. argument is floating point.
  480. Emacs Lisp arithmetic functions do not check for integer overflow.
  481. Thus @code{(1+ 536870911)} may evaluate to
  482. @minus{}536870912, depending on your hardware.
  483. @defun 1+ number-or-marker
  484. This function returns @var{number-or-marker} plus 1.
  485. For example,
  486. @example
  487. (setq foo 4)
  488. @result{} 4
  489. (1+ foo)
  490. @result{} 5
  491. @end example
  492. This function is not analogous to the C operator @code{++}---it does not
  493. increment a variable. It just computes a sum. Thus, if we continue,
  494. @example
  495. foo
  496. @result{} 4
  497. @end example
  498. If you want to increment the variable, you must use @code{setq},
  499. like this:
  500. @example
  501. (setq foo (1+ foo))
  502. @result{} 5
  503. @end example
  504. @end defun
  505. @defun 1- number-or-marker
  506. This function returns @var{number-or-marker} minus 1.
  507. @end defun
  508. @defun + &rest numbers-or-markers
  509. This function adds its arguments together. When given no arguments,
  510. @code{+} returns 0.
  511. @example
  512. (+)
  513. @result{} 0
  514. (+ 1)
  515. @result{} 1
  516. (+ 1 2 3 4)
  517. @result{} 10
  518. @end example
  519. @end defun
  520. @defun - &optional number-or-marker &rest more-numbers-or-markers
  521. The @code{-} function serves two purposes: negation and subtraction.
  522. When @code{-} has a single argument, the value is the negative of the
  523. argument. When there are multiple arguments, @code{-} subtracts each of
  524. the @var{more-numbers-or-markers} from @var{number-or-marker},
  525. cumulatively. If there are no arguments, the result is 0.
  526. @example
  527. (- 10 1 2 3 4)
  528. @result{} 0
  529. (- 10)
  530. @result{} -10
  531. (-)
  532. @result{} 0
  533. @end example
  534. @end defun
  535. @defun * &rest numbers-or-markers
  536. This function multiplies its arguments together, and returns the
  537. product. When given no arguments, @code{*} returns 1.
  538. @example
  539. (*)
  540. @result{} 1
  541. (* 1)
  542. @result{} 1
  543. (* 1 2 3 4)
  544. @result{} 24
  545. @end example
  546. @end defun
  547. @defun / dividend divisor &rest divisors
  548. This function divides @var{dividend} by @var{divisor} and returns the
  549. quotient. If there are additional arguments @var{divisors}, then it
  550. divides @var{dividend} by each divisor in turn. Each argument may be a
  551. number or a marker.
  552. If all the arguments are integers, the result is an integer, obtained
  553. by rounding the quotient towards zero after each division.
  554. @example
  555. @group
  556. (/ 6 2)
  557. @result{} 3
  558. @end group
  559. @group
  560. (/ 5 2)
  561. @result{} 2
  562. @end group
  563. @group
  564. (/ 5.0 2)
  565. @result{} 2.5
  566. @end group
  567. @group
  568. (/ 5 2.0)
  569. @result{} 2.5
  570. @end group
  571. @group
  572. (/ 5.0 2.0)
  573. @result{} 2.5
  574. @end group
  575. @group
  576. (/ 25 3 2)
  577. @result{} 4
  578. @end group
  579. @group
  580. (/ -17 6)
  581. @result{} -2
  582. @end group
  583. @end example
  584. @cindex @code{arith-error} in division
  585. If you divide an integer by the integer 0, Emacs signals an
  586. @code{arith-error} error (@pxref{Errors}). Floating-point division of
  587. a nonzero number by zero yields either positive or negative infinity
  588. (@pxref{Float Basics}).
  589. @end defun
  590. @defun % dividend divisor
  591. @cindex remainder
  592. This function returns the integer remainder after division of @var{dividend}
  593. by @var{divisor}. The arguments must be integers or markers.
  594. For any two integers @var{dividend} and @var{divisor},
  595. @example
  596. @group
  597. (+ (% @var{dividend} @var{divisor})
  598. (* (/ @var{dividend} @var{divisor}) @var{divisor}))
  599. @end group
  600. @end example
  601. @noindent
  602. always equals @var{dividend} if @var{divisor} is nonzero.
  603. @example
  604. (% 9 4)
  605. @result{} 1
  606. (% -9 4)
  607. @result{} -1
  608. (% 9 -4)
  609. @result{} 1
  610. (% -9 -4)
  611. @result{} -1
  612. @end example
  613. @end defun
  614. @defun mod dividend divisor
  615. @cindex modulus
  616. This function returns the value of @var{dividend} modulo @var{divisor};
  617. in other words, the remainder after division of @var{dividend}
  618. by @var{divisor}, but with the same sign as @var{divisor}.
  619. The arguments must be numbers or markers.
  620. Unlike @code{%}, @code{mod} permits floating-point arguments; it
  621. rounds the quotient downward (towards minus infinity) to an integer,
  622. and uses that quotient to compute the remainder.
  623. If @var{divisor} is zero, @code{mod} signals an @code{arith-error}
  624. error if both arguments are integers, and returns a NaN otherwise.
  625. @example
  626. @group
  627. (mod 9 4)
  628. @result{} 1
  629. @end group
  630. @group
  631. (mod -9 4)
  632. @result{} 3
  633. @end group
  634. @group
  635. (mod 9 -4)
  636. @result{} -3
  637. @end group
  638. @group
  639. (mod -9 -4)
  640. @result{} -1
  641. @end group
  642. @group
  643. (mod 5.5 2.5)
  644. @result{} .5
  645. @end group
  646. @end example
  647. For any two numbers @var{dividend} and @var{divisor},
  648. @example
  649. @group
  650. (+ (mod @var{dividend} @var{divisor})
  651. (* (floor @var{dividend} @var{divisor}) @var{divisor}))
  652. @end group
  653. @end example
  654. @noindent
  655. always equals @var{dividend}, subject to rounding error if either
  656. argument is floating point and to an @code{arith-error} if @var{dividend} is an
  657. integer and @var{divisor} is 0. For @code{floor}, see @ref{Numeric
  658. Conversions}.
  659. @end defun
  660. @node Rounding Operations
  661. @section Rounding Operations
  662. @cindex rounding without conversion
  663. The functions @code{ffloor}, @code{fceiling}, @code{fround}, and
  664. @code{ftruncate} take a floating-point argument and return a floating-point
  665. result whose value is a nearby integer. @code{ffloor} returns the
  666. nearest integer below; @code{fceiling}, the nearest integer above;
  667. @code{ftruncate}, the nearest integer in the direction towards zero;
  668. @code{fround}, the nearest integer.
  669. @defun ffloor float
  670. This function rounds @var{float} to the next lower integral value, and
  671. returns that value as a floating-point number.
  672. @end defun
  673. @defun fceiling float
  674. This function rounds @var{float} to the next higher integral value, and
  675. returns that value as a floating-point number.
  676. @end defun
  677. @defun ftruncate float
  678. This function rounds @var{float} towards zero to an integral value, and
  679. returns that value as a floating-point number.
  680. @end defun
  681. @defun fround float
  682. This function rounds @var{float} to the nearest integral value,
  683. and returns that value as a floating-point number.
  684. Rounding a value equidistant between two integers returns the even integer.
  685. @end defun
  686. @node Bitwise Operations
  687. @section Bitwise Operations on Integers
  688. @cindex bitwise arithmetic
  689. @cindex logical arithmetic
  690. In a computer, an integer is represented as a binary number, a
  691. sequence of @dfn{bits} (digits which are either zero or one). A bitwise
  692. operation acts on the individual bits of such a sequence. For example,
  693. @dfn{shifting} moves the whole sequence left or right one or more places,
  694. reproducing the same pattern ``moved over''.
  695. The bitwise operations in Emacs Lisp apply only to integers.
  696. @defun lsh integer1 count
  697. @cindex logical shift
  698. @code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the
  699. bits in @var{integer1} to the left @var{count} places, or to the right
  700. if @var{count} is negative, bringing zeros into the vacated bits. If
  701. @var{count} is negative, @code{lsh} shifts zeros into the leftmost
  702. (most-significant) bit, producing a positive result even if
  703. @var{integer1} is negative. Contrast this with @code{ash}, below.
  704. Here are two examples of @code{lsh}, shifting a pattern of bits one
  705. place to the left. We show only the low-order eight bits of the binary
  706. pattern; the rest are all zero.
  707. @example
  708. @group
  709. (lsh 5 1)
  710. @result{} 10
  711. ;; @r{Decimal 5 becomes decimal 10.}
  712. 00000101 @result{} 00001010
  713. (lsh 7 1)
  714. @result{} 14
  715. ;; @r{Decimal 7 becomes decimal 14.}
  716. 00000111 @result{} 00001110
  717. @end group
  718. @end example
  719. @noindent
  720. As the examples illustrate, shifting the pattern of bits one place to
  721. the left produces a number that is twice the value of the previous
  722. number.
  723. Shifting a pattern of bits two places to the left produces results
  724. like this (with 8-bit binary numbers):
  725. @example
  726. @group
  727. (lsh 3 2)
  728. @result{} 12
  729. ;; @r{Decimal 3 becomes decimal 12.}
  730. 00000011 @result{} 00001100
  731. @end group
  732. @end example
  733. On the other hand, shifting one place to the right looks like this:
  734. @example
  735. @group
  736. (lsh 6 -1)
  737. @result{} 3
  738. ;; @r{Decimal 6 becomes decimal 3.}
  739. 00000110 @result{} 00000011
  740. @end group
  741. @group
  742. (lsh 5 -1)
  743. @result{} 2
  744. ;; @r{Decimal 5 becomes decimal 2.}
  745. 00000101 @result{} 00000010
  746. @end group
  747. @end example
  748. @noindent
  749. As the example illustrates, shifting one place to the right divides the
  750. value of a positive integer by two, rounding downward.
  751. The function @code{lsh}, like all Emacs Lisp arithmetic functions, does
  752. not check for overflow, so shifting left can discard significant bits
  753. and change the sign of the number. For example, left shifting
  754. 536,870,911 produces @minus{}2 in the 30-bit implementation:
  755. @example
  756. (lsh 536870911 1) ; @r{left shift}
  757. @result{} -2
  758. @end example
  759. In binary, the argument looks like this:
  760. @example
  761. @group
  762. ;; @r{Decimal 536,870,911}
  763. 0111...111111 (30 bits total)
  764. @end group
  765. @end example
  766. @noindent
  767. which becomes the following when left shifted:
  768. @example
  769. @group
  770. ;; @r{Decimal @minus{}2}
  771. 1111...111110 (30 bits total)
  772. @end group
  773. @end example
  774. @end defun
  775. @defun ash integer1 count
  776. @cindex arithmetic shift
  777. @code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1}
  778. to the left @var{count} places, or to the right if @var{count}
  779. is negative.
  780. @code{ash} gives the same results as @code{lsh} except when
  781. @var{integer1} and @var{count} are both negative. In that case,
  782. @code{ash} puts ones in the empty bit positions on the left, while
  783. @code{lsh} puts zeros in those bit positions.
  784. Thus, with @code{ash}, shifting the pattern of bits one place to the right
  785. looks like this:
  786. @example
  787. @group
  788. (ash -6 -1) @result{} -3
  789. ;; @r{Decimal @minus{}6 becomes decimal @minus{}3.}
  790. 1111...111010 (30 bits total)
  791. @result{}
  792. 1111...111101 (30 bits total)
  793. @end group
  794. @end example
  795. In contrast, shifting the pattern of bits one place to the right with
  796. @code{lsh} looks like this:
  797. @example
  798. @group
  799. (lsh -6 -1) @result{} 536870909
  800. ;; @r{Decimal @minus{}6 becomes decimal 536,870,909.}
  801. 1111...111010 (30 bits total)
  802. @result{}
  803. 0111...111101 (30 bits total)
  804. @end group
  805. @end example
  806. Here are other examples:
  807. @c !!! Check if lined up in smallbook format! XDVI shows problem
  808. @c with smallbook but not with regular book! --rjc 16mar92
  809. @smallexample
  810. @group
  811. ; @r{ 30-bit binary values}
  812. (lsh 5 2) ; 5 = @r{0000...000101}
  813. @result{} 20 ; = @r{0000...010100}
  814. @end group
  815. @group
  816. (ash 5 2)
  817. @result{} 20
  818. (lsh -5 2) ; -5 = @r{1111...111011}
  819. @result{} -20 ; = @r{1111...101100}
  820. (ash -5 2)
  821. @result{} -20
  822. @end group
  823. @group
  824. (lsh 5 -2) ; 5 = @r{0000...000101}
  825. @result{} 1 ; = @r{0000...000001}
  826. @end group
  827. @group
  828. (ash 5 -2)
  829. @result{} 1
  830. @end group
  831. @group
  832. (lsh -5 -2) ; -5 = @r{1111...111011}
  833. @result{} 268435454
  834. ; = @r{0011...111110}
  835. @end group
  836. @group
  837. (ash -5 -2) ; -5 = @r{1111...111011}
  838. @result{} -2 ; = @r{1111...111110}
  839. @end group
  840. @end smallexample
  841. @end defun
  842. @defun logand &rest ints-or-markers
  843. This function returns the ``logical and'' of the arguments: the
  844. @var{n}th bit is set in the result if, and only if, the @var{n}th bit is
  845. set in all the arguments. (``Set'' means that the value of the bit is 1
  846. rather than 0.)
  847. For example, using 4-bit binary numbers, the ``logical and'' of 13 and
  848. 12 is 12: 1101 combined with 1100 produces 1100.
  849. In both the binary numbers, the leftmost two bits are set (i.e., they
  850. are 1's), so the leftmost two bits of the returned value are set.
  851. However, for the rightmost two bits, each is zero in at least one of
  852. the arguments, so the rightmost two bits of the returned value are 0's.
  853. @noindent
  854. Therefore,
  855. @example
  856. @group
  857. (logand 13 12)
  858. @result{} 12
  859. @end group
  860. @end example
  861. If @code{logand} is not passed any argument, it returns a value of
  862. @minus{}1. This number is an identity element for @code{logand}
  863. because its binary representation consists entirely of ones. If
  864. @code{logand} is passed just one argument, it returns that argument.
  865. @smallexample
  866. @group
  867. ; @r{ 30-bit binary values}
  868. (logand 14 13) ; 14 = @r{0000...001110}
  869. ; 13 = @r{0000...001101}
  870. @result{} 12 ; 12 = @r{0000...001100}
  871. @end group
  872. @group
  873. (logand 14 13 4) ; 14 = @r{0000...001110}
  874. ; 13 = @r{0000...001101}
  875. ; 4 = @r{0000...000100}
  876. @result{} 4 ; 4 = @r{0000...000100}
  877. @end group
  878. @group
  879. (logand)
  880. @result{} -1 ; -1 = @r{1111...111111}
  881. @end group
  882. @end smallexample
  883. @end defun
  884. @defun logior &rest ints-or-markers
  885. This function returns the ``inclusive or'' of its arguments: the @var{n}th bit
  886. is set in the result if, and only if, the @var{n}th bit is set in at least
  887. one of the arguments. If there are no arguments, the result is zero,
  888. which is an identity element for this operation. If @code{logior} is
  889. passed just one argument, it returns that argument.
  890. @smallexample
  891. @group
  892. ; @r{ 30-bit binary values}
  893. (logior 12 5) ; 12 = @r{0000...001100}
  894. ; 5 = @r{0000...000101}
  895. @result{} 13 ; 13 = @r{0000...001101}
  896. @end group
  897. @group
  898. (logior 12 5 7) ; 12 = @r{0000...001100}
  899. ; 5 = @r{0000...000101}
  900. ; 7 = @r{0000...000111}
  901. @result{} 15 ; 15 = @r{0000...001111}
  902. @end group
  903. @end smallexample
  904. @end defun
  905. @defun logxor &rest ints-or-markers
  906. This function returns the ``exclusive or'' of its arguments: the
  907. @var{n}th bit is set in the result if, and only if, the @var{n}th bit is
  908. set in an odd number of the arguments. If there are no arguments, the
  909. result is 0, which is an identity element for this operation. If
  910. @code{logxor} is passed just one argument, it returns that argument.
  911. @smallexample
  912. @group
  913. ; @r{ 30-bit binary values}
  914. (logxor 12 5) ; 12 = @r{0000...001100}
  915. ; 5 = @r{0000...000101}
  916. @result{} 9 ; 9 = @r{0000...001001}
  917. @end group
  918. @group
  919. (logxor 12 5 7) ; 12 = @r{0000...001100}
  920. ; 5 = @r{0000...000101}
  921. ; 7 = @r{0000...000111}
  922. @result{} 14 ; 14 = @r{0000...001110}
  923. @end group
  924. @end smallexample
  925. @end defun
  926. @defun lognot integer
  927. This function returns the logical complement of its argument: the @var{n}th
  928. bit is one in the result if, and only if, the @var{n}th bit is zero in
  929. @var{integer}, and vice-versa.
  930. @example
  931. (lognot 5)
  932. @result{} -6
  933. ;; 5 = @r{0000...000101} (30 bits total)
  934. ;; @r{becomes}
  935. ;; -6 = @r{1111...111010} (30 bits total)
  936. @end example
  937. @end defun
  938. @node Math Functions
  939. @section Standard Mathematical Functions
  940. @cindex transcendental functions
  941. @cindex mathematical functions
  942. @cindex floating-point functions
  943. These mathematical functions allow integers as well as floating-point
  944. numbers as arguments.
  945. @defun sin arg
  946. @defunx cos arg
  947. @defunx tan arg
  948. These are the basic trigonometric functions, with argument @var{arg}
  949. measured in radians.
  950. @end defun
  951. @defun asin arg
  952. The value of @code{(asin @var{arg})} is a number between
  953. @ifnottex
  954. @minus{}pi/2
  955. @end ifnottex
  956. @tex
  957. @math{-\pi/2}
  958. @end tex
  959. and
  960. @ifnottex
  961. pi/2
  962. @end ifnottex
  963. @tex
  964. @math{\pi/2}
  965. @end tex
  966. (inclusive) whose sine is @var{arg}. If @var{arg} is out of range
  967. (outside [@minus{}1, 1]), @code{asin} returns a NaN.
  968. @end defun
  969. @defun acos arg
  970. The value of @code{(acos @var{arg})} is a number between 0 and
  971. @ifnottex
  972. pi
  973. @end ifnottex
  974. @tex
  975. @math{\pi}
  976. @end tex
  977. (inclusive) whose cosine is @var{arg}. If @var{arg} is out of range
  978. (outside [@minus{}1, 1]), @code{acos} returns a NaN.
  979. @end defun
  980. @defun atan y &optional x
  981. The value of @code{(atan @var{y})} is a number between
  982. @ifnottex
  983. @minus{}pi/2
  984. @end ifnottex
  985. @tex
  986. @math{-\pi/2}
  987. @end tex
  988. and
  989. @ifnottex
  990. pi/2
  991. @end ifnottex
  992. @tex
  993. @math{\pi/2}
  994. @end tex
  995. (exclusive) whose tangent is @var{y}. If the optional second
  996. argument @var{x} is given, the value of @code{(atan y x)} is the
  997. angle in radians between the vector @code{[@var{x}, @var{y}]} and the
  998. @code{X} axis.
  999. @end defun
  1000. @defun exp arg
  1001. This is the exponential function; it returns @math{e} to the power
  1002. @var{arg}.
  1003. @end defun
  1004. @defun log arg &optional base
  1005. This function returns the logarithm of @var{arg}, with base
  1006. @var{base}. If you don't specify @var{base}, the natural base
  1007. @math{e} is used. If @var{arg} or @var{base} is negative, @code{log}
  1008. returns a NaN.
  1009. @end defun
  1010. @defun expt x y
  1011. This function returns @var{x} raised to power @var{y}. If both
  1012. arguments are integers and @var{y} is positive, the result is an
  1013. integer; in this case, overflow causes truncation, so watch out.
  1014. If @var{x} is a finite negative number and @var{y} is a finite
  1015. non-integer, @code{expt} returns a NaN.
  1016. @end defun
  1017. @defun sqrt arg
  1018. This returns the square root of @var{arg}. If @var{arg} is finite
  1019. and less than zero, @code{sqrt} returns a NaN.
  1020. @end defun
  1021. In addition, Emacs defines the following common mathematical
  1022. constants:
  1023. @defvar float-e
  1024. The mathematical constant @math{e} (2.71828@dots{}).
  1025. @end defvar
  1026. @defvar float-pi
  1027. The mathematical constant @math{pi} (3.14159@dots{}).
  1028. @end defvar
  1029. @node Random Numbers
  1030. @section Random Numbers
  1031. @cindex random numbers
  1032. A deterministic computer program cannot generate true random
  1033. numbers. For most purposes, @dfn{pseudo-random numbers} suffice. A
  1034. series of pseudo-random numbers is generated in a deterministic
  1035. fashion. The numbers are not truly random, but they have certain
  1036. properties that mimic a random series. For example, all possible
  1037. values occur equally often in a pseudo-random series.
  1038. Pseudo-random numbers are generated from a ``seed''. Starting from
  1039. any given seed, the @code{random} function always generates the same
  1040. sequence of numbers. By default, Emacs initializes the random seed at
  1041. startup, in such a way that the sequence of values of @code{random}
  1042. (with overwhelming likelihood) differs in each Emacs run.
  1043. Sometimes you want the random number sequence to be repeatable. For
  1044. example, when debugging a program whose behavior depends on the random
  1045. number sequence, it is helpful to get the same behavior in each
  1046. program run. To make the sequence repeat, execute @code{(random "")}.
  1047. This sets the seed to a constant value for your particular Emacs
  1048. executable (though it may differ for other Emacs builds). You can use
  1049. other strings to choose various seed values.
  1050. @defun random &optional limit
  1051. This function returns a pseudo-random integer. Repeated calls return a
  1052. series of pseudo-random integers.
  1053. If @var{limit} is a positive integer, the value is chosen to be
  1054. nonnegative and less than @var{limit}. Otherwise, the value might be
  1055. any integer representable in Lisp, i.e., an integer between
  1056. @code{most-negative-fixnum} and @code{most-positive-fixnum}
  1057. (@pxref{Integer Basics}).
  1058. If @var{limit} is @code{t}, it means to choose a new seed as if Emacs
  1059. were restarting.
  1060. If @var{limit} is a string, it means to choose a new seed based on the
  1061. string's contents.
  1062. @end defun