N
[expr, prec]N[Pi, 50]
N[1/7]
N[1/7, 5]
You can manually assign numerical values to symbols.
When you do not specify a precision, MachinePrecision
is taken.
N[a] = 10.9
a
N
automatically threads over expressions, except when a symbol has
attributes NHoldAll
, NHoldFirst
, or NHoldRest
.
N[a + b]
N[a, 20]
N[a, 20] = 11;
N[a + b, 20]
N[f[a, b]]
SetAttributes[f, NHoldAll]
N[f[a, b]]
The precision can be a pattern:
N[c, p_?(#>10&)] := p
N[c, 3]
N[c, 11]
You can also use UpSet
or TagSet
to specify values for N
:
N[d] ^= 5;
However, the value will not be stored in UpValues
, but
in NValues
(as for Set
):
UpValues[d]
NValues[d]
e /: N[e] = 6;
N[e]
Values for N[expr]
must be associated with the head of expr:
f /: N[e[f]] = 7;
You can use Condition
:
N[g[x_, y_], p_] := x + y * Pi /; x + y > 3
SetAttributes[g, NHoldRest]
N[g[1, 1]]
N[g[2, 2]] // InputForm
The precision of the result is no higher than the precision of the input
N[Exp[0.1], 100]
% // Precision
N[Exp[1/10], 100]
% // Precision
N[Exp[1.0`20], 100]
% // Precision
N can also accept an option “Method”. This establishes what is the prefrered underlying method to compute numerical values:
N[F[Pi], 30, Method->"numpy"]
N[F[Pi], 30, Method->"sympy"]