float -- convert to a floating
point number
Introductionfloat(object) converts the object or
numerical subexpressions of the object to floating point numbers.
Call(s)float(object)
Parametersobject |
- | any MuPAD object |
Returnsa floating point number of type DOM_FLOAT or DOM_COMPLEX, or the input object
with exact numbers replaced by floating point numbers.
object
Side
EffectsThe function is sensitive to the environment variable DIGITS which determines the numerical
working precision.
Related
FunctionsDIGITS, Pref::floatFormat, Pref::trailingZeroes
Detailsfloat converts numbers and numerical expressions such as
sqrt(sin(2)) or sqrt(3) + sin(PI/17)*I to
real or complex floating point numbers of type DOM_FLOAT or DOM_COMPLEX, respectively. If
symbolic objects other than the special constants CATALAN, E, EULER, and PI are present, only numerical
subexpressions are converted to floats. In particular, identifiers and
indexed identifiers are returned unchanged by float.
Cf. example 1.float call is mapped recursively to the operands of
an expression. When numbers (or constants such as PI) are found, they are converted to
floating point approximations. The number of significant decimal digits
is given by the environment variable DIGITS; the default value is
10. The converted operands are combined by arithmetical
operations or function calls according to the structure of the
expression. E.g., a call such as float(PI - 314/100) may
be regarded as a sequence of numerical operations:
t1 := float(PI); t2 := float(314/100); result := t1 - t2Consequently, float evaluation via
float may be subject to
error propagation. Cf. example 2.float is automatically mapped to the elements of sets and lists.
However, it is not automatically mapped to the entries of arrays or tables. Use
map(object, float) for a
fast floating point conversion of all entries of an array or a table.
Use mapcoeffs(p,
float) to convert the coefficients of a polynomial p of type DOM_POLY. Cf. example 3.Pref::floatFormat and Pref::trailingZeroes can
be used to modify the screen output of floating point numbers.numeric::rationalize.sin, exp, besselJ etc. are implemented as function environments.
Via overloading, the "float"
attribute (slot) of a function environment
f, say, is called for the float evaluation of symbolic
calls f(x1, x2, ...) contained in an expression.
The user may extend the functionality of the system function
float to his own functions. For this, the function
f to be processed must be declared as a function
environment via funcenv. A "float"
attribute must be written, which is called by the system function
float in the form f::float(x1, x2, ...)
whenever a symbolic call f(x1, x2, ...) inside an
expression is found. The arguments passed to f::float are
not converted to floats, neither is the return value of the slot
subject to any further float evaluation. Thus, the float conversion of
symbolic functions calls of f is entirely determined by
the slot routine. Cf. example 4.
d, say,
written in the MuPAD language, can overload float
to define the float evaluation of its elements. A slot d::float must be implemented. If an
element x, say, of this domain is subject to a float
evaluation, the slot is called in the form d::float(x). As
for function environments, neither x nor the return value
of the slot are subject to any further float evaluation.
If a domain does not have a "float" slot, the system
function float returns its elements unchanged.
DIGITS for further information.float is a function of the system kernel.
Example
1We convert some numbers and numerical expressions to floats:
>> float(17), float(PI/7 + I/4), float(4^(1/3) + sin(7))
17.0, 0.4487989505 + 0.25 I, 2.244387651
float is sensitive to DIGITS:
>> DIGITS := 20: float(17), float(PI/7 + I/4), float(4^(1/3) + sin(7))
17.0, 0.4487989505128276055 + 0.25 I, 2.2443876506869885652
Symbolic objects such as identifiers are returned unchanged:
>> DIGITS := 10: float(2*x + sin(3))
2.0 x + 0.141120008
Example
2We illustrate error propagation in numerical
computations. The following rational number approximates
exp(2) to 17 decimal digits:
>> r := 738905609893065023/100000000000000000:
The following float call converts
exp(2) and r to floating point
approximations. The approximation errors propagate and are amplified in
the following numerical expression:
>> DIGITS := 10: float(10^20*(r - exp(2)))
320.0
None of the digits in this result is correct! A better
result is obtained by increasing DIGITS:
>> DIGITS := 20: float(10^20*(r - exp(2)))
276.95725394785404205
>> delete r, DIGITS:
Example
3float is mapped to the elements of sets and lists:
>> float([PI, 1/7, [1/4, 2], {sin(1), 7/2}])
[3.141592654, 0.1428571429, [0.25, 2.0], {0.8414709848, 3.5}]
For tables and arrays, the function map must be used to forward
float to the entries:
>> T := table("a" = 4/3, 3 = PI): float(T), map(T, float)
table( table(
3 = PI, , 3 = 3.141592654,
"a" = 4/3 "a" = 1.333333333
) )
>> A := array(1..2, [1/7, PI]): float(A), map(A, float)
+- -+ +- -+
| 1/7, PI |, | 0.1428571429, 3.141592654 |
+- -+ +- -+
Matrix domains overload the function float.
In contrast to arrays, float works directly on a matrix:
>> float(matrix(A))
+- -+
| 0.1428571429 |
| |
| 3.141592654 |
+- -+
Use mapcoeffs to apply
float to the coefficients of a polynomial generated by
poly:
>> p := poly(9/4*x^2 + PI, [x]): float(p), mapcoeffs(p, float)
2 2
poly(9/4 x + PI, [x]), poly(2.25 x + 3.141592654, [x])
>> delete A, T, p:
Example
4We demonstrate overloading of float by a
function environment. The following function Sin is to
represent the sine function. In contrast to MuPAD's sin, it measures its argument in
degrees rather than in radians (i.e., Sin(x) =
sin(PI/180*x)). The only functionality of Sin
is to produce floating point values if the argument is a real float.
For all other kinds of arguments, a symbolic function call is to be
returned:
>> Sin := proc(x)
begin
if domtype(x) = DOM_FLOAT then
return(Sin::float(x));
else return(procname(args()))
end_if;
end_proc:
The function is turned into a function environment via
funcenv:
>> Sin := funcenv(Sin):
Finally, the "float" attribute is
implemented. If the argument can be converted to a real floating point
number, a floating point result is produced. In all other cases, a
symbolic call of Sin is returned:
>> Sin::float := proc(x)
begin x := float(x):
if domtype(x) = DOM_FLOAT then
return(float(sin(PI/180*x)));
else return(Sin(x))
end_if;
end_proc:
Now, float evaluation of arbitrary expressions involving
Sin is possible:
>> Sin(x), Sin(x + 0.3), Sin(120)
Sin(x), Sin(x + 0.3), Sin(120)
>> Sin(120.0), float(Sin(120)), float(Sin(x + 120))
0.8660254038, 0.8660254038, Sin(x + 120.0)
>> float(sqrt(2) + Sin(120 + sqrt(3)))
2.264730594
>> delete Sin: