Skip to main content

float

float System Class

Class Precedence List:

float, real, number, t

Description:

A float is a mathematical rational (but not a Common Lisp rational) of the form s · f · be−p, where s is +1 or 1, the sign; b is an integer greater than 1, the base or radix of the representation; p is a positive integer , the precision (in base-b digits) of the float; f is a positive integer between bp−1 and bp 1 (inclusive), the significand; and e is an integer , the exponent. The value of p and the

range of e depends on the implementation and on the type of float within that implementation. In addition, there is a floating-point zero; depending on the implementation, there can also be a “minus zero”. If there is no minus zero, then 0▷0 and *−*0▷0 are both interpreted as simply a floating-point zero. (= 0.0 -0.0) is always true. If there is a minus zero, (eql -0.0 0.0) is false, otherwise it is true.

The types short-float, single-float, double-float, and long-float are subtypes of type float. Any two of them must be either disjoint types or the same type; if the same type, then any other types between them in the above ordering must also be the same type. For example, if the type single-float and the type long-float are the same type, then the type double-float must be the same type also.

Compound Type Specifier Kind:

Abbreviating.

Compound Type Specifier Syntax:

(float [lower-limit [upper-limit]])

Compound Type Specifier Arguments:

lower-limit, upper-limitinterval designators for type float. The defaults for each of lower-limit and upper-limit is the symbol *.

Compound Type Specifier Description:

This denotes the floats on the interval described by lower-limit and upper-limit.

See Also:

Figure 2–9, Section 2.3.2 (Constructing Numbers from Tokens), Section 22.1.3.1.3 (Printing Floats)

Notes:

Note that all mathematical integers are representable not only as Common Lisp reals, but also as complex floats. For example, possible representations of the mathematical number 1 include the integer 1, the float 1.0, or the complex #C(1.0 0.0).

Expanded Reference: float

Type Checking

The float type is the supertype of all floating-point number types. Every float is also a real and a number.

(typep 1.0 'float)
=> T
(typep 1.0d0 'float)
=> T
(typep 1 'float)
=> NIL
(typep 1/3 'float)
=> NIL
(floatp 3.14)
=> T

Float Subtypes

The standard defines four float subtypes: short-float, single-float, double-float, and long-float. Implementations may merge some of these types.

(subtypep 'short-float 'float)
=> T
=> T
(subtypep 'single-float 'float)
=> T
=> T
(subtypep 'double-float 'float)
=> T
=> T
(subtypep 'long-float 'float)
=> T
=> T

(type-of 1.0)
=> SINGLE-FLOAT
(type-of 1.0d0)
=> DOUBLE-FLOAT

Type Specifier with Range

The float type specifier accepts optional lower and upper bounds.

(typep 0.5 '(float 0.0 1.0))
=> T
(typep 1.5 '(float 0.0 1.0))
=> NIL
(typep -0.1 '(float 0.0 *))
=> NIL
(typep 3.14 '(float * *))
=> T

Converting to Float

The float function converts a real number to a floating-point number, optionally matching the type of a prototype float.

(float 1)
=> 1.0
(float 1/3)
=> 0.33333334
(float 1 1.0d0)
=> 1.0d0
(float 1/3 1.0d0)
=> 0.3333333333333333d0