You may specify multiple alternative patterns (originally mentioned in a comment):
f[x:(_Real|_Integer)] := 2 x;
You may specify multiple alternative matches simultaneously:
f[(x_Real|x_Integer)] := 2 x;
You may specify by set-membership tests (Element
):
f[x_?(Element[#, Reals] === True &)] := 2 x;
Note, however, that while this option will work for exceptional cases like f[Sqrt[2]]
, is also likely going to be quite a bit slower than any of the above.
If you are concerned not with membership in the reals but rather numeric quantities, you may use:
f[x_?NumericQ] := 2 x;
Note, however, that this will allow complex quantities. This is frequently used to prevent excessive symbolic computation with difficult functions implemented inside of other complex operations, such as Integrate
.
f[x_?NumericQ]
$\endgroup$