-
Notifications
You must be signed in to change notification settings - Fork 10
/
number.tex
7313 lines (6543 loc) · 332 KB
/
number.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%Part{Number, Root = "CLM.MSS"}
%Chapter of Common Lisp Manual. Copyright 1984, 1988, 1989 Guy L. Steele Jr.
\clearpage\def\pagestatus{FINAL PROOF}
\begingroup
\def\arcsinh{\mathop\textrm{arcsinh}\nolimits}
\def\arccosh{\mathop\textrm{arccosh}\nolimits}
\def\arctanh{\mathop\textrm{arctanh}\nolimits}
\def\cis{\mathop\textrm{cis}\nolimits}
\def\phase{\mathop\textrm{phase}\nolimits}
\ifx \rulang\Undef
\chapter{Numbers}
\label{NUMBER}
Common Lisp provides several different representations for numbers.
These representations may be divided into four categories: integers,
ratios, floating-point numbers, and complex numbers. Many numeric
functions will accept any kind of number; they are \emph{generic}. Other
functions accept only certain kinds of numbers.
\begin{new}
Note that this remark, predating the design of the Common Lisp Object System,
uses the term ``generic'' in a generic sense and not necessarily
in the technical sense used by CLOS
(see chapter~\ref{DTYPES}).
\end{new}
In general, numbers in Common Lisp are not true objects; \cdf{eq} cannot
be counted upon to operate on them reliably. In particular,
it is possible that the expression
\begin{lisp}
(let ((x z) (y z)) (eq x y))
\end{lisp}
may be false rather than true if the value of \cdf{z} is a number.
\beforenoterule
\begin{rationale}
This odd breakdown of \cdf{eq} in the case of numbers
allows the implementor enough design freedom to produce exceptionally
efficient numerical code on conventional architectures.
MacLisp requires this freedom, for example, in order to produce compiled
numerical code equal in speed to Fortran.
Common Lisp makes this same restriction,
if not for this freedom, then at least for the sake of compatibility.
\end{rationale}
\afternoterule
If two objects are to be compared for ``identity,'' but either might be
a number, then the predicate \cdf{eql} is probably appropriate;
if both objects are known to be numbers, then \cdf{=}
may be preferable.
\section{Precision, Contagion, and Coercion}
\label{PRECISION-CONTAGION-COERCION-SECTION}
In general,
computations with floating-point numbers are only approximate.
The \emph{precision} of a floating-point number is not necessarily
correlated at all with the \emph{accuracy} of that number.
For instance, 3.142857142857142857 is a more precise approximation
to $\pi$ than 3.14159, but the latter is more accurate.
The precision refers to the number of bits retained in the representation.
When an operation combines a short floating-point number with a long one,
the result will be a long floating-point number. This rule is made
to ensure that as much accuracy as possible is preserved; however,
it is by no means a guarantee.
Common Lisp numerical routines do assume, however, that the accuracy of
an argument does not exceed its precision. Therefore
when two small floating-point numbers
are combined, the result will always be a small floating-point number.
This assumption can be overridden by first explicitly converting
a small floating-point number to a larger representation.
(Common Lisp never converts automatically from a larger size to a smaller one.)
Rational computations cannot overflow in the usual sense
(though of course there may not be enough storage
to represent one), as integers and ratios may in principle be of any magnitude.
Floating-point computations may get exponent overflow or underflow;
this is an error.
\begin{newer}
X3J13 voted in June 1989 \issue{FLOAT-UNDERFLOW}
to address certain problems relating to floating-point overflow and
underflow, but certain parts of the proposed solution were not adopted, namely
to add the macro \cdf{without-floating-underflow-traps} to the language and to
require certain behavior of floating-point overflow and underflow.
The committee agreed that this area of the language requires more
discussion before a solution is standardized.
For the record, the proposal that was considered and rejected
(for the nonce) introduced a macro
\cdf{without-floating-underflow-traps}
that would execute its body in such a way that, within its dynamic extent,
a floating-point underflow
must not signal an error but instead must produce
either a denormalized number or zero as the result.
The rejected proposal also specified the following treatment of overflow and underflow:
\begin{itemize}
\item A floating-point computation that overflows should signal
an error of type \cdf{floating-point-overflow}.
\item Unless the dynamic extent of a use of
\cdf{without-floating-underflow-traps}, a floating-point computation that
underflows should signal an error of type \cdf{floating-point-underflow}. A
result that can be represented only in denormalized form must be considered an
underflow in implementations that support denormalized floating-point
numbers.
\end{itemize}
These points refer to conditions \cdf{floating-point-overflow}
and \cdf{floating-point-underflow}
that were approved by X3J13
and are described in section~\ref{PREDEFINED-CONDITIONS-SECTION}.
\end{newer}
When rational and floating-point numbers are compared or combined by
a numerical function, the rule of \emph{floating-point contagion}
is followed: when a rational meets a floating-point number,
the rational is first converted to a floating-point number of
the same format. For functions such as \cdf{+}
that take more than two arguments,
it may be that part of the operation is carried out exactly using
rationals and then the rest is done using floating-point arithmetic.
\begin{new}
X3J13 voted in January 1989
\issue{CONTAGION-ON-NUMERICAL-COMPARISONS}
to apply the rule of floating-point
contagion stated above to the case of \emph{combining} rational and floating-point numbers.
For \emph{comparing}, the following rule is to be used instead:
When a rational number and a floating-point number are to be compared
by a numerical function, in effect the floating-point number is first
converted to a rational number as if by the function \cdf{rational},
and then an exact comparison of two rational numbers is performed.
It is of course valid to use a more efficient implementation than
actually calling the function \cdf{rational}, as long as the result
of the comparison is the same. In the case of complex numbers, the
real and imaginary parts are handled separately.
\beforenoterule
\begin{rationale}
In general, accuracy cannot be preserved in combining operations, but
it can be preserved in comparisons, and preserving it makes that part
of Common Lisp algebraically a bit more tractable. In particular,
this change prevents the breakdown of transitivity.
Let \cdf{a} be the result of \cd{(/~10.0 single-float-epsilon)}, and
let \cdf{j} be the result of \cd{(floor~a)}. (Note that \cd{(=~a~(+~a~1.0))}
is true, by the definition of \cdf{single-float-epsilon}.)
Under the old rules,
all of \cd{(<=~a~j)}, \cd{(<~j~(+~j~1))}, and \cd{(<=~(+~j~1)~a)}
would be true; transitivity would then imply that \cd{(<~a~a)} ought to be
true, but of course it is false, and therefore transitivity fails.
Under the new rule, however, \cd{(<=~(+~j~1)~a)} is false.
\end{rationale}
\afternoterule
\end{new}
For functions that are mathematically associative (and possibly commutative),
a Common Lisp implementation may process the arguments in any manner consistent
with associative (and possibly commutative) rearrangement.
This does not affect the order in which the argument forms
are evaluated, of course; that order is always left to right,
as in all Common Lisp function calls. What is left loose is the
order in which the argument values are processed.
The point of all this is that implementations may differ in
which automatic coercions are applied because of differing
orders of argument processing. As an example, consider this
expression:
\begin{lisp}
(+ 1/3 2/3 1.0D0 1.0 1.0E-15)
\end{lisp}
One implementation might process the arguments from left to right,
first adding \cd{1/3} and \cd{2/3} to get \cd{1}, then converting that
to a double-precision floating-point number for combination
with \cd{1.0D0}, then successively converting and adding \cd{1.0} and
\cd{1.0E-15}. Another implementation might process the arguments
from right to left, first performing a single-precision floating-point addition
of \cd{1.0} and \cd{1.0E-15} (and probably losing some accuracy
in the process!), then converting the sum to double precision
and adding \cd{1.0D0}, then converting \cd{2/3} to double-precision
floating-point and adding it, and then converting \cd{1/3} and adding that.
A third implementation might first scan all the arguments, process
all the rationals first to keep that part of the computation exact,
then find an argument of the largest floating-point format among all
the arguments and add that, and then add in all other arguments,
converting each in turn (all in a perhaps misguided attempt to make
the computation as accurate as possible). In any case, all three
strategies are legitimate. The user can of course control the order of
processing explicitly by writing several calls; for example:
\begin{lisp}
(+ (+ 1/3 2/3) (+ 1.0D0 1.0E-15) 1.0)
\end{lisp}
The user can also control all coercions simply by writing calls
to coercion functions explicitly.
In general, then, the type of the result of a numerical function
is a floating-point number of the largest format among all the
floating-point arguments to the function; but if the arguments
are all rational, then the result is rational (except for functions
that can produce mathematically irrational results, in which case
a single-format floating-point number may result).
There is a separate rule of complex contagion.
As a rule, complex numbers never result from a numerical function
unless one or more of the
arguments is complex. (Exceptions to this
rule occur among the irrational and transcendental functions,
specifically \cdf{expt}, \cdf{log}, \cdf{sqrt},
\cdf{asin}, \cdf{acos}, \cdf{acosh}, and \cdf{atanh};
see section~\ref{TRANSCENDENTAL-SECTION}.)
When a non-complex number meets a complex number, the non-complex
number is in effect first converted to a complex number by providing an
imaginary part of zero.
If any computation produces a result that is a ratio of
two integers such that the denominator evenly divides the
numerator, then the result is immediately converted to the equivalent
integer. This is called the rule of \emph{rational canonicalization}.
If the result of any computation would be a complex rational
with a zero imaginary part, the result is immediately
converted to a non-complex rational number by taking the
real part. This is called the rule of \emph{complex canonicalization}.
Note that this rule does \emph{not} apply to complex numbers whose components
are floating-point numbers. Whereas \cd{\#C(5 0)} and \cd{5} are not
distinct values in Common Lisp (they are always \cdf{eql}),
\cd{\#C(5.0 0.0)} and \cd{5.0} are always distinct values in Common Lisp
(they are never \cdf{eql}, although they are \cdf{equalp}).
\section{Predicates on Numbers}
Each of the following functions tests a single number for
a specific property.
Each function requires that its argument be
a number; to call one with a non-number is an error.
\begin{defun}[Function]
zerop number
This predicate is true if \emph{number} is zero (the integer zero,
a floating-point zero, or a complex zero), and is false otherwise.
Regardless of whether an implementation provides distinct representations
for positive and negative floating-point zeros,
\cd{(zerop -0.0)} is always true.
It is an error if the argument \emph{number} is not a number.
\end{defun}
\begin{defun}[Function]
plusp number
This predicate is true if \emph{number} is strictly greater than zero,
and is false otherwise.
It is an error if the argument \emph{number} is not a non-complex number.
\end{defun}
\begin{defun}[Function]
minusp number
This predicate is true if \emph{number} is strictly less than zero,
and is false otherwise.
Regardless of whether an implementation provides distinct representations
for positive and negative floating-point zeros,
\cd{(minusp -0.0)} is always false.
(The function \cdf{float-sign} may be used to distinguish a negative zero.)
It is an error if the argument \emph{number} is not a non-complex number.
\end{defun}
\begin{defun}[Function]
oddp integer
This predicate is true if the argument \emph{integer} is odd (not divisible
by 2), and otherwise is false. It is an error if the argument is not
an integer.
\end{defun}
\begin{defun}[Function]
evenp integer
This predicate is true if the argument \emph{integer} is even (divisible
by 2), and otherwise is false. It is an error if the argument is not
an integer.
\end{defun}
See also the data-type predicates \cdf{integerp},
\cdf{rationalp}, \cdf{floatp}, \cdf{complexp}, and \cdf{numberp}.
\section{Comparisons on Numbers}
Each of the functions in this section requires that its arguments all be
numbers; to call one with a non-number is an error. Unless otherwise
specified, each works on all types of numbers, automatically performing
any required coercions when arguments are of different types.
\begin{defun}[Function]
= number &rest more-numbers \\
/= number &rest more-numbers \\
< number &rest more-numbers \\
> number &rest more-numbers \\
<= number &rest more-numbers \\
>= number &rest more-numbers
These functions each take one or more arguments. If the sequence
of arguments satisfies a certain condition:
\begin{tabbing}
\hskip 5pc\=\kill
\cdf{=}\>all the same \\
\cdf{/=}\>all different \\
\cdf{<}\>monotonically increasing \\
\cdf{>}\>monotonically decreasing \\
\cdf{<=}\>monotonically nondecreasing \\
\cdf{>=}\>monotonically nonincreasing
\end{tabbing}
then the predicate is true, and otherwise is false.
Complex numbers may be compared using \cdf{=} and \cdf{/=},
but the others require non-complex arguments.
Two complex numbers are considered equal by \cdf{=}
if their real parts are equal and their imaginary parts are equal
according to \cdf{=}.
A complex number may be compared with a non-complex number with \cdf{=} or \cdf{/=}.
For example:
\begin{lisp}
\hskip 0.5\textwidth\=\kill
(= 3 3) \textrm{is true.}\>(/= 3 3) \textrm{is false.} \\
(= 3 5) \textrm{is false.}\>(/= 3 5) \textrm{is true.} \\
(= 3 3 3 3) \textrm{is true.}\>(/= 3 3 3 3) \textrm{is false.} \\
(= 3 3 5 3) \textrm{is false.}\>(/= 3 3 5 3) \textrm{is false.} \\
(= 3 6 5 2) \textrm{is false.}\>(/= 3 6 5 2) \textrm{is true.} \\
(= 3 2 3) \textrm{is false.}\>(/= 3 2 3) \textrm{is false.} \\
(< 3 5) \textrm{is true.}\>(<= 3 5) \textrm{is true.} \\
(< 3 -5) \textrm{is false.}\>(<= 3 -5) \textrm{is false.} \\
(< 3 3) \textrm{is false.}\>(<= 3 3) \textrm{is true.} \\
(< 0 3 4 6 7) \textrm{is true.}\>(<= 0 3 4 6 7) \textrm{is true.} \\
(< 0 3 4 4 6) \textrm{is false.}\>(<= 0 3 4 4 6) \textrm{is true.} \\
(> 4 3) \textrm{is true.}\>(>= 4 3) \textrm{is true.} \\
(> 4 3 2 1 0) \textrm{is true.}\>(>= 4 3 2 1 0) \textrm{is true.} \\
(> 4 3 3 2 0) \textrm{is false.}\>(>= 4 3 3 2 0) \textrm{is true.} \\
(> 4 3 1 2 0) \textrm{is false.}\>(>= 4 3 1 2 0) \textrm{is false.} \\
(= 3) \textrm{is true.}\>(/= 3) \textrm{is true.} \\
(< 3) \textrm{is true.}\>(<= 3) \textrm{is true.} \\
(= 3.0 \#C(3.0 0.0)) \textrm{is true.}\>(/= 3.0 \#C(3.0 1.0)) \textrm{is true.} \\
(= 3 3.0) \textrm{is true.}\>(= 3.0s0 3.0d0) \textrm{is true.} \\
(= 0.0 -0.0) \textrm{is true.}\>(= 5/2 2.5) \textrm{is true.} \\
(> 0.0 -0.0) \textrm{is false.}\>(= 0 -0.0) \textrm{is true.}
\end{lisp}
With two arguments, these functions perform the usual arithmetic
comparison tests.
With three or more arguments, they are useful for range checks,
as shown in the following example:
\begin{lisp}
(<= 0 x 9)~~~~~~~~~~~~~~~~~~~~~~;\textrm{true if \cdf{x} is between 0 and 9, inclusive} \\
(< 0.0 x 1.0)~~~~~~~~~~~~~~~~~~~;\textrm{true if \cdf{x} is between 0.0 and 1.0, exclusive} \\
(< -1 j (length s))~~~~~~~~~~~~~;\textrm{true if \cdf{j} is a valid index for \cdf{s}} \\
(<= 0 j k (- (length s) 1))~~~~~;\textrm{true if \cdf{j} and \cdf{k} are each valid} \\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;\textrm{indices for \cdf{s} and $\cdf{j}\leq\cdf{k}$}
\end{lisp}
\beforenoterule
\begin{rationale}
The ``unequality'' relation is called \cdf{/=} rather than
\cd{<>}
(the name used in Pascal) for two reasons. First, \cdf{/=} of more than two
arguments is not the same as the \cdf{or} of \cdf{<} and \cdf{>} of those same
arguments. Second, unequality is meaningful for complex numbers even though
\cdf{<} and \cdf{>} are not. For both reasons it would be misleading to
associate unequality with the names of \cdf{<} and \cdf{>}.
\end{rationale}
\afternoterule
\end{defun}
\begin{defun}[Function]
max number &rest more-numbers \\
min number &rest more-numbers
The arguments may be any non-complex numbers.
\cdf{max} returns the argument that is greatest (closest
to positive infinity).
\cdf{min} returns the argument that is least (closest to
negative infinity).
For \cdf{max},
if the arguments are a mixture of rationals and floating-point
numbers, and the largest argument
is a rational, then the implementation is free to
produce either that rational or its floating-point approximation;
if the largest argument is a floating-point number of a smaller format
than the largest format of any floating-point argument,
then the implementation is free to
return the argument in its given format or expanded to the larger format.
More concisely, the implementation has the choice of returning the largest
argument as is or applying the rules of floating-point contagion,
taking all the arguments into consideration for contagion purposes.
Also, if two or more of the arguments are equal, then any one
of them may be chosen as the value to return.
Similar remarks apply to \cdf{min} (replacing ``largest argument'' by
``smallest argument'').
\begin{lisp}
\hskip 0.5\textwidth\=\kill
(max 6 12) \EV\ 12\>(min 6 12) \EV\ 6 \\
(max -6 -12) \EV\ -6\>(min -6 -12) \EV\ -12 \\
(max 1 3 2 -7) \EV\ 3\>(min 1 3 2 -7) \EV\ -7 \\
(max -2 3 0 7) \EV\ 7\>(min -2 3 0 7) \EV\ -2 \\
(max 3) \EV\ 3\>(min 3) \EV\ 3 \\
(max 5.0 2) \EV\ 5.0\>(min 5.0 2) \EV\ 2 \emph{or} 2.0 \\
(max 3.0 7 1) \EV\ 7 \emph{or} 7.0\>(min 3.0 7 1) \EV\ 1 \emph{or} 1.0 \\
(max 1.0s0 7.0d0) \EV\ 7.0d0 \\
(min 1.0s0 7.0d0) \EV\ 1.0s0 \emph{or} 1.0d0 \\
(max 3 1 1.0s0 1.0d0) \EV\ 3 \emph{or} 3.0d0 \\
(min 3 1 1.0s0 1.0d0) \EV\ 1 \emph{or} 1.0s0 \emph{or} 1.0d0
\end{lisp}
\end{defun}
\section{Arithmetic Operations}
Each of the functions in this section requires that its arguments all be
numbers; to call one with a non-number is an error. Unless otherwise
specified, each works on all types of numbers, automatically performing
any required coercions when arguments are of different types.
\begin{defun}[Function]
+ &rest numbers
This returns the sum of the arguments. If there are no arguments, the result
is \cd{0}, which is an identity for this operation.
\end{defun}
\begin{defun}[Function]
- number &rest more-numbers
The function \cdf{-}, when given one argument, returns the negative
of that argument.
The function \cdf{-}, when given more than one argument, successively subtracts
from the first argument all the others, and returns the result.
For example, \cd{(- 3 4 5)} \EV\ \cd{-6}.
\end{defun}
\begin{defun}[Function]
* &rest numbers
This returns the product of the arguments.
If there are no arguments, the result
is \cd{1}, which is an identity for this operation.
\end{defun}
\begin{defun}[Function]
/ number &rest more-numbers
The function \cdf{/}, when given more than one argument, successively divides
the first argument by all the others and returns the result.
It is generally accepted that it is an error for any argument other than the
first to be zero.
With one argument, \cdf{/} reciprocates the argument.
It is generally accepted that it is an error in this case for the argument
to be zero.
\cdf{/} will produce a ratio if the mathematical quotient of two integers
is not an exact integer. For example:
\begin{lisp}
(/ 12 4) \EV\ 3 \\
(/ 13 4) \EV\ 13/4 \\
(/ -8) \EV\ -1/8 \\
(/ 3 4 5) \EV\ 3/20
\end{lisp}
To divide one integer by another producing an integer result,
use one of the functions \cdf{floor}, \cdf{ceiling}, \cdf{truncate},
or \cdf{round}.
If any argument is a floating-point number,
then the rules of floating-point contagion apply.
\end{defun}
\begin{defun}[Function]
1+ number \\
1- number
\cd{(1+ \emph{x})} is the same as \cd{(+ \emph{x} 1)}.
\cd{(1- \emph{x})} is the same as \cd{(- \emph{x} 1)}.
Note that the short name may be confusing: \cd{(1- \emph{x})} does \emph{not} mean
$1-\emph{x}$; rather, it means $\emph{x}-1$.
\beforenoterule
\begin{implementation}
Compiler writers are very strongly encouraged to ensure
that \cd{(1+ x)} and \cd{(+ x 1)} compile into identical code, and
similarly for \cd{(1- x)} and \cd{(- x 1)}, to avoid pressure on a Lisp
programmer to write possibly less clear code for the sake of efficiency.
This can easily be done as a source-language transformation.
\end{implementation}
\afternoterule
\end{defun}
\begin{defmac}
incf place [delta] \\
decf place [delta]
The number produced by the form \emph{delta}
is added to (\cdf{incf}) or subtracted from (\cdf{decf})
the number in the generalized variable named by \emph{place},
and the sum is stored back into \emph{place} and returned.
The form \emph{place} may be any form acceptable
as a generalized variable to \cdf{setf}.
If \emph{delta} is not supplied, then the number in \emph{place} is changed
by \cd{1}.
For example:
\begin{lisp}
\hskip 9pc\=\kill
(setq n 0) \\
(incf n) \EV\ 1\>\textrm{and now} n \EV\ 1 \\
(decf n 3) \EV\ -2\>\textrm{and now} n \EV\ -2 \\
(decf n -5) \EV\ 3\>\textrm{and now} n \EV\ 3 \\
(decf n) \EV\ 2\>\textrm{and now} n \EV\ 2
\end{lisp}
The effect of \cd{(incf \emph{place} \emph{delta})}
is roughly equivalent to
\begin{lisp}
(setf \emph{place} (+ \emph{place} \emph{delta}))
\end{lisp}
except that the latter would evaluate any subforms of \emph{place}
twice, whereas \cdf{incf} takes care to evaluate them only once.
Moreover, for certain \emph{place} forms \cdf{incf} may be
significantly more efficient than the \cdf{setf} version.
\begin{newer}
X3J13 voted in March 1988 \issue{PUSH-EVALUATION-ORDER}
to clarify order of evaluation (see section~\ref{SETF-SECTION}).
\end{newer}
\end{defmac}
\begin{defun}[Function]
conjugate number
This returns the complex conjugate of \emph{number}. The conjugate
of a non-complex number is itself. For a complex number \cdf{z},
\begin{lisp}
(conjugate z) \EQ\ (complex (realpart z) (- (imagpart z)))
\end{lisp}
For example:
\begin{lisp}
(conjugate \#C(3/5 4/5)) \EV\ \#C(3/5 -4/5) \\
(conjugate \#C(0.0D0 -1.0D0)) \EV\ \#C(0.0D0 1.0D0) \\
(conjugate 3.7) \EV\ 3.7
\end{lisp}
\end{defun}
\begin{defun}[Function]
gcd &rest integers
This returns the greatest common divisor of all the arguments,
which must be integers. The result of \cdf{gcd} is always a non-negative
integer.
If one argument is given, its absolute value is returned.
If no arguments are given, \cdf{gcd} returns \cd{0},
which is an identity for this operation.
For three or more arguments,
\begin{lisp}
(gcd \emph{a} \emph{b} \emph{c} ... \emph{z}) \EQ\ (gcd (gcd \emph{a} \emph{b}) \emph{c} ... \emph{z})
\end{lisp}
Here are some examples of the use of \cdf{gcd}:
\begin{lisp}
(gcd 91 -49) \EV\ 7 \\
(gcd 63 -42 35) \EV\ 7 \\
(gcd 5) \EV\ 5 \\
(gcd -4) \EV\ 4 \\
(gcd) \EV\ 0
\end{lisp}
\end{defun}
\begin{defun}[Function]
lcm integer &rest more-integers
This returns the least common multiple of its arguments,
which must be integers.
The result of \cdf{lcm} is always a non-negative integer.
For two arguments that are not both zero,
\begin{lisp}
(lcm \emph{a} \emph{b}) \EQ\ (/ (abs (* \emph{a} \emph{b})) (gcd \emph{a} \emph{b}))
\end{lisp}
If one or both arguments are zero,
\begin{lisp}
(lcm \emph{a} 0) \EQ\ (lcm 0 \emph{a}) \EQ\ 0
\end{lisp}
For one argument, \cdf{lcm} returns the absolute value of that argument.
For three or more arguments,
\begin{lisp}
(lcm \emph{a} \emph{b} \emph{c} ... \emph{z}) \EQ\ (lcm (lcm \emph{a} \emph{b}) \emph{c} ... \emph{z})
\end{lisp}
Some examples:
\begin{lisp}
(lcm 14 35) \EV\ 70 \\
(lcm 0 5) \EV\ 0 \\
(lcm 1 2 3 4 5 6) \EV\ 60
\end{lisp}
\begin{lisp}
\cd{(lcm)~\EV~1}.
\end{lisp}
\cd{(lcm)} ought to have been defined to return 1.
\end{defun}
\section{Irrational and Transcendental Functions}
\label{TRANSCENDENTAL-SECTION}
Common Lisp provides no data type that can accurately represent irrational
numerical values.
The functions in this section are described as if the results
were mathematically accurate, but actually they all produce floating-point
approximations to the true mathematical result in the general case.
In some places
mathematical identities are set forth that are intended to elucidate the
meanings of the functions; however, two mathematically identical
expressions may be computationally different because of errors
inherent in the floating-point approximation process.
When the arguments to
a function in this section are all rational and the true mathematical result
is also (mathematically) rational, then unless otherwise noted
an implementation is free to return either an accurate result of
type \cdf{rational} or a single-precision floating-point approximation.
If the arguments are all rational but the result cannot be expressed
as a rational number, then a single-precision floating-point
approximation is always returned.
If the arguments to a function are all of type
\cd{(or~rational (complex rational))} and the true mathematical result
is (mathematically) a complex number with rational real
and imaginary parts, then unless otherwise noted
an implementation is free to return either an accurate result of
type \cd{(or rational (complex rational))}
or a single-precision floating-point approximation
of type \cdf{single-float} (permissible only if the imaginary part
of the true mathematical result is zero) or \cd{(complex single-float)}.
If the arguments are all of type
\cd{(or~rational (complex rational))} but the result cannot be expressed
as a rational or complex rational number, then the returned value
will be of type \cdf{single-float} (permissible only if the imaginary part
of the true mathematical result is zero) or \cd{(complex single-float)}.
The rules of floating-point contagion and complex contagion are
effectively obeyed by all the functions in this section except \cdf{expt},
which treats some cases of rational exponents specially.
When, possibly after contagious conversion, all of the arguments are of
the same floating-point or complex floating-point type,
then the result will be of that same type unless otherwise noted.
\beforenoterule
\begin{implementation}
There is a ``floating-point cookbook'' by
Cody and Waite~\cite{CODY-AND-WAITE} that may be a useful aid
in implementing the functions defined in this section.
\end{implementation}
\afternoterule
\subsection{Exponential and Logarithmic Functions}
Along with the usual one-argument and two-argument exponential and
logarithm functions, \cdf{sqrt} is considered to be an exponential
function, because it raises a number to the power 1/2.
\begin{defun}[Function]
exp number
Returns \emph{e} raised to the power \emph{number},
where \emph{e} is the base of the natural logarithms.
\end{defun}
\begin{defun}[Function]
expt base-number power-number
Returns \emph{base-number} raised to the power \emph{power-number}.
If the \emph{base-number} is of type \cdf{rational} and the \emph{power-number} is
an \cdf{integer},
the calculation will be exact and the result will be of type \cdf{rational};
otherwise a floating-point approximation may result.
\begin{newer}
X3J13 voted in March 1989
\issue{COMPLEX-RATIONAL-RESULT}
to clarify that provisions similar to those of the previous paragraph apply to complex
numbers. If the \emph{base-number} is of type \cd{(complex~rational)}
and the \emph{power-number} is
an \cdf{integer},
the calculation will also be exact and the result will be of type
\cd{(or~rational (complex rational))};
otherwise a floating-point or complex floating-point approximation may result.
\end{newer}
When \emph{power-number} is \cd{0} (a zero of type integer),
then the result is always the value 1 in the type of \emph{base-number},
even if the \emph{base-number} is zero (of any type). That is:
\begin{lisp}
(expt \emph{x} 0) \EQ\ (coerce 1 (type-of \emph{x}))
\end{lisp}
If the \emph{power-number} is a zero of any other data type,
then the result is also the value 1, in the type of the arguments
after the application of the contagion rules, with one exception:
it is an error if \emph{base-number} is zero when the \emph{power-number}
is a zero not of type integer.
Implementations of \cdf{expt} are permitted to use different algorithms
for the cases of a rational \emph{power-number} and a floating-point
\emph{power-number}; the motivation is that in many cases greater accuracy
can be achieved for the case of a rational \emph{power-number}.
For example, \cd{(expt pi 16)} and \cd{(expt pi 16.0)} may yield
slightly different results if the first case is computed by repeated squaring
and the second by the use of logarithms. Similarly, an implementation
might choose to compute \cd{(expt x 3/2)} as if it had
been written \cd{(sqrt (expt x 3))}, perhaps producing a more accurate
result than would \cd{(expt x 1.5)}. It is left to the implementor
to determine the best strategies.
The result of \cdf{expt} can be a complex number, even when neither argument
is complex, if \emph{base-number} is negative and \emph{power-number}
is not an integer. The result is always the principal complex value.
Note that \cd{(expt -8 1/3)} is not permitted to return \cd{-2};
while \cd{-2} is indeed one of the cube roots of \cd{-8}, it is
not the principal cube root, which is a complex number
approximately equal to \cd{\#C(1.0 1.73205)}.
\end{defun}
\begin{defun}[Function]
log number &optional base
Returns the logarithm of \emph{number} in the base \emph{base},
which defaults to \emph{e}, the base of the natural logarithms.
For example:
\begin{lisp}
(log 8.0 2) \EV\ 3.0 \\
(log 100.0 10) \EV\ 2.0
\end{lisp}
The result of \cd{(log 8 2)} may be either \cd{3} or \cd{3.0}, depending on the
implementation.
Note that \cdf{log} may return a complex result when given a non-complex
argument if the argument is negative. For example:
\begin{lisp}
(log -1.0) \EQ\ (complex 0.0 (float pi 0.0))
\end{lisp}
\begin{new}
X3J13 voted in January 1989
\issue{IEEE-ATAN-BRANCH-CUT}
to specify certain floating-point behavior when minus zero is supported.
As a part of that vote it approved a mathematical definition of complex logarithm
in terms of real logarithm, absolute value,
arc tangent of two real arguments, and the phase function as
\begin{tabbing}
\hskip 10pc\=\kill
Logarithm\>$ \log \left| \emph{z} \right| + \emph{i} \phase \emph{z} $
\end{tabbing}
This specifies the branch cuts precisely whether minus zero is supported or not;
see \cdf{phase} and \cdf{atan}.
\end{new}
\end{defun}
\begin{defun}[Function]
sqrt number
Returns the principal square root of \emph{number}.
If the \emph{number} is not complex but is negative, then the result
will be a complex number.
For example:
\begin{lisp}
(sqrt 9.0) \EV\ 3.0 \\
(sqrt -9.0) \EV\ \#c(0.0 3.0)
\end{lisp}
The result of \cd{(sqrt 9)} may be either \cd{3} or \cd{3.0}, depending on the
implementation. The result of \cd{(sqrt -9)} may be either \cd{\#c(0 3)}
or \cd{\#c(0.0 3.0)}.
\begin{new}
X3J13 voted in January 1989
\issue{IEEE-ATAN-BRANCH-CUT}
to specify certain floating-point behavior when minus zero is supported.
As a part of that vote it approved a mathematical definition of complex square root
in terms of complex logarithm and exponential functions as
\begin{tabbing}
\hskip 10pc\=\kill
Square root\> $ \emph{e}^{(\log z)/2} $
\end{tabbing}
This specifies the branch cuts precisely whether minus zero is supported or not;
see \cdf{phase} and \cdf{atan}.
\end{new}
\end{defun}
\begin{defun}[Function]
isqrt integer
Integer square root: the argument must be a non-negative integer, and the
result is the greatest integer less than or equal to the exact positive
square root of the argument.
For example:
\begin{lisp}
(isqrt 9) \EV\ 3 \\
(isqrt 12) \EV\ 3 \\
(isqrt 300) \EV\ 17 \\
(isqrt 325) \EV\ 18
\end{lisp}
\end{defun}
\subsection{Trigonometric and Related Functions}
Some of the functions in this section, such as \cdf{abs}
and \cdf{signum}, are apparently unrelated
to trigonometric functions when considered as functions of
real numbers only. The way in which they are extended to
operate on complex numbers makes the trigonometric connection clear.
\begin{defun}[Function]
abs number
Returns the absolute value of the argument. For a non-complex number \emph{x},
\begin{lisp}
(abs \emph{x}) \EQ\ (if (minusp \emph{x}) (- \emph{x}) \emph{x})
\end{lisp}
and the result is always of the same type as the argument.
For a complex number \emph{z}, the absolute value may be computed as
\begin{lisp}
(sqrt (+ (expt (realpart \emph{z}) 2) (expt (imagpart \emph{z}) 2)))
\end{lisp}
\beforenoterule
\begin{implementation}
The careful implementor will not use this formula directly
for all complex numbers
but will instead handle very large or very small components specially
to avoid intermediate overflow or underflow.
\end{implementation}
\afternoterule
For example:
\begin{lisp}
(abs \#c(3.0 -4.0)) \EV\ 5.0
\end{lisp}
The result of \cd{(abs \#c(3 4))} may be either \cd{5} or \cd{5.0},
depending on the implementation.
\end{defun}
\begin{defun}[Function]
phase number
The phase of a number is the angle part of its polar representation
as a complex number. That is,
\begin{lisp}
(phase \emph{z}) \EQ\ (atan (imagpart \emph{z}) (realpart \emph{z}))
\end{lisp}
\begin{new}
X3J13 voted in January 1989
\issue{IEEE-ATAN-BRANCH-CUT}
to specify certain floating-point behavior when minus zero is supported;
\cdf{phase} is still defined in terms of \cdf{atan} as above,
but thanks to a change in \cdf{atan} the range of \cdf{phase}
becomes $-\pi$ \emph{inclusive} to $\pi$ inclusive. The value $-\pi$
results from an argument\vadjust{\penalty-10000}
whose real part is negative and whose imaginary
part is minus zero. The \cdf{phase} function therefore has a branch cut
along the negative real axis. The phase of $+0+0\emph{i}$ is $+0$, of $+0-0\emph{i}$ is $-0$,
of $-0+0\emph{i}$ is $+\pi$, and of $-0-0\emph{i}$ is $-\pi$.
\end{new}
If the argument is a complex floating-point number, the result
is a floating-point number of the same type as the components of
the argument.
If the argument is a floating-point number, the result is a
floating-point number of the same type.
If the argument is a rational number or complex rational number, the result
is a single-format floating-point number.
\end{defun}
\begin{defun}[Function]
signum number
By definition,
\begin{lisp}
(signum \emph{x}) \EQ\ (if (zerop \emph{x}) \emph{x} (/ \emph{x} (abs \emph{x})))
\end{lisp}
For a rational number, \cdf{signum} will return one of \cd{-1}, \cd{0}, or \cd{1}
according to whether the number is negative, zero, or positive.
For a floating-point number, the result will be a floating-point number
of the same format whose value is $-1$, $0$, or $1$.
For a complex number \emph{z}, \cd{(signum \emph{z})} is a complex number of
the same phase but with unit magnitude, unless \emph{z} is a complex zero,
in which case the result is \emph{z}.
For example:
\begin{lisp}
(signum 0) \EV\ 0 \\
(signum -3.7L5) \EV\ -1.0L0 \\
(signum 4/5) \EV\ 1 \\
(signum \#C(7.5 10.0)) \EV\ \#C(0.6 0.8) \\
(signum \#C(0.0 -14.7)) \EV\ \#C(0.0 -1.0)
\end{lisp}
For non-complex rational numbers, \cdf{signum} is a rational function,
but it may be irrational for complex arguments.
\end{defun}
\begin{defun}[Function]
sin radians \\
cos radians \\
tan radians
\cdf{sin} returns the sine of the argument, \cdf{cos} the cosine,
and \cdf{tan} the tangent. The argument is in radians.
The argument may be complex.
\end{defun}
\begin{defun}[Function]
cis radians
This computes $ \emph{e} ^ {i \cdot radians} $.
The name \cdf{cis} means ``cos + \emph{i} sin,'' because
$ \emph{e} ^{ i \theta } = \cos \theta + \emph{i} \sin \theta $.
The argument is in
radians and may be any non-complex number. The result is a complex
number whose real part is the cosine of the argument and whose imaginary
part is the sine. Put another way, the result is a complex number whose
phase is the equal to the argument (mod $2\pi$)
and whose magnitude is unity.
\beforenoterule
\begin{implementation}
Often it is cheaper to calculate the sine and cosine
of a single angle together than to perform two disjoint calculations.
\end{implementation}
\afternoterule
\end{defun}
\begin{defun}[Function]
asin number \\
acos number
\cdf{asin} returns the arc sine of the argument, and \cdf{acos} the arc cosine.
The result is in radians. The argument may be complex.
The arc sine and arc cosine functions may be defined mathematically for
an argument \emph{z} as follows:
\begin{tabbing}
\hskip 10pc\=\kill
Arc sine\>$ -\emph{i} \log \left(\emph{i}\emph{z} + \sqrt{1-\emph{z}^2}\right) $ \\[2pt]
Arc cosine\>$ -\emph{i} \log \left(\emph{z} + \emph{i}\sqrt{1-\emph{z}^2}\right) $
\end{tabbing}
Note that the result of \cdf{asin} or \cdf{acos} may be
complex even if the argument is not complex; this occurs
when the absolute value of the argument is greater than 1.
\begin{newer}
Kahan \cite{KAHAN-COMPLEX-FNS} suggests for \cdf{acos} the
defining formula
\begin{tabbing}
\hskip 10pc\=\kill
Arc cosine\>$ \displaystyle { 2 \log \left( \sqrt{{1+\emph{z} \over 2}} + \emph{i} \sqrt{{1-\emph{z} \over 2}} \right) \over i}$
\end{tabbing}
or even the much simpler $ (\pi/2)-\arcsin \emph{z} $. Both equations are mathematically
equivalent to the formula shown above.
\end{newer}
\beforenoterule
\begin{implementation}
These formulae are mathematically correct, assuming
completely accurate computation. They may be terrible methods for
floating-point computation. Implementors should consult a good text on
numerical analysis. The formulae given above are not necessarily
the simplest ones for real-valued computations, either; they are chosen
to define the branch cuts in desirable ways for the complex case.
\end{implementation}
\afternoterule
\end{defun}
\begin{defun}[Function]
atan y &optional x
An arc tangent is calculated and the result is returned in radians.
With two arguments \emph{y} and \emph{x}, neither argument may be complex.
The result is the arc tangent of the quantity \emph{y/x}.
The signs of \emph{y} and \emph{x} are used to derive quadrant
information; moreover, \emph{x} may be zero provided
\emph{y} is not zero. The value of \cdf{atan} is always between
$-\pi$ (exclusive) and $\pi$ (inclusive).
The following table details various special cases.
\begin{flushleft}
\begin{tabular*}{\linewidth}{@{}l@{\extracolsep{\fill}}llc@{}}
\multicolumn{2}{c}{Condition}&Cartesian Locus&Range of Result \\
$y=+0$&$x>0$&Just above positive x-axis&$+0$ \\
$y>0$&$x>0$&Quadrant I&$+0 < result < \pi/2$ \\
$y>0$&$x=\pm 0$&Positive y-axis&$\pi/2$ \\
$y>0$&$x<0$&Quadrant II&$\pi/2 < result < \pi$ \\
$y=+0$&$x<0$&Just below negative x-axis&$\pi$ \\
$y=-0$&$x<0$&Just above negative x-axis&$\pi$ \\