3.2.4 Fixnums

► |  | The Racket Reference

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

► | 3 | Datatypes

3.1 | Booleans and Equality
3.2 | Numbers
3.3 | Strings
3.4 | Byte Strings
3.5 | Characters
3.6 | Symbols
3.7 | Regular Expressions
3.8 | Keywords
3.9 | Pairs and Lists
3.10 | Mutable Pairs and Lists
3.11 | Vectors
3.12 | Boxes
3.13 | Hash Tables
3.14 | Sequences and Streams
3.15 | Dictionaries
3.16 | Sets
3.17 | Procedures
3.18 | Void and Undefined

▼ | 3.2 | Numbers

3.2.1 | Number Types
3.2.2 | Generic Numerics
3.2.3 | Flonums
3.2.4 | Fixnums
3.2.5 | Extflonums

► | 3.2.4 | Fixnums

3.2.4.1 | Fixnum Arithmetic
3.2.4.2 | Fixnum Vectors

On this page:

3.2.4.1`` Fixnum Arithmetic
fx+
fx-
fx*
fxquotient
fxremainder
fxmodulo
fxabs
fxand
fxior
fxxor
fxnot
fxlshift
fxrshift
fx=
fx<
fx>
fx<=
fx>=
fxmin
fxmax
fx->fl
fl->fx
3.2.4.2`` Fixnum Vectors
fxvector?
fxvector
make-fxvector
fxvector-length
fxvector-ref
fxvector-set!
fxvector-copy
in-fxvector
for/ fxvector
for*/ fxvector
shared-fxvector
make-shared-fxvector

top ← prev up next →

##### 3.2.4`` Fixnums

(require racket/fixnum)

The racket/fixnum library provides operations like fx+ that consume and produce only fixnums. The operations in this library are meant to be safe versions of unsafe operations like unsafe-fx+. These safe operations are generally no faster than using generic primitives like +.

The expected use of the racket/fixnum library is for code where the require of racket/fixnum is replaced with

> (require (filtered-in
> (λ (name) (regexp-replace #rx"unsafe-" name ""))
> racket/unsafe/ops))

to drop in unsafe versions of the library. Alternately, when encountering crashes with code that uses unsafe fixnum operations, use the racket/fixnum library to help debug the problems.

##### 3.2.4.1`` Fixnum Arithmetic

> procedure (fx+ a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fx- a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fx* a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fxquotient a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fxremainder a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fxmodulo a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fxabs a) → fixnum?
> a : fixnum?

Safe versions of unsafe-fx+, unsafe-fx-, unsafe-fx*, unsafe-fxquotient, unsafe-fxremainder, unsafe-fxmodulo, and unsafe-fxabs. The exn:fail:contract:non-fixnum-result exception is raised if the arithmetic result would not be a fixnum.

> procedure (fxand a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fxior a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fxxor a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fxnot a) → fixnum?
> a : fixnum?
> procedure (fxlshift a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fxrshift a b) → fixnum?
> a : fixnum?
> b : fixnum?

Safe versions of unsafe-fxand, unsafe-fxior, unsafe-fxxor, unsafe-fxnot, unsafe-fxlshift, and unsafe-fxrshift. The exn:fail:contract:non-fixnum-result exception is raised if the arithmetic result would not be a fixnum.

> procedure (fx= a b) → boolean?
> a : fixnum?
> b : fixnum?
> procedure (fx< a b) → boolean?
> a : fixnum?
> b : fixnum?
> procedure (fx> a b) → boolean?
> a : fixnum?
> b : fixnum?
> procedure (fx<= a b) → boolean?
> a : fixnum?
> b : fixnum?
> procedure (fx>= a b) → boolean?
> a : fixnum?
> b : fixnum?
> procedure (fxmin a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fxmax a b) → fixnum?
> a : fixnum?
> b : fixnum?

Safe versions of unsafe-fx=, unsafe-fx<, unsafe-fx>, unsafe-fx<=, unsafe-fx>=, unsafe-fxmin, and unsafe-fxmax.

> procedure (fx->fl a) → flonum?
> a : fixnum?
> procedure (fl->fx a) → fixnum?
> a : flonum?

Safe versions of unsafe-fx->fl and unsafe-fl->fx.

##### 3.2.4.2`` Fixnum Vectors

A fxvector is like a vector, but it holds only fixnums. The only advantage of a fxvector over a vector is that a shared version can be created with functions like shared-fxvector.

Two fxvectors are equal? if they have the same length, and if the values in corresponding slots of the fxvectors are equal?.

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

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

> procedure (fxvector x ...) → fxvector?
> x : fixnum?

Creates a fxvector containing the given fixnums.

Example:
> > (fxvector 2 3 4 5)
> (fxvector 2 3 4 5)

> procedure (make-fxvector size [x]) → fxvector?
> size : exact-nonnegative-integer?
> x : fixnum? = 0

Creates a fxvector with size elements, where every slot in the fxvector is filled with x.

Example:
> > (make-fxvector 4 3)
> (fxvector 3 3 3 3)

> procedure (fxvector-length vec) → exact-nonnegative-integer?
> vec : fxvector?

Returns the length of vec (i.e., the number of slots in the fxvector).

> procedure (fxvector-ref vec pos) → fixnum?
> vec : fxvector?
> pos : exact-nonnegative-integer?

Returns the fixnum in slot pos of vec. The first slot is position 0, and the last slot is one less than (fxvector-length vec).

> procedure (fxvector-set! vec pos x) → fixnum?
> vec : fxvector?
> pos : exact-nonnegative-integer?
> x : fixnum?

Sets the fixnum in slot pos of vec. The first slot is position 0, and the last slot is one less than (fxvector-length vec).

> procedure (fxvector-copy vec [start end]) → fxvector?
> vec : fxvector?
> start : exact-nonnegative-integer? = 0
> end : exact-nonnegative-integer? = (vector-length v)

Creates a fresh fxvector of size (- end start), with all of the elements of vec from start (inclusive) to end (exclusive).

> procedure (in-fxvector vec [start stop step]) → sequence?
> vec : fxvector?
> start : exact-nonnegative-integer? = 0
> stop : (or/c exact-integer? #f) = #f
> step : (and/c exact-integer? (not/c zero?)) = 1

Returns a sequence equivalent to vec when no optional arguments are supplied.

The optional arguments start, stop, and step are as in in-vector.

An in-fxvector application can provide better performance for fxvector iteration when it appears directly in a for clause.

> syntax (for/fxvector maybe-length (for-clause ...) body ...)
> syntax (for*/fxvector maybe-length (for-clause ...) body ...)
> maybe-length |  | = |  |
> |  | | |  | #:length length-expr
> |  | | |  | #:length length-expr #:fill fill-expr
> > | length-expr |  | : |  | exact-nonnegative-integer?
> > | fill-expr |  | : |  | fixnum?

Like for/vector or for*/vector, but for fxvectors. The default fill-expr produces 0.

> procedure (shared-fxvector x ...) → fxvector?
> x : fixnum?

Creates a fxvector containing the given fixnums. For communication among places, the new fxvector is allocated in the shared memory space.

Example:
> > (shared-fxvector 2 3 4 5)
> (fxvector 2 3 4 5)

> procedure (make-shared-fxvector size [x]) → fxvector?
> size : exact-nonnegative-integer?
> x : fixnum? = 0

Creates a fxvector with size elements, where every slot in the fxvector is filled with x. For communication among places, the new fxvector is allocated in the shared memory space.

Example:
> > (make-shared-fxvector 4 3)
> (fxvector 3 3 3 3)

top ← prev up next →