4.2.3 Flonums

► |  | 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.3 | Flonums

4.2.3.1 | Flonum Arithmetic
4.2.3.2 | Flonum Vectors

On this page:

4.2.3.1`` Flonum Arithmetic
fl+
fl-
fl*
fl/
flabs
fl=
fl<
fl>
fl<=
fl>=
flmin
flmax
flround
flfloor
flceiling
fltruncate
flsin
flcos
fltan
flasin
flacos
flatan
fllog
flexp
flsqrt
flexpt
->fl
fl->exact-integer
make-flrectangular
flreal-part
flimag-part
flrandom
4.2.3.2`` Flonum Vectors
flvector?
flvector
make-flvector
flvector-length
flvector-ref
flvector-set!
flvector-copy
in-flvector
for/ flvector
for*/ flvector
shared-flvector
make-shared-flvector

top ← prev up next →

##### 4.2.3`` Flonums

(require racket/flonum) | package: base

The racket/flonum library provides operations like fl+ that consume and produce only flonums. Flonum-specific operations can provide better performance when used consistently, and they are as safe as generic operations like +.

> > > See also Fixnum and Flonum Optimizations in The Racket Guide.

##### 4.2.3.1`` Flonum Arithmetic

> procedure (fl+ a ...) → flonum?
> a : flonum?
> procedure (fl- a b ...) → flonum?
> a : flonum?
> b : flonum?
> procedure (fl* a ...) → flonum?
> a : flonum?
> procedure (fl/ a b ...) → flonum?
> a : flonum?
> b : flonum?
> procedure (flabs a) → flonum?
> a : flonum?

Like +, -, *, /, and abs, but constrained to consume flonums. The result is always a flonum.

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

> procedure (fl= a b ...) → boolean?
> a : flonum?
> b : flonum?
> procedure (fl< a b ...) → boolean?
> a : flonum?
> b : flonum?
> procedure (fl> a b ...) → boolean?
> a : flonum?
> b : flonum?
> procedure (fl<= a b ...) → boolean?
> a : flonum?
> b : flonum?
> procedure (fl>= a b ...) → boolean?
> a : flonum?
> b : flonum?
> procedure (flmin a b ...) → flonum?
> a : flonum?
> b : flonum?
> procedure (flmax a b ...) → flonum?
> a : flonum?
> b : flonum?

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

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

> procedure (flround a) → flonum?
> a : flonum?
> procedure (flfloor a) → flonum?
> a : flonum?
> procedure (flceiling a) → flonum?
> a : flonum?
> procedure (fltruncate a) → flonum?
> a : flonum?

Like round, floor, ceiling, and truncate, but constrained to consume flonums.

> procedure (flsin a) → flonum?
> a : flonum?
> procedure (flcos a) → flonum?
> a : flonum?
> procedure (fltan a) → flonum?
> a : flonum?
> procedure (flasin a) → flonum?
> a : flonum?
> procedure (flacos a) → flonum?
> a : flonum?
> procedure (flatan a) → flonum?
> a : flonum?
> procedure (fllog a) → flonum?
> a : flonum?
> procedure (flexp a) → flonum?
> a : flonum?
> procedure (flsqrt a) → flonum?
> a : flonum?

Like sin, cos, tan, asin, acos, atan, log, exp, and sqrt, but constrained to consume and produce flonums. The result is +nan.0 when a number outside the range -1.0 to 1.0 is given to flasin or flacos, or when a negative number is given to fllog or flsqrt.

> procedure (flexpt a b) → flonum?
> a : flonum?
> b : flonum?

Like expt, but constrained to consume and produce flonums.

Due to the result constraint, the results compared to expt differ in the following cases: These special cases correspond to pow in C99 [C99].

- (flexpt -1.0 +inf.0) — 1.0

- (flexpt a +inf.0) where a is negative — (expt (abs a) +inf.0)

- (flexpt a -inf.0) where a is negative — (expt (abs a) -inf.0)

- (expt -inf.0 b) where b is a non-integer:

  - b is negative — 0.0

  - b is positive — +inf.0

- (flexpt a b) where a is negative and b is not an integer — +nan.0

> procedure (->fl a) → flonum?
> a : exact-integer?

Like exact->inexact, but constrained to consume exact integers, so the result is always a flonum.

> procedure (fl->exact-integer a) → exact-integer?
> a : flonum?

Like inexact->exact, but constrained to consume an integer flonum, so the result is always an exact integer.

> procedure (make-flrectangular a b)
> | → |  |
> (and/c complex?
> (lambda (c) (flonum? (real-part c)))
> (lambda (c) (flonum? (imag-part c))))
> a : flonum?
> b : flonum?
> procedure (flreal-part a) → flonum?
> | a |  | : |  |
> (and/c complex?
> (lambda (c) (flonum? (real-part c)))
> (lambda (c) (flonum? (imag-part c))))
> procedure (flimag-part a) → flonum?
> | a |  | : |  |
> (and/c complex?
> (lambda (c) (flonum? (real-part c)))
> (lambda (c) (flonum? (imag-part c))))

Like make-rectangular, real-part, and imag-part, but both parts of the complex number must be inexact.

> procedure (flrandom rand-gen) → (and flonum? (>/c 0) (</c 1))
> rand-gen : pseudo-random-generator?

Equivalent to (random rand-gen).

##### 4.2.3.2`` Flonum Vectors

A flvector is like a vector, but it holds only inexact real numbers. This representation can be more compact, and unsafe operations on flvectors (see racket/unsafe/ops) can execute more efficiently than unsafe operations on vectors of inexact reals.

An f64vector as provided by ffi/vector stores the same kinds of values as a flvector, but with extra indirections that make f64vectors more convenient for working with foreign libraries. The lack of indirections makes unsafe flvector access more efficient.

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

A printed flvector starts with #fl(, optionally with a number between the #fl and (. See Reading Vectors for information on reading flvectors and Printing Vectors for information on printing flvectors.

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

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

> procedure (flvector x ...) → flvector?
> x : flonum?

Creates a flvector containing the given inexact real numbers.

Example:

> > (flvector 2.0 3.0 4.0 5.0)
> (flvector 2.0 3.0 4.0 5.0)

> procedure (make-flvector size [x]) → flvector?
> size : exact-nonnegative-integer?
> x : flonum? = 0.0

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

Example:

> > (make-flvector 4 3.0)
> (flvector 3.0 3.0 3.0 3.0)

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

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

> procedure (flvector-ref vec pos) → flonum?
> vec : flvector?
> pos : exact-nonnegative-integer?

Returns the inexact real number in slot pos of vec. The first slot is position 0, and the last slot is one less than (flvector-length vec).

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

Sets the inexact real number in slot pos of vec. The first slot is position 0, and the last slot is one less than (flvector-length vec).

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

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

> procedure (in-flvector vec [start stop step]) → sequence?
> vec : flvector?
> 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.

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

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

Like for/vector or for*/vector, but for flvectors. The default fill-expr produces 0.0.

> procedure (shared-flvector x ...) → flvector?
> x : flonum?

Creates a flvector containing the given inexact real numbers. For communication among places, the new flvector is allocated in the shared memory space.

Example:

> > (shared-flvector 2.0 3.0 4.0 5.0)
> (flvector 2.0 3.0 4.0 5.0)

> procedure (make-shared-flvector size [x]) → flvector?
> size : exact-nonnegative-integer?
> x : flonum? = 0.0

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

Example:

> > (make-shared-flvector 4 3.0)
> (flvector 3.0 3.0 3.0 3.0)

top ← prev up next →