Interesting pattern came up as I go through the homework replies of my students. Why is there no RationalQ or RealQ? We have Rational and Real as type restrictors / heads in pattern matching, like _Rational or _Real. Why no Qs for them?

  • 4
    It's a good question, really, since we do have IntegerQ. If you need something like that, you can always use a curried form like RationalQ = MatchQ[_Rational]. – Sjoerd Smit Sep 12 at 13:36
  • 6
    No good answer to this one ... but people regularly make the mistake of assuming that IntegerQ tests if an expression is mathematically an integer (what it does is that it checks if the datatype is Integer) – Szabolcs Sep 12 at 13:38
  • 5
    For the math (not datatype), one would use Element[x, Rationals] instead. That will often not evaluate, but it is Simplifyable. It doesn't check datatype. It's checks whether the value is rational. – Szabolcs Sep 12 at 13:57
  • 4
    If there were a RationalQ, I'd expect RationalQ[2] to be False because the datatype of 2 is Integer, not Rational. That has nothing to do with 2 being a rational number. – Szabolcs Sep 12 at 13:58
  • 4
    Related question on RealQ – Rohit Namjoshi Sep 12 at 14:41

There is a RealQ, see

Developer`RealQ

Also relevant:

Developer`MachineRealQ

The difference between the two:

Developer`RealQ[1.`20]
Developer`MachineRealQ[1.`20]

False

True

So, Developer`RealQ is a test for arbitrary precision numbers, while Developer`MachineRealQ checks whether its input is a double precision number.

Notice that both return

Developer`RealQ[1]
Developer`MachineRealQ[1]

False

False

Compare this to

IntegerQ[10^100000]
Developer`MachineIntegerQ[10^100000]

True

False

As Szabolcs and Sjoerd pointed out, these tests are for data types and not tests in mathematical sense. For example, we also have the following:

IntegerQ[(1 - Sqrt[2]) (1 + Sqrt[2])]
IntegerQ[Simplify[(1 - Sqrt[2]) (1 + Sqrt[2])]]

False

True

A somewhat more mathematical test seems to be Assumptions`ARealQ:

Assumptions`ARealQ[(1 - Sqrt[2]) (1 + Sqrt[2])]

True

But as it is undocumented, I really don't know what does it do.

And on top of that, we have Assumptions`ARationalQ, quite a mysterious beast:

Assumptions`ARationalQ[(1 - Sqrt[2]) (1 + Sqrt[2])]
Assumptions`ARationalQ[1/2]
Assumptions`ARationalQ[1]
Assumptions`ARationalQ[I]
Assumptions`ARationalQ[1.]

False

True

True

False

False

Not to mention Reduce`RationalNumberQ which behaves similarly erratic.

Why no RationalQ or RealQ?

Probably because it isn't unambiguous what such a function should do. From the comments above:

If there were a RationalQ, I'd expect RationalQ[2] to be False

But many other users would expect something like this:

enter image description here

For IntegerQ there aren't such conflicting expectations.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.