float
float Function
Syntax:
float number &optional prototype ! float
Arguments and Values:
number—a real.
prototype—a float.
float—a float.
Description:
float converts a real number to a float.
If a prototype is supplied, a float is returned that is mathematically equal to number but has the same format as prototype.
If prototype is not supplied, then if the number is already a float, it is returned; otherwise, a float is returned that is mathematically equal to number but is a single float.
Examples:
(float 0) *!* 0.0
(float 1 .5) *!* 1.0
(float 1.0) *!* 1.0
(float 1/2) *!* 0.5
<i>!</i> 1.0d0 <i><sub>or</sub></i>
*!* 1.0
(eql (float 1.0 1.0d0) 1.0d0) *! true*
See Also:
coerceExpanded Reference: float
Converting integers and ratios to floats
float converts a real number to a single float when no prototype is supplied.
(float 0)
=> 0.0
(float 1)
=> 1.0
(float 1/2)
=> 0.5
(float 1/3)
=> 0.33333334
Float is identity for float inputs
If the argument is already a float and no prototype is given, the same float is returned.
(float 1.0)
=> 1.0
(float 3.14)
=> 3.14
Using a prototype to control float format
The optional second argument specifies the desired float format by example. The result will have the same float format as the prototype.
(float 1 1.0)
=> 1.0
(float 1 1.0d0)
=> 1.0d0
(float 1/3 1.0d0)
=> 0.3333333333333333d0
;; Verify the format matches the prototype
(eql (float 1.0 1.0d0) 1.0d0)
=> T
Converting pi to different precisions
float is useful for converting constants to a specific float format.
(float pi 1.0)
=> 3.1415927
(float pi 1.0d0)
=> 3.141592653589793d0
Practical use: ensuring float arithmetic
When you need floating-point division rather than rational division, convert one operand.
;; Rational division
(/ 1 3)
=> 1/3
;; Floating-point division
(/ (float 1) 3)
=> 0.33333334