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}\left[15\right]\left(\frac{5}{3}\mathrm{exp}\left(-2+3I\right)\mathrm{sin}\left(\frac{\mathrm{\pi }}{4}\right)\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}{4}{x}^{2}+\frac{1}{3}x-\mathrm{sqrt}\left(2\right)\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.
 > $\mathrm{int}\left(\mathrm{exp}\left({x}^{3}\right),x=0..1\right)$
 ${{\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(\mathrm{Int}\left(\mathrm{tan}\left(x\right),x=0..\frac{\mathrm{\pi }}{4}\right)\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.
 > $\mathrm{sum}\left(\mathrm{exp}\left(x\right),x=\mathrm{RootOf}\left({\mathrm{_Z}}^{5}+{\mathrm{_Z}}^{2}+1\right)\right)$
 ${\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(\mathrm{Sum}\left(\frac{1}{\mathrm{sqrt}\left(x\right)},x=1..1000\right)\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.
 > $\mathrm{limit}\left(n\left(\frac{1}{3}-\frac{1}{3+\frac{1}{n}}\right),n=\mathrm{\infty }\right)$
 $\frac{{1}}{{9}}$ (11)
 > $\mathrm{limit}\left(n\left(\mathrm{evalf}\left(\frac{1}{3}\right)-\frac{1}{3+\frac{1}{n}}\right),n=\mathrm{\infty }\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)