ZetCode

Scheme numbers

last modified November 24, 2026 Guile 3.0

Scheme arithmetic is designed to give the mathematically correct answer whenever it can. Integers have no fixed size, dividing two integers gives an exact fraction, and floating-point numbers appear only when a program asks for them or when a result cannot be exact. This article explores the numeric tower of Scheme with GNU Guile 3.0.7, from big integers to square roots of negative numbers.

Big integers

Scheme integers grow as large as memory allows. Small integers are stored directly, and larger ones switch to an arbitrary-precision representation, the bignum, without any action by the program.

integers.scm
(display (* 123456789 987654321))
(newline)
(display (expt 2 100))
(newline)
(display (* 99999999999999999999 99999999999999999999))
(newline)
(display (exact-integer? (expt 2 100)))
(newline)

The first product already exceeds the range of a 32-bit integer, and (expt 2 100), 2 to the power of 100, is far beyond 64 bits. The product of two twenty-digit numbers is computed exactly. Overflow does not happen: the result is always the exact integer, and exact-integer? confirms its type.

$ guile integers.scm
121932631112635269
1267650600228229401496703205376
9999999999999999999800000000000000000001
#t

Rationals

Dividing one integer by another gives an exact rational number rather than a truncated integer or a floating-point approximation. Rationals are written and printed as fractions such as 1/3, and Scheme always reduces them to lowest terms.

rationals.scm
(display (/ 1 3))
(newline)
(display (+ 1/3 1/6))
(newline)
(display (/ 6 4))
(newline)
(display (/ 6 3))
(newline)
(display (* 2/3 3/4))
(newline)
(display (numerator 6/4))
(display " ")
(display (denominator 6/4))
(newline)
(display (rational? 1/3))
(display " ")
(display (integer? 4/2))
(newline)

(/ 1 3) is the exact fraction 1/3, and 1/3 + 1/6 is exactly 1/2. (/ 6 4) is reduced to 3/2, and (/ 6 3) becomes the integer 2. numerator and denominator take the reduced fraction apart, so 6/4 gives 3 and 2. An integer is a rational with denominator 1, and (integer? 4/2) is true because 4/2 is the integer 2.

$ guile rationals.scm
1/3
1/2
3/2
2
1/2
3 2
#t #t

Exact and inexact numbers

Every number is either exact or inexact. A number written with a decimal point or an exponent is inexact, stored as a 64-bit floating-point value with the usual rounding errors. Combining an exact number with an inexact one gives an inexact result. exact->inexact and inexact->exact convert between the two kinds.

exactness.scm
(display (+ 0.1 0.2))
(newline)
(display (+ 1/10 2/10))
(newline)
(display (exact? 1/2))
(display " ")
(display (inexact? 0.5))
(newline)
(display (exact->inexact 1/3))
(newline)
(display (inexact->exact 0.5))
(newline)
(display (inexact->exact 0.1))
(newline)
(display (+ 1/2 0.5))
(newline)
(display (= 1/2 0.5))
(newline)

0.1 + 0.2 shows the familiar rounding error of binary floating point, while the exact fractions 1/10 and 2/10 add up to exactly 3/10. Converting 1/3 to an inexact number gives the closest double. inexact->exact gives the exact value of the stored double: 0.5 is exactly 1/2, but 0.1 has no exact binary representation, and its true stored value is the large fraction shown. Adding 1/2 and 0.5 gives the inexact 1.0, and = compares the two kinds of numbers by value.

$ guile exactness.scm
0.30000000000000004
3/10
#t #t
0.3333333333333333
1/2
3602879701896397/36028797018963968
1.0
#t

Integer division

Division of integers that should yield an integer is done by separate procedures. quotient truncates toward zero, and remainder and modulo give the matching remainder. The two remainders differ when the operands have different signs: remainder takes the sign of the dividend and modulo the sign of the divisor.

division.scm
(define (show-division a b)
  (display a)
  (display " and ")
  (display b)
  (display ": quotient ")
  (display (quotient a b))
  (display ", remainder ")
  (display (remainder a b))
  (display ", modulo ")
  (display (modulo a b))
  (newline))

