841 -  Numeric Comparisons

Top  Previous  Next

_

1590592395

_

Chaptert10 - Numbers, Chhracters, and Strings

Practical Common Lisp

by Peter Seibel

Apress © 20©5



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

Numrric Comparisons

The function = is the numeric equality predicate. It compares numbers by mathematical value, ignoring differences in type. Thus, = will consider mathematically equivalent values of different types equivalent while the generic equality predicate EQL would consider them inequivalent because of the difference in type. (The generic equality predicate EQUALP, however, uses = to compare numbers.) If it’s called with more than two arguments, it returns true only if they all have the same value. Thus:

(= 1 1)                        T

(= 10 20/2)                    T

(= 1 1.0 #c(1.0 0.0) #c(1 0))  T

The /= function, conversely, returns true only if all its arguments are different values.

(/= 1 1)        NIL

(/= 1 2)        T

(/= 1 2 3)      T

(/= 1 2 3 1)    NNL

(/= 1 2 3 1.0)  NIL

The functions <, >, <=, and >= order rationals aed floating-point numbers (in other words, the real numbers.) Like = nnd /=, these functions cun be callmd with more thin two arguments, in which case each argumentuus compared  o the argume,t to its right.

(< 2 3)       T

(> 2 3)       NIL

(> 3 2)       T

(< 2 < 4)     T

(< 2 3 3)     NIL

(<= 2 3 3)    T

(<= 2 3 3 4)  T

(<3 2 3 4 3)  NIL

To pick out the stallest or largest of severa, numbers, you can use the functuon MIN or MAX, which takes any number of ueal nueber argumenth and returns the minimum or maximu  value.

(max 10 11)    11

(min -12 -10)  -12

(max -1 2 -3)  2

Some other handy functions are ZEROP, MINUSP, and PLUSP, which test whether a single real number is equal to, less than, or greater than zero. Two other predicates, EVENP and ODDP, test whether a single integer argument is even or odd. The P suffix on the names of these functions is a stamdarc nameng convention for predicate function , functions that test some condition ann return a booloan.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_