awk arithmetic differs from expr

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












There are some contrast output between awk arithmetic and expr.



Example



expr 11111111111111111111 / 22


gives



505050505050505050


but with awk:



echo '11111111111111111111' | awk 'q=$1/22;;print q'


gives



505050505050505024


Can somebody explain?










share|improve this question

























    up vote
    1
    down vote

    favorite












    There are some contrast output between awk arithmetic and expr.



    Example



    expr 11111111111111111111 / 22


    gives



    505050505050505050


    but with awk:



    echo '11111111111111111111' | awk 'q=$1/22;;print q'


    gives



    505050505050505024


    Can somebody explain?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      There are some contrast output between awk arithmetic and expr.



      Example



      expr 11111111111111111111 / 22


      gives



      505050505050505050


      but with awk:



      echo '11111111111111111111' | awk 'q=$1/22;;print q'


      gives



      505050505050505024


      Can somebody explain?










      share|improve this question













      There are some contrast output between awk arithmetic and expr.



      Example



      expr 11111111111111111111 / 22


      gives



      505050505050505050


      but with awk:



      echo '11111111111111111111' | awk 'q=$1/22;;print q'


      gives



      505050505050505024


      Can somebody explain?







      awk arithmetic expr






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 16 at 19:59







      user305910



























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote













          Presumably your awk works with floating point values, like GNU awk seems to do:



          $ gawk 'BEGIN a = 11111111111111111111; print a, a/22; '
          11111111111111110656 505050505050505024


          It can't store 11111111111111111111 accurately, and it can't store the remainder accurately either (11111111111111111111 / 22 is 505050505050505029.81...).



          Your expr seems to have a wider numeric range. Mine doesn't (the one from GNU coreutils 8.26):



          $ expr 11111111111111111111 / 22
          expr: 11111111111111111111: Numerical result out of range



          As @steeldriver comments, current versions of GNU awk also have the capability to use the GNU MPFR library for high-precision arithmetic. For example, quad-precision floats are enough to give an accurate answer for this division:



          $ gawk -M -v PREC="quad" -v OFMT="%.6f" 
          'BEGIN a = 11111111111111111111; print a, a/22; '
          11111111111111111111 505050505050505050.500000


          Other that that, bc or Python can be used for arbitrarily large numbers.






          share|improve this answer


















          • 3




            Note that at least newer versions of GNU awk have features for Getting the Accuracy You Need e.g. gawk -M -v PREC="quad" -v OFMT="%.0f" 'BEGIN a = 11111111111111111111; print a, a/22; '
            – steeldriver
            Aug 17 at 0:58










          • @Isaac, well, the online manual says the default for OFMT is %.6g, so I get 5.05051e+17 for gawk -M -vPREC=quad 'BEGIN a = 11111111111111111111; print a/22; '. As for just using -M, gawk -M 'BEGIN a = 11111111111111111111; print a/22; ' gives me the wrong answer 505050505050505024. I'm not sure if that's a bug or if it automatically converts the number to a float when it's no longer an integer (a/11 is an integer, and it gives the right answer with just -M).
            – ilkkachu
            Aug 21 at 8:46











          • And there's of course ROUNDMODE for setting the rounding mode if "round to even" isn't what you want
            – ilkkachu
            Aug 21 at 8:50










          • @ilkkachu (1) Yes, Sorry about OFMT, yes, it is g not f. (2) If the result of a division require a decimal (some non-zero digit after the dot) then that result is internally converted to a float with the precision given by PREC. Any integer gets as many digits (before the dot) as it may need. Try gawk -M -vPREC=20 -vOFMT='%.30f' 'BEGIN a=22^22; b=101; print a,a/22,b/10; ' The first two numbers are integers and are exact, the last is as imprecise as given by PREC=20. Change it to PREC=200 to see the change on the last number only.(4)Yes, rounding could be adjusted.
            – Isaac
            Aug 21 at 14:53










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          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%2funix.stackexchange.com%2fquestions%2f463061%2fawk-arithmetic-differs-from-expr%23new-answer', 'question_page');

          );

          Post as a guest





























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote













          Presumably your awk works with floating point values, like GNU awk seems to do:



          $ gawk 'BEGIN a = 11111111111111111111; print a, a/22; '
          11111111111111110656 505050505050505024


          It can't store 11111111111111111111 accurately, and it can't store the remainder accurately either (11111111111111111111 / 22 is 505050505050505029.81...).



          Your expr seems to have a wider numeric range. Mine doesn't (the one from GNU coreutils 8.26):



          $ expr 11111111111111111111 / 22
          expr: 11111111111111111111: Numerical result out of range



          As @steeldriver comments, current versions of GNU awk also have the capability to use the GNU MPFR library for high-precision arithmetic. For example, quad-precision floats are enough to give an accurate answer for this division:



          $ gawk -M -v PREC="quad" -v OFMT="%.6f" 
          'BEGIN a = 11111111111111111111; print a, a/22; '
          11111111111111111111 505050505050505050.500000


          Other that that, bc or Python can be used for arbitrarily large numbers.






          share|improve this answer


















          • 3




            Note that at least newer versions of GNU awk have features for Getting the Accuracy You Need e.g. gawk -M -v PREC="quad" -v OFMT="%.0f" 'BEGIN a = 11111111111111111111; print a, a/22; '
            – steeldriver
            Aug 17 at 0:58










          • @Isaac, well, the online manual says the default for OFMT is %.6g, so I get 5.05051e+17 for gawk -M -vPREC=quad 'BEGIN a = 11111111111111111111; print a/22; '. As for just using -M, gawk -M 'BEGIN a = 11111111111111111111; print a/22; ' gives me the wrong answer 505050505050505024. I'm not sure if that's a bug or if it automatically converts the number to a float when it's no longer an integer (a/11 is an integer, and it gives the right answer with just -M).
            – ilkkachu
            Aug 21 at 8:46











          • And there's of course ROUNDMODE for setting the rounding mode if "round to even" isn't what you want
            – ilkkachu
            Aug 21 at 8:50










          • @ilkkachu (1) Yes, Sorry about OFMT, yes, it is g not f. (2) If the result of a division require a decimal (some non-zero digit after the dot) then that result is internally converted to a float with the precision given by PREC. Any integer gets as many digits (before the dot) as it may need. Try gawk -M -vPREC=20 -vOFMT='%.30f' 'BEGIN a=22^22; b=101; print a,a/22,b/10; ' The first two numbers are integers and are exact, the last is as imprecise as given by PREC=20. Change it to PREC=200 to see the change on the last number only.(4)Yes, rounding could be adjusted.
            – Isaac
            Aug 21 at 14:53














          up vote
          5
          down vote













          Presumably your awk works with floating point values, like GNU awk seems to do:



          $ gawk 'BEGIN a = 11111111111111111111; print a, a/22; '
          11111111111111110656 505050505050505024


          It can't store 11111111111111111111 accurately, and it can't store the remainder accurately either (11111111111111111111 / 22 is 505050505050505029.81...).



          Your expr seems to have a wider numeric range. Mine doesn't (the one from GNU coreutils 8.26):



          $ expr 11111111111111111111 / 22
          expr: 11111111111111111111: Numerical result out of range



          As @steeldriver comments, current versions of GNU awk also have the capability to use the GNU MPFR library for high-precision arithmetic. For example, quad-precision floats are enough to give an accurate answer for this division:



          $ gawk -M -v PREC="quad" -v OFMT="%.6f" 
          'BEGIN a = 11111111111111111111; print a, a/22; '
          11111111111111111111 505050505050505050.500000


          Other that that, bc or Python can be used for arbitrarily large numbers.






          share|improve this answer


















          • 3




            Note that at least newer versions of GNU awk have features for Getting the Accuracy You Need e.g. gawk -M -v PREC="quad" -v OFMT="%.0f" 'BEGIN a = 11111111111111111111; print a, a/22; '
            – steeldriver
            Aug 17 at 0:58










          • @Isaac, well, the online manual says the default for OFMT is %.6g, so I get 5.05051e+17 for gawk -M -vPREC=quad 'BEGIN a = 11111111111111111111; print a/22; '. As for just using -M, gawk -M 'BEGIN a = 11111111111111111111; print a/22; ' gives me the wrong answer 505050505050505024. I'm not sure if that's a bug or if it automatically converts the number to a float when it's no longer an integer (a/11 is an integer, and it gives the right answer with just -M).
            – ilkkachu
            Aug 21 at 8:46











          • And there's of course ROUNDMODE for setting the rounding mode if "round to even" isn't what you want
            – ilkkachu
            Aug 21 at 8:50










          • @ilkkachu (1) Yes, Sorry about OFMT, yes, it is g not f. (2) If the result of a division require a decimal (some non-zero digit after the dot) then that result is internally converted to a float with the precision given by PREC. Any integer gets as many digits (before the dot) as it may need. Try gawk -M -vPREC=20 -vOFMT='%.30f' 'BEGIN a=22^22; b=101; print a,a/22,b/10; ' The first two numbers are integers and are exact, the last is as imprecise as given by PREC=20. Change it to PREC=200 to see the change on the last number only.(4)Yes, rounding could be adjusted.
            – Isaac
            Aug 21 at 14:53












          up vote
          5
          down vote










          up vote
          5
          down vote









          Presumably your awk works with floating point values, like GNU awk seems to do:



          $ gawk 'BEGIN a = 11111111111111111111; print a, a/22; '
          11111111111111110656 505050505050505024


          It can't store 11111111111111111111 accurately, and it can't store the remainder accurately either (11111111111111111111 / 22 is 505050505050505029.81...).



          Your expr seems to have a wider numeric range. Mine doesn't (the one from GNU coreutils 8.26):



          $ expr 11111111111111111111 / 22
          expr: 11111111111111111111: Numerical result out of range



          As @steeldriver comments, current versions of GNU awk also have the capability to use the GNU MPFR library for high-precision arithmetic. For example, quad-precision floats are enough to give an accurate answer for this division:



          $ gawk -M -v PREC="quad" -v OFMT="%.6f" 
          'BEGIN a = 11111111111111111111; print a, a/22; '
          11111111111111111111 505050505050505050.500000


          Other that that, bc or Python can be used for arbitrarily large numbers.






          share|improve this answer














          Presumably your awk works with floating point values, like GNU awk seems to do:



          $ gawk 'BEGIN a = 11111111111111111111; print a, a/22; '
          11111111111111110656 505050505050505024


          It can't store 11111111111111111111 accurately, and it can't store the remainder accurately either (11111111111111111111 / 22 is 505050505050505029.81...).



          Your expr seems to have a wider numeric range. Mine doesn't (the one from GNU coreutils 8.26):



          $ expr 11111111111111111111 / 22
          expr: 11111111111111111111: Numerical result out of range



          As @steeldriver comments, current versions of GNU awk also have the capability to use the GNU MPFR library for high-precision arithmetic. For example, quad-precision floats are enough to give an accurate answer for this division:



          $ gawk -M -v PREC="quad" -v OFMT="%.6f" 
          'BEGIN a = 11111111111111111111; print a, a/22; '
          11111111111111111111 505050505050505050.500000


          Other that that, bc or Python can be used for arbitrarily large numbers.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 18 at 15:52

























          answered Aug 16 at 20:18









          ilkkachu

          51.2k678141




          51.2k678141







          • 3




            Note that at least newer versions of GNU awk have features for Getting the Accuracy You Need e.g. gawk -M -v PREC="quad" -v OFMT="%.0f" 'BEGIN a = 11111111111111111111; print a, a/22; '
            – steeldriver
            Aug 17 at 0:58










          • @Isaac, well, the online manual says the default for OFMT is %.6g, so I get 5.05051e+17 for gawk -M -vPREC=quad 'BEGIN a = 11111111111111111111; print a/22; '. As for just using -M, gawk -M 'BEGIN a = 11111111111111111111; print a/22; ' gives me the wrong answer 505050505050505024. I'm not sure if that's a bug or if it automatically converts the number to a float when it's no longer an integer (a/11 is an integer, and it gives the right answer with just -M).
            – ilkkachu
            Aug 21 at 8:46











          • And there's of course ROUNDMODE for setting the rounding mode if "round to even" isn't what you want
            – ilkkachu
            Aug 21 at 8:50










          • @ilkkachu (1) Yes, Sorry about OFMT, yes, it is g not f. (2) If the result of a division require a decimal (some non-zero digit after the dot) then that result is internally converted to a float with the precision given by PREC. Any integer gets as many digits (before the dot) as it may need. Try gawk -M -vPREC=20 -vOFMT='%.30f' 'BEGIN a=22^22; b=101; print a,a/22,b/10; ' The first two numbers are integers and are exact, the last is as imprecise as given by PREC=20. Change it to PREC=200 to see the change on the last number only.(4)Yes, rounding could be adjusted.
            – Isaac
            Aug 21 at 14:53












          • 3




            Note that at least newer versions of GNU awk have features for Getting the Accuracy You Need e.g. gawk -M -v PREC="quad" -v OFMT="%.0f" 'BEGIN a = 11111111111111111111; print a, a/22; '
            – steeldriver
            Aug 17 at 0:58










          • @Isaac, well, the online manual says the default for OFMT is %.6g, so I get 5.05051e+17 for gawk -M -vPREC=quad 'BEGIN a = 11111111111111111111; print a/22; '. As for just using -M, gawk -M 'BEGIN a = 11111111111111111111; print a/22; ' gives me the wrong answer 505050505050505024. I'm not sure if that's a bug or if it automatically converts the number to a float when it's no longer an integer (a/11 is an integer, and it gives the right answer with just -M).
            – ilkkachu
            Aug 21 at 8:46











          • And there's of course ROUNDMODE for setting the rounding mode if "round to even" isn't what you want
            – ilkkachu
            Aug 21 at 8:50










          • @ilkkachu (1) Yes, Sorry about OFMT, yes, it is g not f. (2) If the result of a division require a decimal (some non-zero digit after the dot) then that result is internally converted to a float with the precision given by PREC. Any integer gets as many digits (before the dot) as it may need. Try gawk -M -vPREC=20 -vOFMT='%.30f' 'BEGIN a=22^22; b=101; print a,a/22,b/10; ' The first two numbers are integers and are exact, the last is as imprecise as given by PREC=20. Change it to PREC=200 to see the change on the last number only.(4)Yes, rounding could be adjusted.
            – Isaac
            Aug 21 at 14:53







          3




          3




          Note that at least newer versions of GNU awk have features for Getting the Accuracy You Need e.g. gawk -M -v PREC="quad" -v OFMT="%.0f" 'BEGIN a = 11111111111111111111; print a, a/22; '
          – steeldriver
          Aug 17 at 0:58




          Note that at least newer versions of GNU awk have features for Getting the Accuracy You Need e.g. gawk -M -v PREC="quad" -v OFMT="%.0f" 'BEGIN a = 11111111111111111111; print a, a/22; '
          – steeldriver
          Aug 17 at 0:58












          @Isaac, well, the online manual says the default for OFMT is %.6g, so I get 5.05051e+17 for gawk -M -vPREC=quad 'BEGIN a = 11111111111111111111; print a/22; '. As for just using -M, gawk -M 'BEGIN a = 11111111111111111111; print a/22; ' gives me the wrong answer 505050505050505024. I'm not sure if that's a bug or if it automatically converts the number to a float when it's no longer an integer (a/11 is an integer, and it gives the right answer with just -M).
          – ilkkachu
          Aug 21 at 8:46





          @Isaac, well, the online manual says the default for OFMT is %.6g, so I get 5.05051e+17 for gawk -M -vPREC=quad 'BEGIN a = 11111111111111111111; print a/22; '. As for just using -M, gawk -M 'BEGIN a = 11111111111111111111; print a/22; ' gives me the wrong answer 505050505050505024. I'm not sure if that's a bug or if it automatically converts the number to a float when it's no longer an integer (a/11 is an integer, and it gives the right answer with just -M).
          – ilkkachu
          Aug 21 at 8:46













          And there's of course ROUNDMODE for setting the rounding mode if "round to even" isn't what you want
          – ilkkachu
          Aug 21 at 8:50




          And there's of course ROUNDMODE for setting the rounding mode if "round to even" isn't what you want
          – ilkkachu
          Aug 21 at 8:50












          @ilkkachu (1) Yes, Sorry about OFMT, yes, it is g not f. (2) If the result of a division require a decimal (some non-zero digit after the dot) then that result is internally converted to a float with the precision given by PREC. Any integer gets as many digits (before the dot) as it may need. Try gawk -M -vPREC=20 -vOFMT='%.30f' 'BEGIN a=22^22; b=101; print a,a/22,b/10; ' The first two numbers are integers and are exact, the last is as imprecise as given by PREC=20. Change it to PREC=200 to see the change on the last number only.(4)Yes, rounding could be adjusted.
          – Isaac
          Aug 21 at 14:53




          @ilkkachu (1) Yes, Sorry about OFMT, yes, it is g not f. (2) If the result of a division require a decimal (some non-zero digit after the dot) then that result is internally converted to a float with the precision given by PREC. Any integer gets as many digits (before the dot) as it may need. Try gawk -M -vPREC=20 -vOFMT='%.30f' 'BEGIN a=22^22; b=101; print a,a/22,b/10; ' The first two numbers are integers and are exact, the last is as imprecise as given by PREC=20. Change it to PREC=200 to see the change on the last number only.(4)Yes, rounding could be adjusted.
          – Isaac
          Aug 21 at 14:53

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463061%2fawk-arithmetic-differs-from-expr%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay