Why no RationalQ or RealQ?
Clash Royale CLAN TAG#URR8PPP
up vote
17
down vote
favorite
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?
expression-test data-types head
 |Â
show 3 more comments
up vote
17
down vote
favorite
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?
expression-test data-types head
4
It's a good question, really, since we do haveIntegerQ
. If you need something like that, you can always use a curried form likeRationalQ = MatchQ[_Rational]
.
– Sjoerd Smit
23 hours ago
5
No good answer to this one ... but people regularly make the mistake of assuming thatIntegerQ
tests if an expression is mathematically an integer (what it does is that it checks if the datatype isInteger
)
– Szabolcs
23 hours ago
5
For the math (not datatype), one would useElement[x, Rationals]
instead. That will often not evaluate, but it isSimplify
able. It doesn't check datatype. It's checks whether the value is rational.
– Szabolcs
22 hours ago
4
If there were aRationalQ
, I'd expectRationalQ[2]
to beFalse
because the datatype of2
isInteger
, notRational
. That has nothing to do with2
being a rational number.
– Szabolcs
22 hours ago
4
Related question on RealQ
– Rohit Namjoshi
22 hours ago
 |Â
show 3 more comments
up vote
17
down vote
favorite
up vote
17
down vote
favorite
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?
expression-test data-types head
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?
expression-test data-types head
expression-test data-types head
edited 21 hours ago
gwr
6,81122356
6,81122356
asked 23 hours ago


Andreas Lauschke
2,7701316
2,7701316
4
It's a good question, really, since we do haveIntegerQ
. If you need something like that, you can always use a curried form likeRationalQ = MatchQ[_Rational]
.
– Sjoerd Smit
23 hours ago
5
No good answer to this one ... but people regularly make the mistake of assuming thatIntegerQ
tests if an expression is mathematically an integer (what it does is that it checks if the datatype isInteger
)
– Szabolcs
23 hours ago
5
For the math (not datatype), one would useElement[x, Rationals]
instead. That will often not evaluate, but it isSimplify
able. It doesn't check datatype. It's checks whether the value is rational.
– Szabolcs
22 hours ago
4
If there were aRationalQ
, I'd expectRationalQ[2]
to beFalse
because the datatype of2
isInteger
, notRational
. That has nothing to do with2
being a rational number.
– Szabolcs
22 hours ago
4
Related question on RealQ
– Rohit Namjoshi
22 hours ago
 |Â
show 3 more comments
4
It's a good question, really, since we do haveIntegerQ
. If you need something like that, you can always use a curried form likeRationalQ = MatchQ[_Rational]
.
– Sjoerd Smit
23 hours ago
5
No good answer to this one ... but people regularly make the mistake of assuming thatIntegerQ
tests if an expression is mathematically an integer (what it does is that it checks if the datatype isInteger
)
– Szabolcs
23 hours ago
5
For the math (not datatype), one would useElement[x, Rationals]
instead. That will often not evaluate, but it isSimplify
able. It doesn't check datatype. It's checks whether the value is rational.
– Szabolcs
22 hours ago
4
If there were aRationalQ
, I'd expectRationalQ[2]
to beFalse
because the datatype of2
isInteger
, notRational
. That has nothing to do with2
being a rational number.
– Szabolcs
22 hours ago
4
Related question on RealQ
– Rohit Namjoshi
22 hours ago
4
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
23 hours ago
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
23 hours ago
5
5
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
23 hours ago
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
23 hours ago
5
5
For the math (not datatype), one would use
Element[x, Rationals]
instead. That will often not evaluate, but it is Simplify
able. It doesn't check datatype. It's checks whether the value is rational.– Szabolcs
22 hours ago
For the math (not datatype), one would use
Element[x, Rationals]
instead. That will often not evaluate, but it is Simplify
able. It doesn't check datatype. It's checks whether the value is rational.– Szabolcs
22 hours ago
4
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
22 hours ago
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
22 hours ago
4
4
Related question on RealQ
– Rohit Namjoshi
22 hours ago
Related question on RealQ
– Rohit Namjoshi
22 hours ago
 |Â
show 3 more comments
2 Answers
2
active
oldest
votes
up vote
19
down vote
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.
add a comment |Â
up vote
13
down vote
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 expectRationalQ[2]
to beFalse
But many other users would expect something like this:
For IntegerQ
there aren't such conflicting expectations.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
19
down vote
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.
add a comment |Â
up vote
19
down vote
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.
add a comment |Â
up vote
19
down vote
up vote
19
down vote
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.
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.
edited 21 hours ago
corey979
20.6k64182
20.6k64182
answered 23 hours ago


Henrik Schumacher
37.5k249107
37.5k249107
add a comment |Â
add a comment |Â
up vote
13
down vote
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 expectRationalQ[2]
to beFalse
But many other users would expect something like this:
For IntegerQ
there aren't such conflicting expectations.
add a comment |Â
up vote
13
down vote
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 expectRationalQ[2]
to beFalse
But many other users would expect something like this:
For IntegerQ
there aren't such conflicting expectations.
add a comment |Â
up vote
13
down vote
up vote
13
down vote
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 expectRationalQ[2]
to beFalse
But many other users would expect something like this:
For IntegerQ
there aren't such conflicting expectations.
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 expectRationalQ[2]
to beFalse
But many other users would expect something like this:
For IntegerQ
there aren't such conflicting expectations.
answered 21 hours ago


Jason B.
45.9k382176
45.9k382176
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181761%2fwhy-no-rationalq-or-realq%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 likeRationalQ = MatchQ[_Rational]
.– Sjoerd Smit
23 hours ago
5
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 isInteger
)– Szabolcs
23 hours ago
5
For the math (not datatype), one would use
Element[x, Rationals]
instead. That will often not evaluate, but it isSimplify
able. It doesn't check datatype. It's checks whether the value is rational.– Szabolcs
22 hours ago
4
If there were a
RationalQ
, I'd expectRationalQ[2]
to beFalse
because the datatype of2
isInteger
, notRational
. That has nothing to do with2
being a rational number.– Szabolcs
22 hours ago
4
Related question on RealQ
– Rohit Namjoshi
22 hours ago