Numbers and the Numeric Tower
4.1 The Tower and Exactness
Every number is either exact (integers, rationals) or inexact (floating point). Exactness is preserved by arithmetic: exact operations give exact results.
number ⊇ complex ⊇ real ⊇ rational ⊇ integer(/ 1 3) ; 1/3 (exact)
(/ 1.0 3) ; 0.3333333333333333 (inexact)
(+ 1/3 1/6) ; 1/2
(* 1.0 1/3) ; 0.3333333333333333
(exact? 1/2) ; #t
(inexact? 0.5) ; #t
(exact 2.5) ; 5/2 (R7RS; R5RS: inexact->exact)
(inexact 1/3) ; 0.3333333333333333 (R5RS: exact->inexact)
(expt 2 100) ; 1267650600228229401496703205376 (bignum)
(sqrt 16) ; 4 (exact when possible)
(sqrt 2) ; 1.4142135623730951
(sqrt -4) ; +2i (complex, in implementations with complex support)
(exact-integer-sqrt 17) ; 4 1 (two values: root and remainder)4.2 Arithmetic Procedures
Literal prefixes: #x1F (hex), #b1010 (binary), #o17 (octal), #e1.5 (exact 3/2), #i1/3 (inexact).
(quotient 17 5) ; 3
(remainder 17 5) ; 2
(modulo -7 3) ; 2 (sign of the divisor)
(remainder -7 3) ; -1 (sign of the dividend)
(floor/ 7 2) ; 3 1 (R7RS: two values)
(truncate/ -7 2) ; -3 -1
(floor-quotient -7 2) ; -4
(gcd 12 18) (lcm 4 6) ; 6 12
(abs -5) ; 5
(floor 2.7) (ceiling 2.1) (round 2.5) (truncate -2.7) ; 2.0 3.0 2.0 -2.0 (round: banker's rounding)
(exact (floor 2.7)) ; 2
(numerator 6/4) (denominator 6/4) ; 3 2
(square 5) ; 25 (R7RS)
(exp 1) (log 100) (sin 0) (atan 1 1)
(number->string 255 16) ; "ff"
(string->number "ff" 16) ; 255
(string->number "abc") ; #f
(exact-integer? 5) (nan? (/ 0. 0.)) (infinite? (/ 1. 0)) ; #t #t #t
(zero? 0) (positive? 3) (negative? -1) (odd? 3) (even? 4)
(= 1 1.0) ; #t (numeric equality ignores exactness)
(eqv? 1 1.0) ; #f (different exactness)