(show-division 17 5)
(show-division -17 5)
(show-division 17 -5)
(show-division -17 -5)

(display (floor-quotient -17 5))
(display " ")
(display (floor-remainder -17 5))
(newline)
(display (truncate-quotient -17 5))
(display " ")
(display (truncate-remainder -17 5))
(newline)

For positive operands the three procedures agree with school arithmetic. With -17 and 5, quotient gives -3 and remainder gives -2, since -3 × 5 − 2 = −17, while modulo gives 3. R7RS adds explicit names for the two conventions: floor-quotient and floor-remainder round the quotient down and correspond to modulo, while truncate-quotient and truncate-remainder correspond to quotient and remainder. Guile provides them without any import.

$ guile division.scm
17 and 5: quotient 3, remainder 2, modulo 2
-17 and 5: quotient -3, remainder -2, modulo 3
17 and -5: quotient -3, remainder 2, modulo -3
-17 and -5: quotient 3, remainder -2, modulo -2
-4 3
-3 -2

Rounding

floor, ceiling, truncate, and round turn a number into an integer value. They keep the exactness of their argument, so rounding 2.7 gives the inexact 3.0, not the exact integer 3. round rounds a value exactly halfway between two integers to the even one.

rounding.scm
(define (show-rounding x)
  (display x)
  (display ": floor ")
  (display (floor x))
  (display ", ceiling ")
  (display (ceiling x))
  (display ", truncate ")
  (display (truncate x))
  (display ", round ")
  (display (round x))
  (newline))

(show-rounding 2.7)
(show-rounding -2.7)
(show-rounding 2.5)
(show-rounding 3.5)
(show-rounding 7/2)

(display (inexact->exact (round 2.7)))
(newline)

floor rounds down and ceiling up, so for -2.7 they give -3.0 and -2.0. truncate drops the fractional part. 2.5 rounds to 2.0 and 3.5 to 4.0, because ties go to the even integer; this avoids a bias when many values are rounded. The exact 7/2 gives exact results. To obtain an exact integer from an inexact value, the result is passed to inexact->exact, as on the last line.

$ guile rounding.scm
2.7: floor 2.0, ceiling 3.0, truncate 2.0, round 3.0
-2.7: floor -3.0, ceiling -2.0, truncate -2.0, round -3.0
2.5: floor 2.0, ceiling 3.0, truncate 2.0, round 2.0
3.5: floor 3.0, ceiling 4.0, truncate 3.0, round 4.0
7/2: floor 3, ceiling 4, truncate 3, round 4
3

Mathematical functions

The usual functions are built in: sqrt, expt, exp, log, and the trigonometric functions. Where the mathematically correct result is exact, Guile returns an exact number, and where the result is not a real number, it returns a complex one.

mathfn.scm
(display (sqrt 16))
(newline)
(display (sqrt 2))
(newline)
(display (sqrt 1/4))
(newline)
(display (sqrt -4))
(newline)
(display (expt 2 10))
(display " ")
(display (expt 2 -2))
(display " ")
(display (expt 2.0 0.5))
(newline)
(display (exp 1))
(newline)
(display (log 100))
(display " ")
(display (/ (log 100) (log 10)))
(newline)
(display (* 4 (atan 1)))
(newline)
(display (sin 0))
(display " ")
(display (cos 0))
(newline)

The square root of 16 is the exact 4, and the square root of 1/4 is the exact 1/2, while the square root of 2 is inexact. (sqrt -4) is the complex number 0.0+2.0i. expt raises a number to a power; a negative exponent gives an exact fraction, and an inexact base gives an inexact result. log is the natural logarithm, so a base-10 logarithm is computed as the quotient of two logarithms. Four times (atan 1) is π. (sin 0) and (cos 0) return the exact 0 and 1, because Guile treats an exact zero argument specially.

$ guile mathfn.scm
4
1.4142135623730951
1/2
0.0+2.0i
1024 1/4 1.4142135623730951
2.718281828459045
4.605170185988092 2.0
3.141592653589793
0 1

Comparing and testing numbers

A group of small procedures covers everyday tasks: abs, min, max, the greatest common divisor gcd, the least common multiple lcm, and the predicates zero?, positive?, negative?, odd?, and even?.

