4.2.4 Fixnums

► |  | 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

► | 4.2.4 | Fixnums

4.2.4.1 | Fixnum Arithmetic
4.2.4.2 | Fixnum Vectors

On this page:

4.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
4.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 →

##### 4.2.4`` Fixnums

(require racket/fixnum) | package: base

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.

##### 4.2.4.1`` Fixnum Arithmetic

> procedure (fx+ a ...) → fixnum?
> a : fixnum?
> procedure (fx- a b ...) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fx* a ...) → fixnum?
> a : 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.

Changed in version 7.0.0.13 of package base: Allow zero or more arguments for fx+ and fx* and one or more arguments for fx-.

> procedure (fxand a ...) → fixnum?
> a : fixnum?
> procedure (fxior a ...) → fixnum?
> a : fixnum?
> procedure (fxxor a ...) → fixnum?
> a : 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.

Changed in version 7.0.0.13 of package base: Allow any number of arguments for fxand, fxior, and fxxor.

> 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.

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

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

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

##### 4.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?.

A printed fxvector starts with #fx(, optionally with a number between the #fx and (. See Reading Vectors for information on reading fxvectors and Printing Vectors for information on printing fxvectors.

> 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 →