# Is there any way to use head of Real and Integer altogether?

Is there any way to use the head of Integer and Real together like below? The below is not right, I know that, it is just for showing my thought.

f[x_(Integer||Real)] := x^2


The reason I ask this is because I need a function that receives an argument which is either integer or real.

• x : (_Integer | _Real ). Look up Alternatives[]. – J. M. will be back soon Jul 15 '15 at 10:21
• Integer and Real are data types. Are you sure this is what you want to check for? This is not the same as determining whether an arbitrary expression is integer or real (or rational or complex). Neither of Pi, Sqrt[2], 2/3 are either of Integer or Real type, but they are all real numbers. – Szabolcs Jul 15 '15 at 10:22
• You need Alternatives rather than Or. Either x : (_Integer | _Real) or x_Integer | x_Real will work. – Mr.Wizard Jul 15 '15 at 10:22
• Related to Szabolcs's note, you might want to just use NumberQ[]/NumericQ[] along with a test like x_ /; Im[x] == 0. – J. M. will be back soon Jul 15 '15 at 10:46
• @Guess I was just getting around to that recommendation. :-) – Mr.Wizard Jul 15 '15 at 10:47

To match your literal request you need Alternatives rather than Or.
Either x : (_Integer | _Real) or x_Integer | x_Real will work.

Following what Szabolcs and "Guess who it is" wrote you might define a realQ like so:

realQ = NumericQ[#] && Im[#] == 0 &;

f[x_?realQ] := x^2

f /@ {1, Pi, 1.3, 2/3, x^2, 7.1 - 2.8 I}

{1, π^2, 1.69, 4/9, f[x^2], f[7.1 - 2.8 I]}


Of note for those who are comfortable using undocumented functions:

InternalRealValuedNumericQ /@ {1, Pi, 1.3, 2/3, x^2, 7.1 - 2.8 I}

{True, True, True, True, False, False}


There is also InternalRealValuedNumberQ which passes only explicit numbers:

InternalRealValuedNumberQ /@ {1, Pi, 1.3, 2/3, x^2, 7.1 - 2.8 I}

{True, False, True, True, False, False}

• Thank you so much for the kind and elegant explanation! – Smart Humanism Jul 15 '15 at 13:46
• @Smart You're welcome. :-) – Mr.Wizard Jul 15 '15 at 13:56
• Just a note: InternalRealValuedNumberQ  is consistent with the behavior of NumberQ which returns False when applied to Pi. – rcollyer Jul 15 '15 at 14:41
• @rcollyer I intended to imply that; sorry if I failed. – Mr.Wizard Jul 15 '15 at 14:50
• Just making it explicit. Implications are often lost. – rcollyer Jul 15 '15 at 14:50

I frequently write functions that take one or more arguments which I limit to those quantities that mathematicians call real numbers. In Mathematica that means any quantity satisfying NumericQ excepting complex numbers. To facilitate writing such functions, I define a pattern

validNum = Except[_Complex, _?NumericQ];


This pattern is used like so:

f[x : validNum] := x^2


### Update

As Guesswhoitis points out the above is not fool-proof. A more robust version is

validNum = Except[z_ /; Head[N[z]] === Complex, _?NumericQ];
f /@ {1, 1/2, .5, Pi, 1 + I/2, 1. + .5 I, Sqrt[-1], (-1)^(2/3), E + I Pi}

{1, 1/4, 0.25, Pi^2, f[1 + I/2], f[1. + 0.5*I], f[I], f[(-1)^(2/3)], f[E + I*Pi]}

• How does it deal with E + I Pi or (-1)^(2/3)? – J. M. will be back soon Jul 15 '15 at 14:09
• @Guesswhoitis. Not well, but I still find it useful. – m_goldberg Jul 15 '15 at 14:15
• @Mr.Wizard. You are right. I have fixed the error. I really must stop violating my own principle of "never post code you have not tested". – m_goldberg Jul 16 '15 at 2:34
• @Guesswhoitis. Do you find my update more to your liking? – m_goldberg Jul 16 '15 at 2:36
• I guess the only thing that would trip it up now would be 1. + 0. I and ilk, but it looks okay otherwise. – J. M. will be back soon Jul 16 '15 at 4:02