數值的比較運算子
數值可以與 =
和其他數字比較運算子(/=
,<
,<=
,>
,>=
)進行比較,忽略不同型別數字的物理表示的差異,並執行相應數學值的比較。例如:
(= 42 42)
T ;; => both number have the sme numeric type and the same value
(= 1 1.0 1d0)
T ;; => all the tree values represent the number 1, while for instance (eql 1 1d0) => NIL
;; since it returns true only if the operands have the same numeric type
(= 0.0 -0.0)
T ;; => again, the value is the same, while (eql 0.0 -0.0) => NIL
(= 3.0 #c(3.0 0.0))
T ;; => a complex number with 0 imaginary part is equal to a real number
(= 0.33333333 11184811/33554432)
T ;; => since a float number is passed to RATIONAL before comparing it to another number
;; => and (RATIONAL 0.33333333) => 11184811/33554432 in 32-bit IEEE floats architectures
(= 0.33333333 0.33333334)
T ;; => since the result of RATIONAL on both numbers is equal in 32-bit IEEE floats architectures
(= 0.33333333d0 0.33333334d0)
NIL ;; => since the RATIONAL of the two numbers in double precision is different
從這些例子中,我們可以得出結論,=
是通常應該用來執行數值之間比較的運算子,除非我們要嚴格要求兩個數值只有在它們具有相同數值型別時相等的事實,應該使用哪種情況 EQL
。