N

N[expr]

gives the numerical value of expr.

N[expr,n]

attempts to give a result with ndigit precision.

Details

  • Unless numbers in expr are exact, or of sufficiently high precision, N[expr,n] may not be able to give results with ndigit 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 machineprecision 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 all

Basic Examples  (3)

Evaluate numerically:

Evaluate numerically to 50-digit precision:

With machine-precision input, the results are always machine precision:

With exact input, the results can be to the precision specified:

Scope  (20)

Mathematical Constants  (4)

Evaluate mathematical constants to any precision:

Get approximate values for large integers:

Complex number results:

More elaborate symbolic mathematical constants can be detected using NumericQ:

In general, NumericQ expressions represent numbers that can be evaluated to any precision:

General Expressions  (3)

Evaluate the entries of a matrix numerically:

Evaluate numerically the numeric parts of a symbolic expression:

Make the coefficients of a polynomial numerical:

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 base is modified:

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:

Machine-number approximation:

47-digit approximation:

Define a data object that represents a polynomial in a sparse form {{c_(1),p_(1)},...}:

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 :

Get the representation with approximate real coefficients:

Evaluate at :

Generalizations & Extensions  (3)

5-digit accuracy and any precision:

1-digit accuracy, corresponding to integer part only:

Get either 20-digit precision or 30-digit accuracy:

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:

Neat Examples  (1)

A -digit texture:

Wolfram Research (1988), N, Wolfram Language function, https://reference.wolfram.com/language/ref/N.html (updated 2003).

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

BibTeX

@misc{reference.wolfram_2024_n, author="Wolfram Research", title="{N}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/N.html}", note=[Accessed: 21-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_n, organization={Wolfram Research}, title={N}, year={2003}, url={https://reference.wolfram.com/language/ref/N.html}, note=[Accessed: 21-December-2024 ]}