4.3.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 | Equality
4.2 | Booleans
4.3 | Numbers
4.4 | Strings
4.5 | Byte Strings
4.6 | Characters
4.7 | Symbols
4.8 | Regular Expressions
4.9 | Keywords
4.10 | Pairs and Lists
4.11 | Mutable Pairs and Lists
4.12 | Vectors
4.13 | Stencil Vectors
4.14 | Boxes
4.15 | Hash Tables
4.16 | Treelists
4.17 | Sequences and Streams
4.18 | Dictionaries
4.19 | Sets
4.20 | Procedures
4.21 | Void
4.22 | Undefined

▼ | 4.3 | Numbers

4.3.1 | Number Types
4.3.2 | Generic Numerics
4.3.3 | Flonums
4.3.4 | Fixnums
4.3.5 | Extflonums

► | 4.3.4 | Fixnums

4.3.4.1 | Fixnum Arithmetic
4.3.4.2 | Fixnum Vectors
4.3.4.3 | Fixnum Range

On this page:

4.3.4.1 Fixnum Arithmetic
fx+
fx-
fx*
fxquotient
fxremainder
fxmodulo
fxabs
fxand
fxior
fxxor
fxnot
fxlshift
fxrshift
fxpopcount
fxpopcount32
fxpopcount16
fx+ / wraparound
fx-/ wraparound
fx*/ wraparound
fxlshift/ wraparound
fxrshift/ logical
fx=
fx<
fx>
fx<=
fx>=
fxmin
fxmax
fx->fl
fl->fx
fixnum-for-every-system?
4.3.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
4.3.4.3 Fixnum Range
most-positive-fixnum
most-negative-fixnum

Racket
top contents ← prev up next →

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

> > > See the documentation of filtered-in for use with #lang racket/base.

> (require (filtered-in
> (λ (name)
> (and (regexp-match #rx"^unsafe-fx" 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.3.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?

Like bitwise-and, bitwise-ior, bitwise-xor, bitwise-not, and arithmetic-shift, but constrained to consume fixnums; the result is always a fixnum. The unsafe-fxlshift and unsafe-fxrshift operations correspond to arithmetic-shift, but require non-negative arguments; unsafe-fxlshift is a positive (i.e., left) shift, and unsafe-fxrshift is a negative (i.e., right) shift, where the number of bits to shift must be no more than the number of bits used to represent a fixnum. 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 (fxpopcount a) → fixnum?
> a : (and/c fixnum? (not/c negative?))
> procedure (fxpopcount32 a) → fixnum?
> a : (and/c fixnum? (integer-in 0 #xFFFFFFFF))
> procedure (fxpopcount16 a) → fixnum?
> a : (and/c fixnum? (integer-in 0 #xFFFF))

Counts the number of bits in the two’s complement representation of a. Depending on the platform, the fxpopcount32 and fxpopcount16 operations can be faster when the result is known to be no more than 32 or 16, respectively.

Added in version 8.5.0.7 of package base.

> procedure (fx+/wraparound a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fx-/wraparound [a] b) → fixnum?
> a : fixnum? = 0
> b : fixnum?
> procedure (fx*/wraparound a b) → fixnum?
> a : fixnum?
> b : fixnum?
> procedure (fxlshift/wraparound a b) → fixnum?
> a : fixnum?
> b : fixnum?

Like fx+, fx-, fx*, and fxlshift, but a fixnum result is produced for any allowed arguments (i.e., for any fixnum argument, except that the second fxlshift/wraparound argument must be between 0 and the number of bits in a fixnum, inclusive). The result is produced by simply discarding bits that do not fit in a fixnum representation. The result is negative if the highest of the retained bits is set—even, for example, if the value was produced by adding two positive fixnums.

Added in version 7.9.0.6 of package base.
Changed in version 8.15.0.12: Changed fx-/wraparound to accept a single argument.

> procedure (fxrshift/logical a b) → fixnum?
> a : fixnum?
> b : fixnum?

Shifts the bits in a to the right by b, filling in with zeros. With the sign bit treated as just another bit, a logical right-shift of a negative-signed fixnum can produce a large positive fixnum. For example, (fxrshift/logical -1 1) produces (most-positive-fixnum), illustrating that logical right-shift results are platform-dependent.

Examples:

> > (fxrshift/logical 128 2)
> 32
> > (fxrshift/logical 255 4)
> 15
> > (= (fxrshift/logical -1 1) (most-positive-fixnum))
> #t

Added in version 8.8.0.5 of package base.

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

Like =, <, >, <=, >=, min, and max, but constrained to consume fixnums.

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 fl) → fixnum?
> fl : flonum?

Conversion between fixnums and flonums with truncation in the case of converting a flonum to a fixnum.

The fx->fl function is the same as exact->inexact or ->fl constrained to a fixnum argument.

The fl->fx function is the same as truncate followed by inexact->exact or fl->exact-integer constrained to returning a fixnum. If the truncated flonum does not fit into a fixnum, the exn:fail:contract exception is raised.

Changed in version 7.7.0.8 of package base: Changed fl->fx to truncate.

> procedure (fixnum-for-every-system? v) → boolean?
> v : any/c

Returns #t if v is a fixnum and is represented by fixnum by every Racket implementation, #f otherwise.

Added in version 7.3.0.11 of package base.

##### 4.3.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)

##### 4.3.4.3 Fixnum Range🔗ℹ

> procedure (most-positive-fixnum) → fixnum?
> procedure (most-negative-fixnum) → fixnum?

Returns the largest-magnitude positive and negative fixnums. The values of (most-positive-fixnum) and (most-negative-fixnum) depend on the platform and virtual machine, but all fixnums are in the range (most-negative-fixnum) to (most-positive-fixnum) inclusive, and all exact integers in that range are fixnums.

Added in version 8.1.0.7 of package base.

top contents ← prev up next →