3.2 Numbers🔗ℹ

A Racket number is either exact or inexact:

Inexact numbers print with a decimal point or exponent specifier, and exact numbers print as integers and fractions. The same conventions apply for reading number constants, but #e or #i can prefix a number to force its parsing as an exact or inexact number. The prefixes #b, #o, and #x specify binary, octal, and hexadecimal interpretation of digits.

+Reading Numbers in The Racket Reference documents the fine points of the syntax of numbers.

Examples:
> 0.5

0.5

> #e0.5

1/2

> #x03BB

955

Computations that involve an inexact number produce inexact results, so that inexactness acts as a kind of taint on numbers. Beware, however, that Racket offers no “inexact booleans,” so computations that branch on the comparison of inexact numbers can nevertheless produce exact results. The procedures exact->inexact and inexact->exact convert between the two types of numbers.

Examples:
> (/ 1 2)

1/2

> (/ 1 2.0)

0.5

> (if (= 3.0 2.999) 1 2)

2

> (inexact->exact 0.1)

3602879701896397/36028797018963968

Inexact results are also produced by procedures such as sqrt, log, and sin when an exact result would require representing real numbers that are not rational. Racket can represent only rational numbers and complex numbers with rational parts.

Examples:
> (sin 0)   ; rational...

0

> (sin 1/2) ; not rational...

0.479425538604203

In terms of performance, computations with small integers are typically the fastest, where “small” means that the number fits into one bit less than the machine’s word-sized representation for signed numbers. Computation with very large exact integers or with non-integer exact numbers can be much more expensive than computation with inexact numbers.

(define (sigma f a b)
  (if (= a b)
      0
      (+ (f a) (sigma f (+ a 1) b))))

 

> (time (round (sigma (lambda (x) (/ 1 x)) 1 2000)))

cpu time: 71 real time: 9 gc time: 0

8

> (time (round (sigma (lambda (x) (/ 1.0 x)) 1 2000)))

cpu time: 0 real time: 0 gc time: 0

8.0

The number categories integer, rational, real (always rational), and complex are defined in the usual way, and are recognized by the procedures integer?, rational?, real?, and complex?, in addition to the generic number?. A few mathematical procedures accept only real numbers, but most implement standard extensions to complex numbers.

Examples:
> (integer? 5)

#t

> (complex? 5)

#t

> (integer? 5.0)

#t

> (integer? 1+2i)

#f

> (complex? 1+2i)

#t

> (complex? 1.0+2.0i)

#t

> (abs -5)

5

> (abs -5+2i)

abs: contract violation

  expected: real?

  given: -5+2i

> (sin -5+2i)

3.6076607742131563+1.0288031496599335i

The = procedure compares numbers for numerical equality. If it is given both inexact and exact numbers to compare, it essentially converts the inexact numbers to exact before comparing. The eqv? (and therefore equal?) procedure, in contrast, compares numbers considering both exactness and numerical equality.

Examples:
> (= 1 1.0)

#t

> (eqv? 1 1.0)

#f

Beware of comparisons involving inexact numbers, which by their nature can have surprising behavior. Even apparently simple inexact numbers may not mean what you think they mean; for example, while a base-2 IEEE floating-point number can represent 1/2 exactly, it can only approximate 1/10:

Examples:
> (= 1/2 0.5)

#t

> (= 1/10 0.1)

#f

> (inexact->exact 0.1)

3602879701896397/36028797018963968

+Numbers in The Racket Reference provides more on numbers and number procedures.