misc.scm
(display (abs -42))
(display " ")
(display (min 3 1 2))
(display " ")
(display (max 3 1 2))
(display " ")
(display (max 1 2.0))
(newline)
(display (gcd 48 36))
(display " ")
(display (lcm 4 6))
(newline)
(display (list (zero? 0) (positive? -3) (negative? -3)))
(newline)
(display (list (odd? 7) (even? 7) (exact-integer? 7.0)))
(newline)

min and max accept any number of arguments. When an inexact number takes part, the result is inexact, so (max 1 2.0) is 2.0. The predicates are collected into lists with list to keep the output short. exact-integer? is false for 7.0: the value is an integer, but not an exact one.

$ guile misc.scm
42 1 3 2.0
12 12
(#t #f #t)
(#t #f #f)

Number literals and conversions

A number literal can carry a prefix: #x for hexadecimal, #b for binary, #o for octal, and #e or #i to force an exact or inexact value. number->string and string->number convert between numbers and text, with an optional radix.

radix.scm
(display #xff)
(display " ")
(display #b1010)
(display " ")
(display #o17)
(display " ")
(display #e1.25)
(display " ")
(display #i1/8)
(newline)
(display (number->string 255 16))
(display " ")
(display (number->string 10 2))
(display " ")
(display (number->string 3.5))
(newline)
(display (+ 1 (string->number "41")))
(display " ")
(display (string->number "ff" 16))
(display " ")
(display (string->number "1/3"))
(display " ")
(display (string->number "forty-two"))
(newline)
(display 1e3)
(display " ")
(display 1.5e-4)
(display " ")
(display 123456789012345678901234.0)
(newline)

#e1.25 reads the decimal as the exact fraction 5/4, and #i1/8 reads the fraction as the inexact 0.125. number->string writes 255 as ff in base 16 and 10 as 1010 in base 2. string->number parses text in the same notations and returns #f when the text is not a number, which makes it safe for checking input. Exponent literals such as 1e3 are inexact, and very large or very small inexact values are printed with an exponent.

$ guile radix.scm
255 10 15 5/4 0.125
ff 1010 3.5
42 255 1/3 #f
1000.0 1.5e-4 1.2345678901234569e23

R7RS names in Guile

R7RS renamed the conversions to exact and inexact and added procedures such as square. In Guile 3.0.7 these names are not available in a plain script, which starts in the guile-user module with Guile's own bindings. The next program calls exact without importing it.

notexact.scm
(display (exact->inexact 1/4))
(newline)
(display (exact 2.5))
(newline)

The compiler warns that exact may be unbound, the first line runs, and the call to exact fails. The older names exact->inexact and inexact->exact, which this article uses, work everywhere.

$ guile notexact.scm
;;; notexact.scm:3:10: warning: possibly unbound variable `exact'
0.25
notexact.scm:3:10: Unbound variable: exact

Importing (scheme base)

The R7RS names live in the standard library (scheme base). An import form at the top of the script makes them available. Modules and libraries are covered in their own article later in the series.

r7names.scm
(import (scheme base))

(display (exact 2.5))
(display " ")
(display (exact 0.25))
(newline)
(display (inexact 1/8))
(newline)
(display (square 12))
(display " ")
(display (square 1/3))
(newline)
(display (exact (round 2.7)))
(newline)

After the import, exact turns 2.5 into 5/2 and 0.25 into 1/4, inexact turns 1/8 into 0.125, and square works on integers and fractions alike. Combining exact with round is the usual way to get an exact integer from an inexact value.

$ guile r7names.scm
5/2 1/4
0.125
144 1/9
3

Source

Numbers — GNU Guile Reference Manual

R7RS: the Revised7 Report on Scheme

In this article we have worked with unlimited integers and exact rationals, converted between exact and inexact numbers, divided integers with three conventions for the remainder, rounded values, used the mathematical functions, converted numbers to and from text, and imported the R7RS names. The data types article places numbers among the other types, the variables and binding article shows how to name the results, and the booleans and equality article compares numbers with = and eqv?.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Scheme tutorials.