NHoldAll
Details
- NHoldAll, NHoldFirst, and NHoldRest are useful in ensuring that arguments to functions are maintained as exact integers, rather than being converted by N to approximate numbers.
Examples
open all close allBasic Examples (1)
Prevent N from affecting the arguments of a function:
SetAttributes[f, NHoldAll]N[f[1 + 2, 3 + Pi]]Scope (3)
System symbols with the NHoldAll attribute:
ssymb = Cases[Map[ToExpression, Names["System`*"]], _Symbol];
Select[ssymb, MemberQ[Attributes[#], NHoldAll]&]The arguments of Derivative remain unchanged with N:
der = Derivative[1, 0, 1, 0][f][1, 2, 3, x]N leaves the derivative order while changing the point of evaluation:
nder = N[der]nder /. f -> Timesf = #1 ^ 2 + Pi ^ 3(#1 ^ 2 - #2) ^ 2&;The function with coefficients converted to numerical values:
nf = N[f]nf[1, 2]The positional parameters remain unchanged with N because Slot has the NHoldAll attribute:
FullForm[f]Applications (2)
SetAttributes[x, NHoldAll]With this attribute, the variables remain unchanged:
N[2x[1] + 5x[2]^2]Define a data object that represents a polynomial
in a sparse form
:
spoly[cp_][x_] := Module[{c, n, p, y},
{p, c} = Transpose[SortBy[cp, Last]];
n = p[[-1]];
y = c[[-1]];
Do[y = c[[k]] + x ^ (n - p[[k]]) * y;n = p[[k]], {k, Length[p] - 1, 1, -1}];
If[n > 0, y *= x ^ n];
y]Make sure that N only affects the coefficients, not the powers:
N[e : spoly[cp_], pa_] := Module[{p, c, nc},
{c, p} = Transpose[cp];
nc = N[c, pa];
If[nc === c, e, spoly[Transpose[{nc, p}]]]]Default N evaluation of the argument needs to be prevented for the rule above to work:
SetAttributes[spoly, NHoldAll]A representation of the polynomial
:
sp = spoly[{{1, 1}, {2, 2}, {3, 4}, {4, 8}}]{sp[x], Expand[sp[x]]}Get the representation with approximate real coefficients:
nsp = N[sp]nsp[π]Properties & Relations (1)
HoldAll prevents evaluation while NHoldAll only prevents numerical evaluation:
SetAttributes[f1, HoldAll]f1[1 + 2, Pi + E]N[%]SetAttributes[f2, NHoldAll]f2[1 + 2, Pi + E]N[%]You can prevent both by setting both attributes:
SetAttributes[f, {HoldAll, NHoldAll}]f[1 + 2, Pi + E]N[%]See Also
Tech Notes
Related Guides
History
Introduced in 1996 (3.0)
Text
Wolfram Research (1996), NHoldAll, Wolfram Language function, https://reference.wolfram.com/language/ref/NHoldAll.html.
CMS
Wolfram Language. 1996. "NHoldAll." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NHoldAll.html.
APA
Wolfram Language. (1996). NHoldAll. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NHoldAll.html
BibTeX
@misc{reference.wolfram_2026_nholdall, author="Wolfram Research", title="{NHoldAll}", year="1996", howpublished="\url{https://reference.wolfram.com/language/ref/NHoldAll.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_nholdall, organization={Wolfram Research}, title={NHoldAll}, year={1996}, url={https://reference.wolfram.com/language/ref/NHoldAll.html}, note=[Accessed: 12-June-2026]}