Use exact numbers to keep results such as one third as fractions. Use inexact numbers for measurements and calculations that allow rounding.
Compare the results
Run this complete example in the Playground.
(list (/ 1 3) (+ 1 2.0) (number->string -0.0))Result
(1/3 3.0 "-0.0")(/ 1 3) returns the exact fraction 1/3. Adding exact 1 to inexact 2.0 returns inexact 3.0. The number->string procedure converts a number to text and preserves the minus sign in -0.0.
Choose a number type
| Type | Examples | Representation |
|---|---|---|
| Exact integer | 0, -12, 999999999999 | Arbitrary precision, so integers can grow beyond a fixed machine word |
| Exact rational | 1/3, -5/2 | A fraction reduced to lowest terms, with a positive denominator |
| Inexact real | 2.0, -0.0, 0.125 | A finite 64-bit IEEE-754 floating-point number |
A fraction with denominator 1 becomes an exact integer. For example, (/ 10 5) returns 2.
For +, -, *, and /, exact inputs produce exact results. If any input is inexact, these operations convert all inputs to floating point.
Compare numbers without rounding the exact input
(list (= 2 2.0) (= 1/10 0.1) (min 1 2.0))Result
(#t #f 1.0)The numbers 2 and 2.0 have the same mathematical value. The inexact number 0.1 stores a binary approximation to one tenth, so it differs from exact 1/10.
The numeric comparisons =, <, >, <=, and >= compare the stored values exactly. They do not first round the exact input to floating point.
The min and max procedures also compare exactly before selecting a value. If any input is inexact, they return the selected value as an inexact number. Thus (min 1 2.0) returns 1.0.
Read printed real numbers
Real numbers print as decimals without exponent notation. The output uses the fewest digits needed to read back the same floating-point value.
| Input | Result |
|---|---|
(number->string 0.125) | "0.125" |
(number->string 1e3) | "1000.0" |
(number->string -0.0) | "-0.0" |
A real number with no fractional part prints with .0. Negative zero keeps its sign.
Check numeric errors
Division by zero produces E313. A calculation or conversion that would produce infinity or NaN produces E314.
The numeric types above cover integers, fractions, and finite real numbers. Complex numbers and functions such as sin and log are not supported. For integer powers and integer square roots, use expt and exact-integer-sqrt.
Related pages
Numeric procedures lists arguments and errors. Equality and identity explains when comparisons distinguish exact and inexact values.