Numbers

Numbers

Robbert Haarman

2010-12-11


Introduction

Numbers are essential for many programs. The Scheme standard defines a rich number system with several classes of number. Although entirely optional, most Scheme implementations support the full number tower: integers, rationals, reals and complex numbers.

Operations on Numbers

; Test if something is a number
(number? 12) ; => #t
(number? "foo") ; => #f

; Simele Arithmetic
(+ 3 2) ; => 5
(- 3 2) ; => 1
(* 3 2) ; => 6
(/ 3 2) ; => 3/2 (or an approximation)

; Test for equal, greater, lesser, etc. value
(= 3 2) ; => #f
(= 3 3) ; => #t
(> 3 2) ; => #t
(< 3 2) ; => #f
(<= 2 3) ; => #t
(>= 3 3) ; => #t

For more information, see Numerical operations in the Scheme standard.


Integers

Integers are the simplest kind of number. Examples of integers are 1, -3 and 372965437563768649384093850934. In other languages, the range of integers that can be represented is often limited to what the CPU internally works with. In Scheme parlance, such numbers are called fixnums. A 32-bit machine, for instance, would be able to represent integers in the range -4294967296 to 4294967295. While this range is wide enough for most purposes, there are situations in which larger numbers need to be represented. Many Scheme implementations support bignums, which allow integers to be of any magnitude, provided enough memory.

Operations on Integers

; Test if something is an integer
(integer? 3) ; => #t
(integer? (/ 6 2)) ; => #t
(integer? (/ 6 5)) ; => #f

Rationals

The next level of the number tower is held by rational numbers. A rational number can be pictured as a fraction of two integers. Examples of rationals are: 1/2, 3/4 and 5/3.

Operations on Rationals

; Test if something is a rational
(rational? 3/4) ; => #t
(rational? 5) ; => #t
(rational? 5.3) ; => #f

; Extract numerator and denominator
(numerator 3/4) ; => 3
(denominator 3/4) ; => 4
(numerator (/ 6 5)) ; => 6

Reals

The class of reals consists of any number that can be written using decimal notation, even those numbers whose sequence of decimals never terminates. Rational numbers are reals, as are transcedental numbers like the square root of 2. Real numbers are typically represented internally as flonums (floating point numbers), which means they have a finite precision that decreases with magnitude. Some procedures are provided to deal with this inexact representation.

Operations on Reals

; Test if something is a real
(real? 3.0) ; => #t
(real? 4) ; => #t
(real? 5/3) ; => #t
(real? (sqrt 2)) ; => #t

; Test if a number is exact or inexact
; Note that these depend on the implenetation
(exact? 3) ; => #t
(exact? 3.0) ; => #f
(exact? 3/1) ; => #t

; Convert between exact and inexact
; Note that conversion may fail or return different results
(exact->inexact 3) ; => 3.0
(inexact->exact 3.1) ; => 31/10

Complex Numbers

Complex numbers consist of a real and an imaginary part (reals are complex numbers whose imaginary part is 0). Two common representations exist. The first (rectangular) consists of a pair of reals, the first one specifying the real part, the second one the imaginary part. The other (polar) representation uses an angle and a magnitude to indicate a position in the real-imaginary plane. Depending on the operations performed, one or the other representation leads to better performance.

Operations on Complex Numbers

; Test if something is a complex number
(complex? 3+2i) ; => #t
(complex? 3) ; => #t


; Create complex numbers
(make-rectangular 3 2) ; => 3+2i
(make-polar 3 2) ; => -1.24844050964143+2.72789228047704i

; Extract components
(real-part 3+2i) ; => 3
(imag-part 3+2i) ; => 2
(magnitude 3+2i) ; => 3.60555127546399
(angle 3+2i) ; => 0.588002603547567
Valid XHTML 1.1! Valid CSS! Viewable with Any Browser