What is precedence in this expression?












1















When I run this program:



print(rand*100)


I get values from [0,1) range.



But for this:



print(100*rand)


I get values from [0,100) range.



What is precedence here? and why first expression does not return values from [0,100) range?










share|improve this question



























    1















    When I run this program:



    print(rand*100)


    I get values from [0,1) range.



    But for this:



    print(100*rand)


    I get values from [0,100) range.



    What is precedence here? and why first expression does not return values from [0,100) range?










    share|improve this question

























      1












      1








      1








      When I run this program:



      print(rand*100)


      I get values from [0,1) range.



      But for this:



      print(100*rand)


      I get values from [0,100) range.



      What is precedence here? and why first expression does not return values from [0,100) range?










      share|improve this question














      When I run this program:



      print(rand*100)


      I get values from [0,1) range.



      But for this:



      print(100*rand)


      I get values from [0,100) range.



      What is precedence here? and why first expression does not return values from [0,100) range?







      perl perlop






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 14:59









      Eugen KonkovEugen Konkov

      5,89533861




      5,89533861
























          3 Answers
          3






          active

          oldest

          votes


















          6














          rand has two syntax:




          • rand

          • rand EXPR


          If what follows rand can be the start of an expression (EXPR), Perl assumes you are using the latter form.



          * can start an EXPR, so rand*... is parsed as rand EXPR. This means that rand*100 is equivalent to rand(*100).



          $ perl -MO=Deparse,-p -wle'print(rand*100)'
          BEGIN { $^W = 1; }
          BEGIN { $/ = "n"; $ = "n"; }
          print(rand(*100));
          -e syntax OK

          $ perl -wle'print(rand*100)'
          Argument "*main::100" isn't numeric in rand at -e line 1.
          0.57355563536203





          share|improve this answer


























          • Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, so rand * 100 still does not avoid this issue; and it happens with rand / 100 too just resulting in a parse error once the parser realizes there's no closing /.

            – Grinnz
            Nov 25 '18 at 17:31



















          2














          You can always use B::Deparse to see how Perl is parsing an expression.



          $ perl -MO=Deparse -e'print(100*rand)'
          print 100 * (rand);
          -e syntax OK
          $ perl -MO=Deparse -e'print(rand*100)'
          print rand *100;
          -e syntax OK





          share|improve this answer
























          • What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)

            – Michael G.
            Nov 25 '18 at 15:36






          • 1





            -MO=Deparse,-p makes it clearer still.

            – mob
            Nov 25 '18 at 16:55



















          0














          The problem is not about presidence, it is about syntax. The presence of the glob "*", which renders the number unworkable.



          print rand 100;


          = 0 - 100



          print (rand(100)); 


          ... gives the same result.



          print rand *100


          ... is ignored



          Essentially you have created a number "*100". "i100" is an interesting number however, but I'm not sure how it is randomised :-)






          share|improve this answer



















          • 1





            The number is not *100 but the numification of the variable *100, which is at that point an undefined glob.

            – Grinnz
            Nov 25 '18 at 17:34











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468764%2fwhat-is-precedence-in-this-expression%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














          rand has two syntax:




          • rand

          • rand EXPR


          If what follows rand can be the start of an expression (EXPR), Perl assumes you are using the latter form.



          * can start an EXPR, so rand*... is parsed as rand EXPR. This means that rand*100 is equivalent to rand(*100).



          $ perl -MO=Deparse,-p -wle'print(rand*100)'
          BEGIN { $^W = 1; }
          BEGIN { $/ = "n"; $ = "n"; }
          print(rand(*100));
          -e syntax OK

          $ perl -wle'print(rand*100)'
          Argument "*main::100" isn't numeric in rand at -e line 1.
          0.57355563536203





          share|improve this answer


























          • Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, so rand * 100 still does not avoid this issue; and it happens with rand / 100 too just resulting in a parse error once the parser realizes there's no closing /.

            – Grinnz
            Nov 25 '18 at 17:31
















          6














          rand has two syntax:




          • rand

          • rand EXPR


          If what follows rand can be the start of an expression (EXPR), Perl assumes you are using the latter form.



          * can start an EXPR, so rand*... is parsed as rand EXPR. This means that rand*100 is equivalent to rand(*100).



          $ perl -MO=Deparse,-p -wle'print(rand*100)'
          BEGIN { $^W = 1; }
          BEGIN { $/ = "n"; $ = "n"; }
          print(rand(*100));
          -e syntax OK

          $ perl -wle'print(rand*100)'
          Argument "*main::100" isn't numeric in rand at -e line 1.
          0.57355563536203





          share|improve this answer


























          • Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, so rand * 100 still does not avoid this issue; and it happens with rand / 100 too just resulting in a parse error once the parser realizes there's no closing /.

            – Grinnz
            Nov 25 '18 at 17:31














          6












          6








          6







          rand has two syntax:




          • rand

          • rand EXPR


          If what follows rand can be the start of an expression (EXPR), Perl assumes you are using the latter form.



          * can start an EXPR, so rand*... is parsed as rand EXPR. This means that rand*100 is equivalent to rand(*100).



          $ perl -MO=Deparse,-p -wle'print(rand*100)'
          BEGIN { $^W = 1; }
          BEGIN { $/ = "n"; $ = "n"; }
          print(rand(*100));
          -e syntax OK

          $ perl -wle'print(rand*100)'
          Argument "*main::100" isn't numeric in rand at -e line 1.
          0.57355563536203





          share|improve this answer















          rand has two syntax:




          • rand

          • rand EXPR


          If what follows rand can be the start of an expression (EXPR), Perl assumes you are using the latter form.



          * can start an EXPR, so rand*... is parsed as rand EXPR. This means that rand*100 is equivalent to rand(*100).



          $ perl -MO=Deparse,-p -wle'print(rand*100)'
          BEGIN { $^W = 1; }
          BEGIN { $/ = "n"; $ = "n"; }
          print(rand(*100));
          -e syntax OK

          $ perl -wle'print(rand*100)'
          Argument "*main::100" isn't numeric in rand at -e line 1.
          0.57355563536203






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 2:00

























          answered Nov 25 '18 at 15:09









          ikegamiikegami

          263k11177397




          263k11177397













          • Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, so rand * 100 still does not avoid this issue; and it happens with rand / 100 too just resulting in a parse error once the parser realizes there's no closing /.

            – Grinnz
            Nov 25 '18 at 17:31



















          • Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, so rand * 100 still does not avoid this issue; and it happens with rand / 100 too just resulting in a parse error once the parser realizes there's no closing /.

            – Grinnz
            Nov 25 '18 at 17:31

















          Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, so rand * 100 still does not avoid this issue; and it happens with rand / 100 too just resulting in a parse error once the parser realizes there's no closing /.

          – Grinnz
          Nov 25 '18 at 17:31





          Two things that make this ambiguity even more fun: Spaces between sigils and variable names are allowed, so rand * 100 still does not avoid this issue; and it happens with rand / 100 too just resulting in a parse error once the parser realizes there's no closing /.

          – Grinnz
          Nov 25 '18 at 17:31













          2














          You can always use B::Deparse to see how Perl is parsing an expression.



          $ perl -MO=Deparse -e'print(100*rand)'
          print 100 * (rand);
          -e syntax OK
          $ perl -MO=Deparse -e'print(rand*100)'
          print rand *100;
          -e syntax OK





          share|improve this answer
























          • What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)

            – Michael G.
            Nov 25 '18 at 15:36






          • 1





            -MO=Deparse,-p makes it clearer still.

            – mob
            Nov 25 '18 at 16:55
















          2














          You can always use B::Deparse to see how Perl is parsing an expression.



          $ perl -MO=Deparse -e'print(100*rand)'
          print 100 * (rand);
          -e syntax OK
          $ perl -MO=Deparse -e'print(rand*100)'
          print rand *100;
          -e syntax OK





          share|improve this answer
























          • What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)

            – Michael G.
            Nov 25 '18 at 15:36






          • 1





            -MO=Deparse,-p makes it clearer still.

            – mob
            Nov 25 '18 at 16:55














          2












          2








          2







          You can always use B::Deparse to see how Perl is parsing an expression.



          $ perl -MO=Deparse -e'print(100*rand)'
          print 100 * (rand);
          -e syntax OK
          $ perl -MO=Deparse -e'print(rand*100)'
          print rand *100;
          -e syntax OK





          share|improve this answer













          You can always use B::Deparse to see how Perl is parsing an expression.



          $ perl -MO=Deparse -e'print(100*rand)'
          print 100 * (rand);
          -e syntax OK
          $ perl -MO=Deparse -e'print(rand*100)'
          print rand *100;
          -e syntax OK






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 15:12









          Dave CrossDave Cross

          47.3k33978




          47.3k33978













          • What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)

            – Michael G.
            Nov 25 '18 at 15:36






          • 1





            -MO=Deparse,-p makes it clearer still.

            – mob
            Nov 25 '18 at 16:55



















          • What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)

            – Michael G.
            Nov 25 '18 at 15:36






          • 1





            -MO=Deparse,-p makes it clearer still.

            – mob
            Nov 25 '18 at 16:55

















          What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)

          – Michael G.
          Nov 25 '18 at 15:36





          What an honour ... I've actually got your "Data Munging with Perl" on my bookshelf directly above me. Metaphoric in more ways than one ;-)

          – Michael G.
          Nov 25 '18 at 15:36




          1




          1





          -MO=Deparse,-p makes it clearer still.

          – mob
          Nov 25 '18 at 16:55





          -MO=Deparse,-p makes it clearer still.

          – mob
          Nov 25 '18 at 16:55











          0














          The problem is not about presidence, it is about syntax. The presence of the glob "*", which renders the number unworkable.



          print rand 100;


          = 0 - 100



          print (rand(100)); 


          ... gives the same result.



          print rand *100


          ... is ignored



          Essentially you have created a number "*100". "i100" is an interesting number however, but I'm not sure how it is randomised :-)






          share|improve this answer



















          • 1





            The number is not *100 but the numification of the variable *100, which is at that point an undefined glob.

            – Grinnz
            Nov 25 '18 at 17:34
















          0














          The problem is not about presidence, it is about syntax. The presence of the glob "*", which renders the number unworkable.



          print rand 100;


          = 0 - 100



          print (rand(100)); 


          ... gives the same result.



          print rand *100


          ... is ignored



          Essentially you have created a number "*100". "i100" is an interesting number however, but I'm not sure how it is randomised :-)






          share|improve this answer



















          • 1





            The number is not *100 but the numification of the variable *100, which is at that point an undefined glob.

            – Grinnz
            Nov 25 '18 at 17:34














          0












          0








          0







          The problem is not about presidence, it is about syntax. The presence of the glob "*", which renders the number unworkable.



          print rand 100;


          = 0 - 100



          print (rand(100)); 


          ... gives the same result.



          print rand *100


          ... is ignored



          Essentially you have created a number "*100". "i100" is an interesting number however, but I'm not sure how it is randomised :-)






          share|improve this answer













          The problem is not about presidence, it is about syntax. The presence of the glob "*", which renders the number unworkable.



          print rand 100;


          = 0 - 100



          print (rand(100)); 


          ... gives the same result.



          print rand *100


          ... is ignored



          Essentially you have created a number "*100". "i100" is an interesting number however, but I'm not sure how it is randomised :-)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 15:18









          Michael G.Michael G.

          2221316




          2221316








          • 1





            The number is not *100 but the numification of the variable *100, which is at that point an undefined glob.

            – Grinnz
            Nov 25 '18 at 17:34














          • 1





            The number is not *100 but the numification of the variable *100, which is at that point an undefined glob.

            – Grinnz
            Nov 25 '18 at 17:34








          1




          1





          The number is not *100 but the numification of the variable *100, which is at that point an undefined glob.

          – Grinnz
          Nov 25 '18 at 17:34





          The number is not *100 but the numification of the variable *100, which is at that point an undefined glob.

          – Grinnz
          Nov 25 '18 at 17:34


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468764%2fwhat-is-precedence-in-this-expression%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)