# How can I define a new symbolic constant like Pi?

There are a few builtin symbolic constants which behave like numbers, e.g. E, Pi, EulerGamma, etc.

How can I define my own in such a way that the system will treat them just like the builtin ones?

I was recently reminded that the following "functions" are settable, and I was surprised (even though I've seen this before). So I thought that it is valuable to share this information.

There are three critical properties of symbolic constants:

• The Constant attribute, which is used by Dt

• The ability to tell that it is a numeric expression (NumericQ[Pi] === True).

• The ability to compute the value to any precision.

We can implement all three by direct assignment. Let the symbol sqrt2 represent $\sqrt{2}$:

SetAttributes[sqrt2, Constant]
NumericQ[sqrt2] = True;
N[sqrt2, prec___] := N[Sqrt, prec]


Note that it wasn't necessary to unprotect any symbols.

Now sqrt2 doesn't evaluate:

sqrt2
(* sqrt2 *)


But it can be computed numerically:

N[sqrt2, 100]
(* 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573 *)


It is numeric:

NumericQ[sqrt2]
(* True *)


Which means that many functions work well with it without the need for additional definitions:

Positive[sqrt2]
(* True *)

Im[sqrt2]
(* 0 *)

Integrate[Exp[-sqrt2 x], {x, 0, Infinity}]
(* 1/sqrt2 *)


Integrate wouldn't give the same result with an arbitrary symbolic parameter:

Integrate[Exp[-s x], {x, 0, Infinity}]
(* ConditionalExpression[1/s, Re[s] > 0] *)


And of course it works with Dt

Dt[sqrt2 x, x]
(* sqrt2 *)


Update: How do we clear such a symbol? Like this:

ClearAll[sqrt2]
NumericQ[sqrt2] =.


I do not know where the NumericQ setting is stored. It doesn't seem to be associated either with NumericQ or sqrt2. Also, NumericQ is clearly set up to handle this usage and will not accept invalid values.

Did I miss anything? If so, let me know!

• @xzczd Where is it stored? I have no idea. It doesn't seem to be associated either with NumericQ or sqrt2. But I can tell you how to clear it: NumericQ[sqrt2] =. If you asked the same question about N then the answer would be NValues[sqrt2]. Oct 11 '16 at 14:58
• OK. Another "missing" truth: the sqrt2 is compilable. Oct 11 '16 at 15:07
• One thing this approach misses is missing relations. Mathematica knows for example that Log[E] == 1 and Sin[Pi] == 0, but it doesn't know sqrt2^2 == 2. I suppose this is a case by case issue though... Oct 11 '16 at 15:15
• Though through spelunking, one can find that FunctionExpand (and therefore FullSimplify) can be taught rules by adding them to SimplifyDump\$FSTab and your symbol can be hooked in by adding your symbol to SimplifyDumpPositiveRules. Oct 11 '16 at 19:54
• When clearing could you remove the need to Unset NumericQ[sqrt2] by using UpSet? NumericQ[sqrt2] ^= True Feb 4 '17 at 1:24