4.2.1 Number Types

► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

► | 4 | Datatypes

4.1 | Booleans and Equality
4.2 | Numbers
4.3 | Strings
4.4 | Byte Strings
4.5 | Characters
4.6 | Symbols
4.7 | Regular Expressions
4.8 | Keywords
4.9 | Pairs and Lists
4.10 | Mutable Pairs and Lists
4.11 | Vectors
4.12 | Boxes
4.13 | Hash Tables
4.14 | Sequences and Streams
4.15 | Dictionaries
4.16 | Sets
4.17 | Procedures
4.18 | Void
4.19 | Undefined

▼ | 4.2 | Numbers

4.2.1 | Number Types
4.2.2 | Generic Numerics
4.2.3 | Flonums
4.2.4 | Fixnums
4.2.5 | Extflonums

On this page:

number?
complex?
real?
rational?
integer?
exact-integer?
exact-nonnegative-integer?
exact-positive-integer?
inexact-real?
fixnum?
flonum?
double-flonum?
single-flonum?
zero?
positive?
negative?
even?
odd?
exact?
inexact?
inexact->exact
exact->inexact
real->single-flonum
real->double-flonum

top ← prev up next →

##### 4.2.1`` Number Types

> procedure (number? v) → boolean?
> v : any/c

Returns #t if v is a number, #f otherwise.

Examples:

> > (number? 1)
> #t
> > (number? 2+3i)
> #t
> > (number? "hello")
> #f
> > (number? +nan.0)
> #t

> procedure (complex? v) → boolean?
> v : any/c

Returns (number? v), because all numbers are complex numbers.

> procedure (real? v) → boolean?
> v : any/c

Returns #t if v is a real number, #f otherwise.

Examples:

> > (real? 1)
> #t
> > (real? +inf.0)
> #t
> > (real? 2+3i)
> #f
> > (real? 2.0+0.0i)
> #f
> > (real? "hello")
> #f

> procedure (rational? v) → boolean?
> v : any/c

Returns #t if v is a rational number, #f otherwise.

Examples:

> > (rational? 1)
> #t
> > (rational? +inf.0)
> #f
> > (rational? "hello")
> #f

> procedure (integer? v) → boolean?
> v : any/c

Returns #t if v is a number that is an integer, #f otherwise.

Examples:

> > (integer? 1)
> #t
> > (integer? 2.3)
> #f
> > (integer? 4.0)
> #t
> > (integer? +inf.0)
> #f
> > (integer? 2+3i)
> #f
> > (integer? "hello")
> #f

> procedure (exact-integer? v) → boolean?
> v : any/c

Returns (and (integer? v) (exact? v)).

Examples:

> > (exact-integer? 1)
> #t
> > (exact-integer? 4.0)
> #f

> procedure (exact-nonnegative-integer? v) → boolean?
> v : any/c

Returns (and (exact-integer? v) (not (negative? v))).

Examples:

> > (exact-nonnegative-integer? 0)
> #t
> > (exact-nonnegative-integer? -1)
> #f

> procedure (exact-positive-integer? v) → boolean?
> v : any/c

Returns (and (exact-integer? v) (positive? v)).

Examples:

> > (exact-positive-integer? 1)
> #t
> > (exact-positive-integer? 0)
> #f

> procedure (inexact-real? v) → boolean?
> v : any/c

Returns (and (real? v) (inexact? v)).

> procedure (fixnum? v) → boolean?
> v : any/c

Return #t if v is a fixnum, #f otherwise.

Note: the result of this function is platform-dependent, so using it in syntax transformers can lead to platform-dependent bytecode files.

> procedure (flonum? v) → boolean?
> v : any/c

Return #t if v is a flonum, #f otherwise.

> procedure (double-flonum? v) → boolean?
> v : any/c

Identical to flonum?.

> procedure (single-flonum? v) → boolean?
> v : any/c

Return #t if v is a single-precision floating-point number, #f otherwise.

> procedure (zero? z) → boolean?
> z : number?

Returns (= 0 z).

Examples:

> > (zero? 0)
> #t
> > (zero? -0.0)
> #t

> procedure (positive? x) → boolean?
> x : real?

Returns (> x 0).

Examples:

> > (positive? 10)
> #t
> > (positive? -10)
> #f
> > (positive? 0.0)
> #f

> procedure (negative? x) → boolean?
> x : real?

Returns (< x 0).

Examples:

> > (negative? 10)
> #f
> > (negative? -10)
> #t
> > (negative? -0.0)
> #f

> procedure (even? n) → boolean?
> n : integer?

Returns (zero? (modulo n 2)).

Examples:

> > (even? 10.0)
> #t
> > (even? 11)
> #f
> > (even? +inf.0)
> even?: contract violation
> expected: integer
> given: +inf.0

> procedure (odd? n) → boolean?
> n : integer?

Returns (not (even? n)).

Examples:

> > (odd? 10.0)
> #f
> > (odd? 11)
> #t
> > (odd? +inf.0)
> odd?: contract violation
> expected: integer
> given: +inf.0

> procedure (exact? z) → boolean?
> z : number?

Returns #t if z is an exact number, #f otherwise.

Examples:

> > (exact? 1)
> #t
> > (exact? 1.0)
> #f

> procedure (inexact? z) → boolean?
> z : number?

Returns #t if z is an inexact number, #f otherwise.

Examples:

> > (inexact? 1)
> #f
> > (inexact? 1.0)
> #t

> procedure (inexact->exact z) → exact?
> z : number?

Coerces z to an exact number. If z is already exact, it is returned. If z is +inf.0, -inf.0, +nan.0, +inf.f, -inf.f, or +nan.f, then the exn:fail:contract exception is raised.

Examples:

> > (inexact->exact 1)
> 1
> > (inexact->exact 1.0)
> 1

> procedure (exact->inexact z) → inexact?
> z : number?

Coerces z to an inexact number. If z is already inexact, it is returned.

Examples:

> > (exact->inexact 1)
> 1.0
> > (exact->inexact 1.0)
> 1.0

> procedure (real->single-flonum x) → single-flonum?
> x : real?

Coerces x to a single-precision floating-point number. If x is already a single-precision floating-point number, it is returned.

> procedure (real->double-flonum x) → flonum?
> x : real?

Coerces x to a double-precision floating-point number. If x is already a double-precision floating-point number, it is returned.
top ← prev up next →