Detailed Information - Maple Help

Home : Support : Online Help : Mathematics : Evaluation : evalf : Detailed Information

evalf

evaluate using floating-point arithmetic

 Calling Sequence evalf(expression) evalf[n](expression)

Parameters

 expression - expression to be evaluated n - (optional) posint; specifies number of digits

Basic Information

 • This help page contains complete information about the evalf command. For basic information on the evalf command, see the evalf help page.

Description

 • The evalf command numerically evaluates expressions (or subexpressions) involving constants (for example, Pi, exp(1), and gamma) and mathematical functions (for example, exp, ln, sin, arctan, cosh, GAMMA, and erf.
 To perform numerical integration, use the evalf command in conjunction with the Int or int command. For more information, see the evalf/Int help page.
 To perform numerical summation, use the evalf command in conjunction with the Sum or sum command. For more information, see the evalf/Sum help page.

Output

 • The evalf command returns a floating-point or complex floating-point number or expression.

Numeric Precision

 • You can control the precision of all numeric computations using the environment variable Digits. By default, Digits is assigned the value 10, so the evalf command uses 10-digit floating-point arithmetic.
 You can assign any positive integer that does not exceed the kernelopts(maxdigits) value to Digits. For more information, see kernelopts.
 • To specify the numeric precision for an evalf computation without changing the value of Digits, use the optional index in the calling sequence, that is, evalf[n](expression).

Constants and Mathematical Functions

 • You can extend the evalf command interface to evaluate new constants and functions.
 To define a new constant K, you must define a procedure evalf/constant/K. Then the calling sequence evalf(K) and more general evalf(expression) calling sequences, in which expression contains K, invoke evalf/constant/K().
 If you define a procedure evalf/f, the calling sequence evalf(f(arguments)) and more general evalf(expression) calling sequences, in which expression contains f(arguments), invoke evalf/f(arguments).
 Note: To call evalf/f(arguments) if floating-point arguments are specified (without requiring the use of unevaluation (right) quotes), use type checking. For an example, see the Examples section.
 • Most initially known mathematics functions automatically invoke the corresponding evalf subfunction if they are passed a floating-point argument.

Examples

 > $\mathrm{evalf}\left(\mathrm{Pi}\right)$
 ${3.141592654}$ (1)
 > ${\mathrm{evalf}}_{15}\left(\frac{5{ⅇ}^{-2+3I}\mathrm{sin}\left(\frac{\mathrm{Pi}}{4}\right)}{3}\right)$
 ${-0.157898022493763}{+}{0.0225078172647505}{}{I}$ (2)
 > $\mathrm{evalf}\left(\mathrm{cos}\left(1\right)+\mathrm{sin}\left(1\right)I\right)$
 ${0.5403023059}{+}{0.8414709848}{}{I}$ (3)
 > $\mathrm{evalf}\left(\frac{3{x}^{2}}{4}+\frac{1x}{3}-\sqrt{2}\right)$
 ${0.7500000000}{}{{x}}^{{2}}{+}{0.3333333333}{}{x}{-}{1.414213562}$ (4)

Numeric Integration

 To compute a numeric approximation to an unevaluated integral returned by the Int or int command, use the evalf command.
 > ${∫}_{0}^{1}{ⅇ}^{{x}^{3}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}ⅆx$
 ${{\int }}_{{0}}^{{1}}{{ⅇ}}^{{{x}}^{{3}}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{x}$ (5)
 > $\mathrm{evalf}\left(\right)$
 ${1.341904418}$ (6)
 > $\mathrm{evalf}\left({{∫}}_{0}^{\frac{\mathrm{Pi}}{4}}\mathrm{tan}\left(x\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}x\right)$
 ${0.3465735903}$ (7)

Numeric Summation

 To compute a numeric approximation to an unevaluated sum returned by the Sum or sum command, use the evalf command.
 > $\sum _{x=\mathrm{RootOf}\left({\mathrm{_Z}}^{5}+{\mathrm{_Z}}^{2}+1\right)}\phantom{\rule[-0.0ex]{5.0px}{0.0ex}}{ⅇ}^{x}$
 ${\sum }_{{x}{=}{\mathrm{RootOf}}{}\left({{\mathrm{_Z}}}^{{5}}{+}{{\mathrm{_Z}}}^{{2}}{+}{1}\right)}{}{{ⅇ}}^{{x}}$ (8)
 > $\mathrm{evalf}\left(\right)$
 ${4.462691252}{+}{0.}{}{I}$ (9)
 > $\mathrm{evalf}\left({\sum }_{x=1}^{1000}\phantom{\rule[-0.0ex]{5.0px}{0.0ex}}\frac{1}{\sqrt{x}}\right)$
 ${61.80100877}$ (10)

When not to use evalf

 There are times when using the evalf command causes problems mathematically. For example, the rounding error in floating-point values can give incorrect solutions to further computations. The following example does not work if you evaluate to a floating-point value too early in the computation.
 > $\underset{n→\mathrm{∞}}{lim}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}n\left(\frac{1}{3}-\frac{1}{3+\frac{1}{n}}\right)$
 $\frac{{1}}{{9}}$ (11)
 > $\underset{n→\mathrm{∞}}{lim}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}n\left(\mathrm{evalf}\left(\frac{1}{3}\right)-\frac{1}{3+\frac{1}{n}}\right)$
 ${Float}{}\left({-}{\mathrm{\infty }}\right)$ (12)

Adding a New Function and evalf Subroutine

 When you specify floating-point arguments in a call to the sin function, it automatically returns a floating-point result.
 > $\mathrm{sin}\left(3.5+4.5I\right)$
 ${-15.79019836}{-}{42.14337074}{}{I}$ (13)
 You can define a new function and corresponding evalf subroutine such that the evalf subroutine is called if the arguments are of floating-point type. To clearly show which code Maple uses, in the following example, the mathematical function returns 2 times the argument for non-floating-point input and 2.01 times the argument for floating-point input.
 > NewFunction := proc(x)   local s;   if type(x,'complex'('float')) then     s := evalf('NewFunction'(x));     if type(s,'complex'('float')) then       return s;     end if;   end if;   return 2*x; end proc:
 > evalf/NewFunction := proc(xx)   local x;   x := evalf(xx);   if not type(x, 'complex'('float')) then     return 'NewFunction'(x);   end if;   return 2.01*x; end proc:
 You can always use the evalf subroutine by directly calling it or placing unevaluation quotes around instances of the function name.
 > $\mathrm{evalf}\left(\mathrm{NewFunction}\left(\mathrm{Pi}\right)\right)$
 ${6.283185308}$ (14)
 > $\mathrm{evalf/NewFunction}\left(\mathrm{Pi}\right)$
 ${6.314601235}$ (15)
 > $\mathrm{evalf}\left('\mathrm{NewFunction}'\left(\mathrm{Pi}\right)\right)$
 ${6.314601235}$ (16)
 > $\mathrm{NewFunction}\left(3\right)$
 ${6}$ (17)
 > $\mathrm{NewFunction}\left(3.0\right)$
 ${6.030}$ (18)