awk arithmetic differs from expr
Clash 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?
awk arithmetic expr
add a comment |Â
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?
awk arithmetic expr
add a comment |Â
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?
awk arithmetic expr
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
awk arithmetic expr
asked Aug 16 at 19:59
user305910
add a comment |Â
add a comment |Â
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.
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 forOFMT
is%.6g
, so I get5.05051e+17
forgawk -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 answer505050505050505024
. 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 courseROUNDMODE
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 isg
notf
. (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. Trygawk -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 byPREC=20
. Change it toPREC=200
to see the change on the last number only.(4)Yes, rounding could be adjusted.
â Isaac
Aug 21 at 14:53
add a comment |Â
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.
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 forOFMT
is%.6g
, so I get5.05051e+17
forgawk -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 answer505050505050505024
. 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 courseROUNDMODE
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 isg
notf
. (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. Trygawk -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 byPREC=20
. Change it toPREC=200
to see the change on the last number only.(4)Yes, rounding could be adjusted.
â Isaac
Aug 21 at 14:53
add a comment |Â
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.
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 forOFMT
is%.6g
, so I get5.05051e+17
forgawk -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 answer505050505050505024
. 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 courseROUNDMODE
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 isg
notf
. (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. Trygawk -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 byPREC=20
. Change it toPREC=200
to see the change on the last number only.(4)Yes, rounding could be adjusted.
â Isaac
Aug 21 at 14:53
add a comment |Â
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.
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.
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 forOFMT
is%.6g
, so I get5.05051e+17
forgawk -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 answer505050505050505024
. 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 courseROUNDMODE
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 isg
notf
. (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. Trygawk -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 byPREC=20
. Change it toPREC=200
to see the change on the last number only.(4)Yes, rounding could be adjusted.
â Isaac
Aug 21 at 14:53
add a comment |Â
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 forOFMT
is%.6g
, so I get5.05051e+17
forgawk -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 answer505050505050505024
. 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 courseROUNDMODE
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 isg
notf
. (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. Trygawk -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 byPREC=20
. Change it toPREC=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
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%2funix.stackexchange.com%2fquestions%2f463061%2fawk-arithmetic-differs-from-expr%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