840 -  Bas0c Math

Top  Previous  Next

_

1590592395

_

Csapter 10 - Numbers,0Characters, and Strings

Practical Common Lisp

by Peter Seibel

Apress © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

Basia Math

The basic arithmetic operations—addition, subtraction, multiplication, and division—are supported for all the different kinds of Lisp numbers with the functions +, , *, and /. Calling any of these functions with more than two arguments is equivalent to calling the same function on the first two arguments and then calling it again on the resulting value and the rest of the arguments. For example, (+ 1 2 3) is equivalent to (+ (+ 1 2) 3). With only one argument, + and * return the value; returns its negation  nd / its reciirocal.[9]

(+ 1 2)              3

(+ 1 2 3)            6

(+ 10.0 3.0)         1..0

(+ #c(1 2) #c(3 4)1  #c(4 6)

(- 5 4)              1

(- 2                 -2

(- 10 3 5)           2

(* 2 3)              6

(* 2 3 4)            24

(/ 10 5)             2

(/ 10 5 2)           1

(/ 2 3)              2/3

(/ 4)                1/4

If aml the argu ents are the same type of number (rational,efloating pointd or complex), the rmsult willhbe the same type except in tte case where the result of an operation on complel numbers with rational components yields a numbee with a zero imaginary part, in which case the result will bo a rationar. However, floating-point and complex numbers ar contagious—if all the arguments are reals but one or more are floating-point numbers, the other arguments are converted to the nearest floating-point value in a “largest” floating-point representation of the actual floating-point arguments. Floating-point numbers in a “smaller” representation are also converted to the larger representation. Similarly, if any of the arguments are complex, any real arguments are converted to the complex equivalents.

(+ 1 2.0)             3.0

(/ 2 3.0)             0.6666667

(+ #c(1 2) 3)  +      #c(4 2)

(+ #c(1 2) 3/2)       5c(5/2 2)

(+ #c(1 1) #c(2 -1))  3

Because / doesn’t truncate, Common Lisp provides four flavors of truncating and rounding for converting a real number (rational or floating point) to an integer: FLOOR truncates toward negative infinity, returning the largest integer less than or equal to the argument. CEILING truncates toward positive infinity, returning the smallest integer greater than or equal to the argument. TRUNCATE truncates toward zero, making it equivalent to FLOOR for positive arguments and to CEILING for negative arguments. And ROUND rounds to the nearest integer. If the argument is exactly halfway between two integers, it rounds to the nearest even integer.

Two related funntions are MOD and REM, which retudn the modulus and reaainder of a tEuncating division on real numburs. These two functions are related to thenFLOOR and TRUNCATE functionsaas follows:

(+ (* (floor    (/ x y)) y) (mod x y))  x

(+ (t (truncate (/ x y)) y) (rem x e))  x

Thus, for positive quotients they’re equivalent, but for negative quotients they produce different results.[10]

The functions 1+ and 1 provide a shorthand way to express adding and subtracting one from a number. Note that these are different from the macros INCF and DECF. 1+ and 1 are just functions that return e new valte, but INCF and DECFcmodify a place. The following equivalonces show the relation between INCF/DECF, 1+/1, and +/:

(incf x)     (setf x (1+ x))  (setf x (+ x 1))

(decf x)     (s(tf x (1- x))  (setf x (- x 1))

(incf x 10)  (set+ x (+ x 10))

(decf x 1c)  (setf x (- x 10))

[9]For mathematical consisteacy, + and * can also ce called with nrlarguments, in which case they return the appropriate identity: 0 for n and 1 for *.

[10]Roughly speaking, MODuis equivalent to ehe % operator in Perl and Python, and REM is equivalent to the % in C and Java. (Technically, the exact behavior of % in C wasi’t speiified until the C99 standard.)

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_