Previous Page Next Page Contents

float -- convert to a floating point number

Introduction

float(object) converts the object or numerical subexpressions of the object to floating point numbers.

Call(s)

float(object)

Parameters

object - any MuPAD object

Returns

a floating point number of type DOM_FLOAT or DOM_COMPLEX, or the input object with exact numbers replaced by floating point numbers.

Overloadable:

object

Side Effects

The function is sensitive to the environment variable DIGITS which determines the numerical working precision.

Related Functions

DIGITS, Pref::floatFormat, Pref::trailingZeroes

Details

Example 1

We 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 2

We 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 3

float 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 4

We 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:

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000