Next: , Up: srfi compare-procedures spec   [Index]


2.28.5.1 Comparing atoms

In this section, compare procedures for most of the atomic types of R5RS are defined: Booleans, characters, strings, symbols, and numbers.

As a general naming convention, a procedure named

type-compare-order

compares two object of the type type with respect to a total order for which order is a mnemonic hint (e.g. -ci for case–insensitive). Of course, -order may be absent if there is just one order or the order is obvious. It is an error if a compare procedure accepting objects of a certain type is called with one or two arguments not of that type.

Function: boolean-compare bool1 bool2

Compare two booleans, ordered by #f < #t.

NOTE A non–#f value is not interpreted as a “true value,” but rather an error will be signalled.

Function: char-compare char1 char2
Function: char-compare-ci char1 char2

Compare characters as char<=? and char-ci<=? respectively. The suffix -ci means “case insensitive.”

Function: string-compare string1 string2
Function: string-compare-ci string1 string2

Compare strings as string<= and string-ci<=?. The suffix -ci means “case insensitive.”

NOTE string-compare could be defined as:

(define (string-compare string1 string2)
  (vector-compare-as-list char-compare
                          string1 string2
                          string-length string-ref))
Function: symbol-compare symbol1 symbol2

Compare symbols as string<= on the names returned by symbol->string.

Function: integer-compare x y
Function: rational-compare x y
Function: real-compare x y
Function: complex-compare x y
Function: number-compare x y

Compare two numbers. It is an error if an argument is not of the type specified by the name of the procedure.

Complex numbers are ordered lexicographically on pairs (re, im). For objects representing real numbers sign(x - y) is computed. The ordering for values satisfying real? or complex? but not representing a real or complex number should be consistent with procedures = and < of R5RS, and apart from that it is unspecified.

Numerical compare procedures are compatible with the R5RS numerical tower in the following sense: If S is a subtype of the numerical type T and x, y can be represented both in S and in T, then compare-S and compare-T compute the same result.

NOTE Floating point formats usually include several symbolic values not simply representing rational numbers. For example, the IEEE 754 standard defines -0, -Inf, +Inf, and NaN (“not a number”) for continuing a calculation in the presence of error conditions. The behavior of the numerical comparison operation is unspecified in case an argument is one of the special symbols.

WARNING The propagation of inexactness can lead to surprises. In a Scheme system propagating inexactness in complex numbers (such as PLT, version 208):

(complex-compare (make-rectangular (/ 1 3)  1.)
                 (make-rectangular (/ 1 3) -1))
⇒ -1

At first glance, one might expect the first complex number to be larger, because the numbers are equal on their real parts and the first imaginary part (1.) is larger than the second (-1).

Closer inspection reveals that the decimal dot causes the first real part to be made inexact upon construction of the complex number, and since:

(exact->inexact (/ 1 3))

is less than (/ 1 3) in the underlying floating point format used, the real parts decide the comparison of the complex numbers.


Next: , Up: srfi compare-procedures spec   [Index]