annotate man/lispref/numbers.texi @ 964:8d610ea37af8

[xemacs-hg @ 2002-08-16 08:15:18 by michaels] 2002-08-13 David Bush <David.Bush@intel.com> * device-msw.c: Correct initialization list for devmode_description Add dumpable flag to msprinter-settings for USE_KKCC * dialog-msw.c: Add dumpable flag to mswindows-dialog-id for USE_KKCC * event-msw.c (mswindows_enqueue_misc_user_event): With USE_KKCC use mutator macros to set event parameters * event-msw.c (mswindows_enqueue_magic_event): With USE_KKCC use mutator macros to set event parameters * event-msw.c (mswindows_enqueue_process_event): With USE_KKCC use mutator macros to set event parameters * event-msw.c (mswindows_enqueue_mouse_button_event): Reorder event setup so event type is set first to support USE_KKCC. For USE_KKCC use mutator macros to set event parameters * event-msw.c (mswindows_enqueue_keypress_event): With USE_KKCC use mutator macros to set event parameters * event-msw.c (mswindows_dequeue_dispatch_event): With USE_KKCC use accessor to get and mutator macros to set event parameters * event-msw.c (mswindows_cancel_dispatch_event): With USE_KKCC use accessor macros to get event parameters * event-msw.c (mswindows_wm_timer_callback): With USE_KKCC use mutator macros to set event parameters * event-msw.c (mswindows_dde_callback): With USE_KKCC use mutator macros to set event parameters * event-msw.c (mswindows_wnd_proc): With USE_KKCC use mutator macros to set event parameters * event-msw.c (emacs_mswindows_remove_timeout): With USE_KKCC use mutator macros to set event parameters * event-msw.c (emacs_mswindows_format_magic_event): With USE_KKCC use accessor macro to access magic event type * event-msw.c (emacs_mswindows_compare_magic_event): With USE_KKCC use accessor macro to access magic event type * event-msw.c (emacs_mswindows_hash_magic_event): With USE_KKCC use accessor macro to access magic event type * event-msw.c (emacs_mswindows_handle_magic_event): With USE_KKCC use accessor macro to access magic event type and event data * event-msw.c (emacs_mswindows_quit_p): With USE_KKCC use event accessor and mutator macros to get/set event parameters * gui-msw.c (mswindows_handle_gui_wm_command): For USE_KKCC use mutator macros to set event parameters. Make the event data type match the event type. * menubar-msw.c (mswindows_popup_menu): With USE_KKCC use accessor macros to extract button position
author michaels
date Fri, 16 Aug 2002 08:15:42 +0000
parents 576fb035e263
children f43f9ca6c7d9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 @c -*-texinfo-*-
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 @c This is part of the XEmacs Lisp Reference Manual.
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 @c See the file lispref.texi for copying conditions.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 @setfilename ../../info/numbers.info
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 @node Numbers, Strings and Characters, Lisp Data Types, Top
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 @chapter Numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 @cindex integers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 @cindex numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 XEmacs supports two numeric data types: @dfn{integers} and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 @dfn{floating point numbers}. Integers are whole numbers such as
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 @minus{}3, 0, #b0111, #xFEED, #o744. Their values are exact. The
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 number prefixes `#b', `#o', and `#x' are supported to represent numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 in binary, octal, and hexadecimal notation (or radix). Floating point
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 numbers are numbers with fractional parts, such as @minus{}4.5, 0.0, or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 2.71828. They can also be expressed in exponential notation: 1.5e2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 equals 150; in this example, @samp{e2} stands for ten to the second
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 power, and is multiplied by 1.5. Floating point values are not exact;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 they have a fixed, limited amount of precision.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 @menu
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 * Integer Basics:: Representation and range of integers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 * Float Basics:: Representation and range of floating point.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 * Predicates on Numbers:: Testing for numbers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 * Comparison of Numbers:: Equality and inequality predicates.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 * Numeric Conversions:: Converting float to integer and vice versa.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 * Arithmetic Operations:: How to add, subtract, multiply and divide.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 * Rounding Operations:: Explicitly rounding floating point numbers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 * Bitwise Operations:: Logical and, or, not, shifting.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 * Math Functions:: Trig, exponential and logarithmic functions.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 * Random Numbers:: Obtaining random integers, predictable or not.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 @end menu
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 @node Integer Basics
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 @section Integer Basics
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 The range of values for an integer depends on the machine. The
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 minimum range is @minus{}134217728 to 134217727 (28 bits; i.e.,
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
40 @ifinfo
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 -2**27
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 @end ifinfo
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
43 @tex
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 $-2^{27}$
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 @end tex
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
46 to
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
47 @ifinfo
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 2**27 - 1),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 @end ifinfo
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
50 @tex
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 $2^{27}-1$),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 @end tex
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 but some machines may provide a wider range. Many examples in this
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 chapter assume an integer has 28 bits.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 @cindex overflow
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 The Lisp reader reads an integer as a sequence of digits with optional
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 initial sign and optional final period.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 1 ; @r{The integer 1.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 1. ; @r{The integer 1.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 +1 ; @r{Also the integer 1.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 -1 ; @r{The integer @minus{}1.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 268435457 ; @r{Also the integer 1, due to overflow.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 0 ; @r{The integer 0.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 -0 ; @r{The integer 0.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 To understand how various functions work on integers, especially the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 bitwise operators (@pxref{Bitwise Operations}), it is often helpful to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 view the numbers in their binary form.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 In 28-bit binary, the decimal integer 5 looks like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 0000 0000 0000 0000 0000 0000 0101
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 @noindent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 (We have inserted spaces between groups of 4 bits, and two spaces
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 between groups of 8 bits, to make the binary integer easier to read.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 The integer @minus{}1 looks like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 1111 1111 1111 1111 1111 1111 1111
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 @noindent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 @cindex two's complement
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 @minus{}1 is represented as 28 ones. (This is called @dfn{two's
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 complement} notation.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 The negative integer, @minus{}5, is creating by subtracting 4 from
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 @minus{}1. In binary, the decimal integer 4 is 100. Consequently,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 @minus{}5 looks like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 1111 1111 1111 1111 1111 1111 1011
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 In this implementation, the largest 28-bit binary integer is the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 decimal integer 134,217,727. In binary, it looks like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 0111 1111 1111 1111 1111 1111 1111
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 Since the arithmetic functions do not check whether integers go
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 outside their range, when you add 1 to 134,217,727, the value is the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 negative integer @minus{}134,217,728:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 (+ 1 134217727)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 @result{} -134217728
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 @result{} 1000 0000 0000 0000 0000 0000 0000
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 Many of the following functions accept markers for arguments as well
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 as integers. (@xref{Markers}.) More precisely, the actual arguments to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 such functions may be either integers or markers, which is why we often
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 give these arguments the name @var{int-or-marker}. When the argument
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 value is a marker, its position value is used and its buffer is ignored.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 @ignore
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 In version 19, except where @emph{integer} is specified as an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 argument, all of the functions for markers and integers also work for
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 floating point numbers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 @end ignore
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 @node Float Basics
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 @section Floating Point Basics
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 XEmacs supports floating point numbers. The precise range of floating
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 point numbers is machine-specific; it is the same as the range of the C
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 data type @code{double} on the machine in question.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 The printed representation for floating point numbers requires either
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 a decimal point (with at least one digit following), an exponent, or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 both. For example, @samp{1500.0}, @samp{15e2}, @samp{15.0e2},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 @samp{1.5e3}, and @samp{.15e4} are five ways of writing a floating point
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 number whose value is 1500. They are all equivalent. You can also use
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 a minus sign to write negative floating point numbers, as in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 @samp{-1.0}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 @cindex IEEE floating point
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 @cindex positive infinity
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 @cindex negative infinity
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 @cindex infinity
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 @cindex NaN
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 Most modern computers support the IEEE floating point standard, which
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 provides for positive infinity and negative infinity as floating point
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 values. It also provides for a class of values called NaN or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 ``not-a-number''; numerical functions return such values in cases where
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 there is no correct answer. For example, @code{(sqrt -1.0)} returns a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 NaN. For practical purposes, there's no significant difference between
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 different NaN values in XEmacs Lisp, and there's no rule for precisely
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 which NaN value should be used in a particular case, so this manual
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 doesn't try to distinguish them. XEmacs Lisp has no read syntax for NaNs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 or infinities; perhaps we should create a syntax in the future.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 You can use @code{logb} to extract the binary exponent of a floating
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 point number (or estimate the logarithm of an integer):
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 @defun logb number
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 This function returns the binary exponent of @var{number}. More
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 precisely, the value is the logarithm of @var{number} base 2, rounded
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 down to an integer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 @node Predicates on Numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 @section Type Predicates for Numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 The functions in this section test whether the argument is a number or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 whether it is a certain sort of number. The functions @code{integerp}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 and @code{floatp} can take any type of Lisp object as argument (the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 predicates would not be of much use otherwise); but the @code{zerop}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 predicate requires a number as its argument. See also
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 @code{integer-or-marker-p}, @code{integer-char-or-marker-p},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 @code{number-or-marker-p} and @code{number-char-or-marker-p}, in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 @ref{Predicates on Markers}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 @defun floatp object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 This predicate tests whether its argument is a floating point
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 number and returns @code{t} if so, @code{nil} otherwise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 @code{floatp} does not exist in Emacs versions 18 and earlier.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 @defun integerp object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 This predicate tests whether its argument is an integer, and returns
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 @code{t} if so, @code{nil} otherwise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 @defun numberp object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 This predicate tests whether its argument is a number (either integer or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 floating point), and returns @code{t} if so, @code{nil} otherwise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 @defun natnump object
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 @cindex natural numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 The @code{natnump} predicate (whose name comes from the phrase
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 ``natural-number-p'') tests to see whether its argument is a nonnegative
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 integer, and returns @code{t} if so, @code{nil} otherwise. 0 is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 considered non-negative.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 @defun zerop number
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 This predicate tests whether its argument is zero, and returns @code{t}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 if so, @code{nil} otherwise. The argument must be a number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 These two forms are equivalent: @code{(zerop x)} @equiv{} @code{(= x 0)}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 @node Comparison of Numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 @section Comparison of Numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 @cindex number equality
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 To test numbers for numerical equality, you should normally use
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 @code{=}, not @code{eq}. There can be many distinct floating point
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 number objects with the same numeric value. If you use @code{eq} to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 compare them, then you test whether two values are the same
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 @emph{object}. By contrast, @code{=} compares only the numeric values
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 of the objects.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 At present, each integer value has a unique Lisp object in XEmacs Lisp.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 Therefore, @code{eq} is equivalent to @code{=} where integers are
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 concerned. It is sometimes convenient to use @code{eq} for comparing an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 unknown value with an integer, because @code{eq} does not report an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 error if the unknown value is not a number---it accepts arguments of any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 type. By contrast, @code{=} signals an error if the arguments are not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 numbers or markers. However, it is a good idea to use @code{=} if you
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 can, even for comparing integers, just in case we change the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 representation of integers in a future XEmacs version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 There is another wrinkle: because floating point arithmetic is not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 exact, it is often a bad idea to check for equality of two floating
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 point values. Usually it is better to test for approximate equality.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 Here's a function to do this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 (defconst fuzz-factor 1.0e-6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 (defun approx-equal (x y)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 (or (and (= x 0) (= y 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 (< (/ (abs (- x y))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 (max (abs x) (abs y)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 fuzz-factor)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 @cindex CL note---integers vrs @code{eq}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 @quotation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 @b{Common Lisp note:} Comparing numbers in Common Lisp always requires
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 @code{=} because Common Lisp implements multi-word integers, and two
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 distinct integer objects can have the same numeric value. XEmacs Lisp
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 can have just one integer object for any given value because it has a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 limited range of integer values.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 @end quotation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 In addition to numbers, all of the following functions also accept
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 characters and markers as arguments, and treat them as their number
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 equivalents.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 @defun = number &rest more-numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 This function returns @code{t} if all of its arguments are numerically
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 equal, @code{nil} otherwise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 (= 5)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 @result{} t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 (= 5 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 @result{} nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 (= 5 5.0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 @result{} t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 (= 5 5 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 @result{} nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 @defun /= number &rest more-numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 This function returns @code{t} if no two arguments are numerically
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 equal, @code{nil} otherwise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 (/= 5 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 @result{} t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 (/= 5 5 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 @result{} nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 (/= 5 6 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 @result{} t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 @defun < number &rest more-numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 This function returns @code{t} if the sequence of its arguments is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 monotonically increasing, @code{nil} otherwise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 (< 5 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 @result{} t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 (< 5 6 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 @result{} nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 (< 5 6 7)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 @result{} t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 @defun <= number &rest more-numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 This function returns @code{t} if the sequence of its arguments is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 monotonically nondecreasing, @code{nil} otherwise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 (<= 5 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 @result{} t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 (<= 5 6 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 @result{} t
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 (<= 5 6 5)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 @result{} nil
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 @defun > number &rest more-numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 This function returns @code{t} if the sequence of its arguments is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 monotonically decreasing, @code{nil} otherwise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 @defun >= number &rest more-numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 This function returns @code{t} if the sequence of its arguments is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 monotonically nonincreasing, @code{nil} otherwise.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 @defun max number &rest more-numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 This function returns the largest of its arguments.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 (max 20)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 @result{} 20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 (max 1 2.5)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 @result{} 2.5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 (max 1 3 2.5)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 @result{} 3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 @defun min number &rest more-numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 This function returns the smallest of its arguments.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 (min -4 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 @result{} -4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 @node Numeric Conversions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 @section Numeric Conversions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 @cindex rounding in conversions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 To convert an integer to floating point, use the function @code{float}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 @defun float number
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 This returns @var{number} converted to floating point.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 If @var{number} is already a floating point number, @code{float} returns
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 it unchanged.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 There are four functions to convert floating point numbers to integers;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 they differ in how they round. These functions accept integer arguments
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 also, and return such arguments unchanged.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 @defun truncate number
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 This returns @var{number}, converted to an integer by rounding towards
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 zero.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 @defun floor number &optional divisor
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 This returns @var{number}, converted to an integer by rounding downward
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 (towards negative infinity).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 If @var{divisor} is specified, @var{number} is divided by @var{divisor}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 before the floor is taken; this is the division operation that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 corresponds to @code{mod}. An @code{arith-error} results if
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 @var{divisor} is 0.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 @defun ceiling number
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 This returns @var{number}, converted to an integer by rounding upward
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 (towards positive infinity).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 @defun round number
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 This returns @var{number}, converted to an integer by rounding towards the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 nearest integer. Rounding a value equidistant between two integers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 may choose the integer closer to zero, or it may prefer an even integer,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 depending on your machine.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 @node Arithmetic Operations
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 @section Arithmetic Operations
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 XEmacs Lisp provides the traditional four arithmetic operations:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 addition, subtraction, multiplication, and division. Remainder and modulus
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 functions supplement the division functions. The functions to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 add or subtract 1 are provided because they are traditional in Lisp and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 commonly used.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 All of these functions except @code{%} return a floating point value
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 if any argument is floating.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 It is important to note that in XEmacs Lisp, arithmetic functions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 do not check for overflow. Thus @code{(1+ 134217727)} may evaluate to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 @minus{}134217728, depending on your hardware.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
413 @defun 1+ number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
414 This function returns @var{number} plus one. @var{number} may be a
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
415 number, character or marker. Markers and characters are converted to
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
416 integers.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
417
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 For example,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 (setq foo 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 @result{} 4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 (1+ foo)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 @result{} 5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 This function is not analogous to the C operator @code{++}---it does not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 increment a variable. It just computes a sum. Thus, if we continue,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 foo
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 @result{} 4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 If you want to increment the variable, you must use @code{setq},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 (setq foo (1+ foo))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 @result{} 5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 Now that the @code{cl} package is always available from lisp code, a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 more convenient and natural way to increment a variable is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 @w{@code{(incf foo)}}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
448 @defun 1- number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
449 This function returns @var{number} minus one. @var{number} may be a
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
450 number, character or marker. Markers and characters are converted to
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
451 integers.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 @defun abs number
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 This returns the absolute value of @var{number}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
458 @defun + &rest numbers
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 This function adds its arguments together. When given no arguments,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 @code{+} returns 0.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
462 If any of the arguments are characters or markers, they are first
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
463 converted to integers.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
464
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 (+)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 @result{} 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 (+ 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 @result{} 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 (+ 1 2 3 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 @result{} 10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
475 @defun - &optional number &rest other-numbers
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 The @code{-} function serves two purposes: negation and subtraction.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 When @code{-} has a single argument, the value is the negative of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 argument. When there are multiple arguments, @code{-} subtracts each of
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
479 the @var{other-numbers} from @var{number}, cumulatively. If there are
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
480 no arguments, an error is signaled.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
481
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
482 If any of the arguments are characters or markers, they are first
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
483 converted to integers.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 (- 10 1 2 3 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 @result{} 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 (- 10)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 @result{} -10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 (-)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 @result{} 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
495 @defun * &rest numbers
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 This function multiplies its arguments together, and returns the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 product. When given no arguments, @code{*} returns 1.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
499 If any of the arguments are characters or markers, they are first
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
500 converted to integers.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
501
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 (*)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 @result{} 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 (* 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 @result{} 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 (* 1 2 3 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508 @result{} 24
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
512 @defun / dividend &rest divisors
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
513 The @code{/} function serves two purposes: inversion and division. When
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
514 @code{/} has a single argument, the value is the inverse of the
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
515 argument. When there are multiple arguments, @code{/} divides
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
516 @var{dividend} by each of the @var{divisors}, cumulatively, returning
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
517 the quotient. If there are no arguments, an error is signaled.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
519 If none of the arguments are floats, then the result is an integer.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 This means the result has to be rounded. On most machines, the result
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 is rounded towards zero after each division, but some machines may round
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 differently with negative arguments. This is because the Lisp function
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 @code{/} is implemented using the C division operator, which also
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 permits machine-dependent rounding. As a practical matter, all known
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 machines round in the standard fashion.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
527 If any of the arguments are characters or markers, they are first
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
528 converted to integers.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
529
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 @cindex @code{arith-error} in division
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 If you divide by 0, an @code{arith-error} error is signaled.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 (@xref{Errors}.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 (/ 6 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 @result{} 3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 (/ 5 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 @result{} 2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 (/ 25 3 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 @result{} 4
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
543 (/ 3.0)
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
544 @result{} 0.3333333333333333
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 (/ -17 6)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 @result{} -2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 The result of @code{(/ -17 6)} could in principle be -3 on some
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 machines.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 @defun % dividend divisor
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 @cindex remainder
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 This function returns the integer remainder after division of @var{dividend}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 by @var{divisor}. The arguments must be integers or markers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 For negative arguments, the remainder is in principle machine-dependent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 since the quotient is; but in practice, all known machines behave alike.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 An @code{arith-error} results if @var{divisor} is 0.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 (% 9 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 @result{} 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566 (% -9 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 @result{} -1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 (% 9 -4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 @result{} 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 (% -9 -4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 @result{} -1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 For any two integers @var{dividend} and @var{divisor},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578 (+ (% @var{dividend} @var{divisor})
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 (* (/ @var{dividend} @var{divisor}) @var{divisor}))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 @noindent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 always equals @var{dividend}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 @defun mod dividend divisor
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 @cindex modulus
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 This function returns the value of @var{dividend} modulo @var{divisor};
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 in other words, the remainder after division of @var{dividend}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 by @var{divisor}, but with the same sign as @var{divisor}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 The arguments must be numbers or markers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 Unlike @code{%}, @code{mod} returns a well-defined result for negative
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 arguments. It also permits floating point arguments; it rounds the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 quotient downward (towards minus infinity) to an integer, and uses that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 quotient to compute the remainder.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 An @code{arith-error} results if @var{divisor} is 0.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603 (mod 9 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 @result{} 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 (mod -9 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 @result{} 3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 (mod 9 -4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 @result{} -3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 (mod -9 -4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 @result{} -1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 (mod 5.5 2.5)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620 @result{} .5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
621 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
624 For any two numbers @var{dividend} and @var{divisor},
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
625
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
626 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
627 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
628 (+ (mod @var{dividend} @var{divisor})
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
629 (* (floor @var{dividend} @var{divisor}) @var{divisor}))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
630 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
631 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
632
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
633 @noindent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
634 always equals @var{dividend}, subject to rounding error if either
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
635 argument is floating point. For @code{floor}, see @ref{Numeric
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
636 Conversions}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
637 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
638
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
639 @node Rounding Operations
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
640 @section Rounding Operations
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
641 @cindex rounding without conversion
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
642
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
643 The functions @code{ffloor}, @code{fceiling}, @code{fround} and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
644 @code{ftruncate} take a floating point argument and return a floating
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
645 point result whose value is a nearby integer. @code{ffloor} returns the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
646 nearest integer below; @code{fceiling}, the nearest integer above;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
647 @code{ftruncate}, the nearest integer in the direction towards zero;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
648 @code{fround}, the nearest integer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
649
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
650 @defun ffloor number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
651 This function rounds @var{number} to the next lower integral value, and
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
652 returns that value as a floating point number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
653 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
654
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
655 @defun fceiling number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
656 This function rounds @var{number} to the next higher integral value, and
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
657 returns that value as a floating point number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
658 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
659
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
660 @defun ftruncate number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
661 This function rounds @var{number} towards zero to an integral value, and
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
662 returns that value as a floating point number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
663 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
664
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
665 @defun fround number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
666 This function rounds @var{number} to the nearest integral value,
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
667 and returns that value as a floating point number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
668 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
669
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
670 @node Bitwise Operations
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
671 @section Bitwise Operations on Integers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
672
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
673 In a computer, an integer is represented as a binary number, a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
674 sequence of @dfn{bits} (digits which are either zero or one). A bitwise
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
675 operation acts on the individual bits of such a sequence. For example,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
676 @dfn{shifting} moves the whole sequence left or right one or more places,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
677 reproducing the same pattern ``moved over''.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
678
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
679 The bitwise operations in XEmacs Lisp apply only to integers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
680
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
681 @defun lsh integer1 count
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
682 @cindex logical shift
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
683 @code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
684 bits in @var{integer1} to the left @var{count} places, or to the right
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
685 if @var{count} is negative, bringing zeros into the vacated bits. If
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
686 @var{count} is negative, @code{lsh} shifts zeros into the leftmost
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
687 (most-significant) bit, producing a positive result even if
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
688 @var{integer1} is negative. Contrast this with @code{ash}, below.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
689
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
690 Here are two examples of @code{lsh}, shifting a pattern of bits one
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
691 place to the left. We show only the low-order eight bits of the binary
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
692 pattern; the rest are all zero.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
693
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
694 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
695 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
696 (lsh 5 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
697 @result{} 10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
698 ;; @r{Decimal 5 becomes decimal 10.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
699 00000101 @result{} 00001010
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
700
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
701 (lsh 7 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
702 @result{} 14
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
703 ;; @r{Decimal 7 becomes decimal 14.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
704 00000111 @result{} 00001110
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
705 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
706 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
707
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
708 @noindent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
709 As the examples illustrate, shifting the pattern of bits one place to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
710 the left produces a number that is twice the value of the previous
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
711 number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
712
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
713 Shifting a pattern of bits two places to the left produces results
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
714 like this (with 8-bit binary numbers):
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
715
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
716 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
717 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
718 (lsh 3 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
719 @result{} 12
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
720 ;; @r{Decimal 3 becomes decimal 12.}
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
721 00000011 @result{} 00001100
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
722 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
723 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
724
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
725 On the other hand, shifting one place to the right looks like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
726
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
727 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
728 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
729 (lsh 6 -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
730 @result{} 3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
731 ;; @r{Decimal 6 becomes decimal 3.}
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
732 00000110 @result{} 00000011
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
733 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
734
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
735 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
736 (lsh 5 -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
737 @result{} 2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
738 ;; @r{Decimal 5 becomes decimal 2.}
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
739 00000101 @result{} 00000010
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
740 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
741 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
742
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
743 @noindent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
744 As the example illustrates, shifting one place to the right divides the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
745 value of a positive integer by two, rounding downward.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
746
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
747 The function @code{lsh}, like all XEmacs Lisp arithmetic functions, does
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
748 not check for overflow, so shifting left can discard significant bits
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
749 and change the sign of the number. For example, left shifting
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
750 134,217,727 produces @minus{}2 on a 28-bit machine:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
751
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
752 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
753 (lsh 134217727 1) ; @r{left shift}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
754 @result{} -2
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
755 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
756
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
757 In binary, in the 28-bit implementation, the argument looks like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
758
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
759 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
760 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
761 ;; @r{Decimal 134,217,727}
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
762 0111 1111 1111 1111 1111 1111 1111
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
763 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
764 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
765
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
766 @noindent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
767 which becomes the following when left shifted:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
768
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
769 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
770 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
771 ;; @r{Decimal @minus{}2}
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
772 1111 1111 1111 1111 1111 1111 1110
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
773 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
774 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
775 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
776
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
777 @defun ash integer1 count
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
778 @cindex arithmetic shift
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
779 @code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
780 to the left @var{count} places, or to the right if @var{count}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
781 is negative.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
782
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
783 @code{ash} gives the same results as @code{lsh} except when
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
784 @var{integer1} and @var{count} are both negative. In that case,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
785 @code{ash} puts ones in the empty bit positions on the left, while
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
786 @code{lsh} puts zeros in those bit positions.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
787
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
788 Thus, with @code{ash}, shifting the pattern of bits one place to the right
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
789 looks like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
790
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
791 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
792 @group
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
793 (ash -6 -1) @result{} -3
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
794 ;; @r{Decimal @minus{}6 becomes decimal @minus{}3.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
795 1111 1111 1111 1111 1111 1111 1010
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
796 @result{}
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
797 1111 1111 1111 1111 1111 1111 1101
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
798 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
799 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
800
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
801 In contrast, shifting the pattern of bits one place to the right with
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
802 @code{lsh} looks like this:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
803
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
804 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
805 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
806 (lsh -6 -1) @result{} 134217725
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
807 ;; @r{Decimal @minus{}6 becomes decimal 134,217,725.}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
808 1111 1111 1111 1111 1111 1111 1010
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
809 @result{}
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
810 0111 1111 1111 1111 1111 1111 1101
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
811 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
812 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
813
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
814 Here are other examples:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
815
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
816 @c !!! Check if lined up in smallbook format! XDVI shows problem
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
817 @c with smallbook but not with regular book! --rjc 16mar92
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
818 @smallexample
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
819 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
820 ; @r{ 28-bit binary values}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
821
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
822 (lsh 5 2) ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
823 @result{} 20 ; = @r{0000 0000 0000 0000 0000 0001 0100}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
824 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
825 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
826 (ash 5 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
827 @result{} 20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
828 (lsh -5 2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
829 @result{} -20 ; = @r{1111 1111 1111 1111 1111 1110 1100}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
830 (ash -5 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
831 @result{} -20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
832 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
833 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
834 (lsh 5 -2) ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
835 @result{} 1 ; = @r{0000 0000 0000 0000 0000 0000 0001}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
836 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
837 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
838 (ash 5 -2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
839 @result{} 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
840 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
841 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
842 (lsh -5 -2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
843 @result{} 4194302 ; = @r{0011 1111 1111 1111 1111 1111 1110}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
844 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
845 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
846 (ash -5 -2) ; -5 = @r{1111 1111 1111 1111 1111 1111 1011}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
847 @result{} -2 ; = @r{1111 1111 1111 1111 1111 1111 1110}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
848 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
849 @end smallexample
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
850 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
851
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
852 @defun logand &rest ints-or-markers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
853 @cindex logical and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
854 @cindex bitwise and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
855 This function returns the ``logical and'' of the arguments: the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
856 @var{n}th bit is set in the result if, and only if, the @var{n}th bit is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
857 set in all the arguments. (``Set'' means that the value of the bit is 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
858 rather than 0.)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
859
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
860 For example, using 4-bit binary numbers, the ``logical and'' of 13 and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
861 12 is 12: 1101 combined with 1100 produces 1100.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
862 In both the binary numbers, the leftmost two bits are set (i.e., they
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
863 are 1's), so the leftmost two bits of the returned value are set.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
864 However, for the rightmost two bits, each is zero in at least one of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
865 the arguments, so the rightmost two bits of the returned value are 0's.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
866
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
867 @noindent
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
868 Therefore,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
869
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
870 @example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
871 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
872 (logand 13 12)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
873 @result{} 12
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
874 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
875 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
876
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
877 If @code{logand} is not passed any argument, it returns a value of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
878 @minus{}1. This number is an identity element for @code{logand}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
879 because its binary representation consists entirely of ones. If
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
880 @code{logand} is passed just one argument, it returns that argument.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
881
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
882 @smallexample
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
883 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
884 ; @r{ 28-bit binary values}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
885
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
886 (logand 14 13) ; 14 = @r{0000 0000 0000 0000 0000 0000 1110}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
887 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
888 @result{} 12 ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
889 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
890
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
891 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
892 (logand 14 13 4) ; 14 = @r{0000 0000 0000 0000 0000 0000 1110}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
893 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
894 ; 4 = @r{0000 0000 0000 0000 0000 0000 0100}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
895 @result{} 4 ; 4 = @r{0000 0000 0000 0000 0000 0000 0100}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
896 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
897
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
898 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
899 (logand)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
900 @result{} -1 ; -1 = @r{1111 1111 1111 1111 1111 1111 1111}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
901 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
902 @end smallexample
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
903 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
904
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
905 @defun logior &rest ints-or-markers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
906 @cindex logical inclusive or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
907 @cindex bitwise or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
908 This function returns the ``inclusive or'' of its arguments: the @var{n}th bit
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
909 is set in the result if, and only if, the @var{n}th bit is set in at least
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
910 one of the arguments. If there are no arguments, the result is zero,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
911 which is an identity element for this operation. If @code{logior} is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
912 passed just one argument, it returns that argument.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
913
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
914 @smallexample
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
915 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
916 ; @r{ 28-bit binary values}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
917
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
918 (logior 12 5) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
919 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
920 @result{} 13 ; 13 = @r{0000 0000 0000 0000 0000 0000 1101}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
921 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
922
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
923 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
924 (logior 12 5 7) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
925 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
926 ; 7 = @r{0000 0000 0000 0000 0000 0000 0111}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
927 @result{} 15 ; 15 = @r{0000 0000 0000 0000 0000 0000 1111}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
928 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
929 @end smallexample
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
930 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
931
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
932 @defun logxor &rest ints-or-markers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
933 @cindex bitwise exclusive or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
934 @cindex logical exclusive or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
935 This function returns the ``exclusive or'' of its arguments: the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
936 @var{n}th bit is set in the result if, and only if, the @var{n}th bit is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
937 set in an odd number of the arguments. If there are no arguments, the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
938 result is 0, which is an identity element for this operation. If
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
939 @code{logxor} is passed just one argument, it returns that argument.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
940
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
941 @smallexample
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
942 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
943 ; @r{ 28-bit binary values}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
944
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
945 (logxor 12 5) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
946 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
947 @result{} 9 ; 9 = @r{0000 0000 0000 0000 0000 0000 1001}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
948 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
949
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
950 @group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
951 (logxor 12 5 7) ; 12 = @r{0000 0000 0000 0000 0000 0000 1100}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
952 ; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
953 ; 7 = @r{0000 0000 0000 0000 0000 0000 0111}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
954 @result{} 14 ; 14 = @r{0000 0000 0000 0000 0000 0000 1110}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
955 @end group
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
956 @end smallexample
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
957 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
958
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
959 @defun lognot integer
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
960 @cindex logical not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
961 @cindex bitwise not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
962 This function returns the logical complement of its argument: the @var{n}th
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
963 bit is one in the result if, and only if, the @var{n}th bit is zero in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
964 @var{integer}, and vice-versa.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
965
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
966 @example
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
967 (lognot 5)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
968 @result{} -6
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
969 ;; 5 = @r{0000 0000 0000 0000 0000 0000 0101}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
970 ;; @r{becomes}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
971 ;; -6 = @r{1111 1111 1111 1111 1111 1111 1010}
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
972 @end example
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
973 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
974
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
975 @node Math Functions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
976 @section Standard Mathematical Functions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
977 @cindex transcendental functions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
978 @cindex mathematical functions
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
979
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
980 These mathematical functions are available if floating point is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
981 supported (which is the normal state of affairs). They allow integers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
982 as well as floating point numbers as arguments.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
983
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
984 @defun sin number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
985 @defunx cos number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
986 @defunx tan number
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
987 These are the ordinary trigonometric functions, with argument measured
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
988 in radians.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
989 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
990
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
991 @defun asin number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
992 The value of @code{(asin @var{number})} is a number between @minus{}pi/2
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
993 and pi/2 (inclusive) whose sine is @var{number}; if, however, @var{number}
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
994 is out of range (outside [-1, 1]), then the result is a NaN.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
995 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
996
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
997 @defun acos number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
998 The value of @code{(acos @var{number})} is a number between 0 and pi
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
999 (inclusive) whose cosine is @var{number}; if, however, @var{number}
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1000 is out of range (outside [-1, 1]), then the result is a NaN.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1001 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1002
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1003 @defun atan number &optional number2
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1004 The value of @code{(atan @var{number})} is a number between @minus{}pi/2
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1005 and pi/2 (exclusive) whose tangent is @var{number}.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1006
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1007 If optional argument @var{number2} is supplied, the function returns
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1008 @code{atan2(@var{number},@var{number2})}.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1009 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1010
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1011 @defun sinh number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1012 @defunx cosh number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1013 @defunx tanh number
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1014 These are the ordinary hyperbolic trigonometric functions.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1015 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1016
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1017 @defun asinh number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1018 @defunx acosh number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1019 @defunx atanh number
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1020 These are the inverse hyperbolic trigonometric functions.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1021 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1022
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1023 @defun exp number
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1024 This is the exponential function; it returns @i{e} to the power
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1025 @var{number}. @i{e} is a fundamental mathematical constant also called the
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1026 base of natural logarithms.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1027 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1028
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1029 @defun log number &optional base
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1030 This function returns the logarithm of @var{number}, with base @var{base}.
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1031 If you don't specify @var{base}, the base @var{e} is used. If @var{number}
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1032 is negative, the result is a NaN.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1033 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1034
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1035 @defun log10 number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1036 This function returns the logarithm of @var{number}, with base 10. If
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1037 @var{number} is negative, the result is a NaN. @code{(log10 @var{x})}
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1038 @equiv{} @code{(log @var{x} 10)}, at least approximately.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1039 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1040
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1041 @defun expt x y
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1042 This function returns @var{x} raised to power @var{y}. If both
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1043 arguments are integers and @var{y} is positive, the result is an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1044 integer; in this case, it is truncated to fit the range of possible
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1045 integer values.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1046 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1047
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1048 @defun sqrt number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1049 This returns the square root of @var{number}. If @var{number} is negative,
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1050 the value is a NaN.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1051 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1052
444
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1053 @defun cube-root number
576fb035e263 Import from CVS: tag r21-2-37
cvs
parents: 428
diff changeset
1054 This returns the cube root of @var{number}.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1055 @end defun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1056
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1057 @node Random Numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1058 @section Random Numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1059 @cindex random numbers
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1060
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1061 A deterministic computer program cannot generate true random numbers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1062 For most purposes, @dfn{pseudo-random numbers} suffice. A series of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1063 pseudo-random numbers is generated in a deterministic fashion. The
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1064 numbers are not truly random, but they have certain properties that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1065 mimic a random series. For example, all possible values occur equally
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1066 often in a pseudo-random series.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1067
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1068 In XEmacs, pseudo-random numbers are generated from a ``seed'' number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1069 Starting from any given seed, the @code{random} function always
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1070 generates the same sequence of numbers. XEmacs always starts with the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1071 same seed value, so the sequence of values of @code{random} is actually
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1072 the same in each XEmacs run! For example, in one operating system, the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1073 first call to @code{(random)} after you start XEmacs always returns
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1074 -1457731, and the second one always returns -7692030. This
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1075 repeatability is helpful for debugging.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1076
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1077 If you want truly unpredictable random numbers, execute @code{(random
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1078 t)}. This chooses a new seed based on the current time of day and on
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1079 XEmacs's process @sc{id} number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1080
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1081 @defun random &optional limit
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1082 This function returns a pseudo-random integer. Repeated calls return a
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1083 series of pseudo-random integers.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1084
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1085 If @var{limit} is a positive integer, the value is chosen to be
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1086 nonnegative and less than @var{limit}.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1087
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1088 If @var{limit} is @code{t}, it means to choose a new seed based on the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1089 current time of day and on XEmacs's process @sc{id} number.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1090 @c "XEmacs'" is incorrect usage!
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1091
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1092 On some machines, any integer representable in Lisp may be the result
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1093 of @code{random}. On other machines, the result can never be larger
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1094 than a certain maximum or less than a certain (negative) minimum.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1095 @end defun