N
N[expr]
gives the numerical value of expr.
N[expr,n]
attempts to give a result with n‐digit precision.
Details
- Unless numbers in expr are exact, or of sufficiently high precision, N[expr,n] may not be able to give results with n‐digit precision.
- N[expr,n] may internally do computations to more than n digits of precision.
- $MaxExtraPrecision specifies the maximum number of extra digits of precision that will ever be used internally.
- The precision n is given in decimal digits; it need not be an integer.
- n must lie between $MinPrecision and $MaxPrecision. $MaxPrecision can be set to Infinity.
- n can be smaller than $MachinePrecision.
- N[expr] gives a machine‐precision number, so long as its magnitude is between $MinMachineNumber and $MaxMachineNumber.
- N[expr] is equivalent to N[expr,MachinePrecision].
- N[0] gives the number 0. with machine precision.
- N converts all nonzero numbers to Real or Complex form.
- N converts each successive argument of any function it encounters to numerical form, unless the head of the function has an attribute such as NHoldAll.
- You can define numerical values of functions using N[f[args]]:=value and N[f[args],n]:=value.
- N[expr,{p,a}] attempts to generate a result with precision at most p and accuracy at most a.
- N[expr,{Infinity,a}] attempts to generate a result with accuracy a.
- N[expr,{Infinity,1}] attempts to find a numerical approximation to the integer part of expr.
Examples
open allclose allBasic Examples (3)
Scope (20)
Mathematical Constants (4)
General Expressions (3)
Machine and Adaptive Precision (4)
N[e] typically works by replacing numbers with machine numbers and computing the result:
This is equivalent to the expression with machine numbers:
The machine roundoff error shows up clearly on a log-log plot of the difference quotient error:
N[e,p] works adaptively using arbitrary-precision numbers when p is not MachinePrecision:
With adaptive precision, the difference quotient error matches the theoretical value:
The adaptive precision control has a default limit, $MaxExtraPrecision, for extra precision:
When the limit is reached, you can locally increase $MaxExtraPrecision to resolve the difficulty:
N does not raise the precision of approximate numbers in its input:
The precision of the result is the same as that of the input:
You can use SetPrecision to increase the precision of input expressions:
Data Objects and Special Rules (5)
N does not affect exact numeric powers:
The power is modified if it is not NumericQ:
SparseArray objects are treated based on the represented array:
In general, N[Normal[s]] is identical to Normal[N[s]]:
Data objects like InterpolatingFunction have special rules so that only the data is affected:
Only the appropriate data has been changed so that the new function uses machine numbers:
Operations like Integrate use the corresponding N-functions when appropriate:
This is using NIntegrate:
For high requested precision, numerical algorithms may take a lot of time or need adjustment:
In this case, you may want to use NIntegrate directly:
For most expressions, N affects the tree shown in FullForm:
The 1. appears in front of x because of how e is represented:
The integers in e have just been converted to machine numbers:
Defining Special Rules (4)
Prevent an indexed variable from being affected by N using the NHoldAll attribute:
With this, N only affects the coefficients:
Without this, N affects the indices too:
Define a numerical constant equal to :
In an exact symbolic expression, the constant does not evaluate:
With N, it does:
Add a definition that will work with arbitrary precision or accuracy:
Numerically evaluate an expression to accuracy 20:
Define a function that represents the real-valued root for positive and positive integer :
Prevent its second argument from being converted to real using NHoldRest:
An exact representation of the cube root of 2:
Define a data object that represents a polynomial in a sparse form :
Make sure that N only affects the coefficients, not the powers:
Default N evaluation of the argument needs to be prevented for the rule to work:
A representation of the polynomial :
Generalizations & Extensions (3)
Properties & Relations (3)
The result of N[e] generally has precision MachinePrecision:
If the result is too large for machine numbers, the precision may be different:
The result of N[e,p] generally has Precision equal to p:
When this is not possible, the Wolfram Language will give a message:
Typically, increasing $MaxExtraPrecision will resolve the problem:
For hidden zeros, positive precision is not achievable, so you may want to use accuracy:
The result of N[e,{∞,a}] generally has Accuracy equal to a:
When this is not possible, the Wolfram Language will give a message:
Typically, increasing $MaxExtraPrecision will resolve the problem:
Text
Wolfram Research (1988), N, Wolfram Language function, https://reference.wolfram.com/language/ref/N.html (updated 2003).
CMS
Wolfram Language. 1988. "N." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2003. https://reference.wolfram.com/language/ref/N.html.
APA
Wolfram Language. (1988). N. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/N.html