This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Transformation Rules and Definitions /

2.5.11 Defining Numerical Values

If you make a definition such as f[x_] := value, Mathematica will use the value you give for any f function it encounters. In some cases, however, you may want to define a value that is to be used specifically when you ask for numerical values.

Defining ordinary and numerical values.

This defines a numerical value for the function f.

In[1]:= N[f[x_]] := Sum[x^-i/i^2, {i, 20}]

Defining the numerical value does not tell Mathematica anything about the ordinary value of f.

In[2]:= f[2] + f[5]


If you ask for a numerical approximation, however, Mathematica uses the numerical values you have defined.

In[3]:= N[%]


You can define numerical values for both functions and symbols. The numerical values are used by all numerical Mathematica functions, including NIntegrate, FindRoot and so on.

Defining numerical values that depend on numerical precision.

This defines a numerical value for the symbol const, using 4n + 5 terms in the product for n-digit precision.

In[4]:= N[const, {n_, Infinity}] := Product[1 - 2^-i, {i, 2, 4n + 5}]

Here is the value of const, computed to 30-digit precision using the value you specified.

In[5]:= N[const, 30]


Mathematica treats numerical values essentially like upvalues. When you define a numerical value for f, Mathematica effectively enters your definition as an upvalue for f with respect to the numerical evaluation operation N.