Why no RationalQ or RealQ?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question



















  • 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






  • 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







  • 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
    22 hours ago







  • 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






  • 4




    Related question on RealQ
    – Rohit Namjoshi
    22 hours ago














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?










share|improve this question



















  • 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






  • 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







  • 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
    22 hours ago







  • 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






  • 4




    Related question on RealQ
    – Rohit Namjoshi
    22 hours ago












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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




    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




    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
    22 hours ago







  • 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






  • 4




    Related question on RealQ
    – Rohit Namjoshi
    22 hours ago












  • 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






  • 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







  • 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
    22 hours ago







  • 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






  • 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 Simplifyable. 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 Simplifyable. 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










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.






share|improve this answer





























    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 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.






    share|improve this answer




















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "387"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      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






























      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.






      share|improve this answer


























        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.






        share|improve this answer
























          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 21 hours ago









          corey979

          20.6k64182




          20.6k64182










          answered 23 hours ago









          Henrik Schumacher

          37.5k249107




          37.5k249107




















              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 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.






              share|improve this answer
























                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 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.






                share|improve this answer






















                  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 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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 21 hours ago









                  Jason B.

                  45.9k382176




                  45.9k382176



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      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













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery