awk high precision arithmetic
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
I am looking for a way to tell awk to do high-precision arithmetic in a substitution operation. This involves, reading a field from a file and substituting it with a 1% increment on that value. However, I am losing precision there. Here is a simplified reproduction of the problem:
$ echo 0.4970436865354813 | awk 'gsub($1, $1*1.1); print'
0.546748
Here, I have a 16 digit after decimal precision but awk gives only six. Using printf, I am getting the same result:
$ echo 0.4970436865354813 | awk 'gsub($1, $1*1.1); printf("%.16Gn", $1)'
0.546748
Any suggestions on to how to get the desired precision?
awk arithmetic floating-point
add a comment |Â
up vote
7
down vote
favorite
I am looking for a way to tell awk to do high-precision arithmetic in a substitution operation. This involves, reading a field from a file and substituting it with a 1% increment on that value. However, I am losing precision there. Here is a simplified reproduction of the problem:
$ echo 0.4970436865354813 | awk 'gsub($1, $1*1.1); print'
0.546748
Here, I have a 16 digit after decimal precision but awk gives only six. Using printf, I am getting the same result:
$ echo 0.4970436865354813 | awk 'gsub($1, $1*1.1); printf("%.16Gn", $1)'
0.546748
Any suggestions on to how to get the desired precision?
awk arithmetic floating-point
Perhaps awk has higher resolution but it's just your output formatting is truncating. Use printf.
â dubiousjim
Nov 28 '12 at 15:25
No changes in result value after using printf. Question edited accordingly.
â Ketan
Nov 28 '12 at 15:32
As @manatwork has pointed out, thatgsub
is unnecessary. The problem isgsub
works on strings, not numbers, so a conversion is done first usingCONVFMT
, and the default value for that is%.6g
.
â jw013
Nov 28 '12 at 15:46
@jw013, As I mentioned in the question, my original problem requires gsub since I need to substitute a number with a 1% increment. Agreed, in the simplified example, it is not required.
â Ketan
Nov 28 '12 at 15:48
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I am looking for a way to tell awk to do high-precision arithmetic in a substitution operation. This involves, reading a field from a file and substituting it with a 1% increment on that value. However, I am losing precision there. Here is a simplified reproduction of the problem:
$ echo 0.4970436865354813 | awk 'gsub($1, $1*1.1); print'
0.546748
Here, I have a 16 digit after decimal precision but awk gives only six. Using printf, I am getting the same result:
$ echo 0.4970436865354813 | awk 'gsub($1, $1*1.1); printf("%.16Gn", $1)'
0.546748
Any suggestions on to how to get the desired precision?
awk arithmetic floating-point
I am looking for a way to tell awk to do high-precision arithmetic in a substitution operation. This involves, reading a field from a file and substituting it with a 1% increment on that value. However, I am losing precision there. Here is a simplified reproduction of the problem:
$ echo 0.4970436865354813 | awk 'gsub($1, $1*1.1); print'
0.546748
Here, I have a 16 digit after decimal precision but awk gives only six. Using printf, I am getting the same result:
$ echo 0.4970436865354813 | awk 'gsub($1, $1*1.1); printf("%.16Gn", $1)'
0.546748
Any suggestions on to how to get the desired precision?
awk arithmetic floating-point
awk arithmetic floating-point
edited Nov 28 '12 at 23:40
Gilles
510k12010081538
510k12010081538
asked Nov 28 '12 at 15:19
Ketan
5,43942741
5,43942741
Perhaps awk has higher resolution but it's just your output formatting is truncating. Use printf.
â dubiousjim
Nov 28 '12 at 15:25
No changes in result value after using printf. Question edited accordingly.
â Ketan
Nov 28 '12 at 15:32
As @manatwork has pointed out, thatgsub
is unnecessary. The problem isgsub
works on strings, not numbers, so a conversion is done first usingCONVFMT
, and the default value for that is%.6g
.
â jw013
Nov 28 '12 at 15:46
@jw013, As I mentioned in the question, my original problem requires gsub since I need to substitute a number with a 1% increment. Agreed, in the simplified example, it is not required.
â Ketan
Nov 28 '12 at 15:48
add a comment |Â
Perhaps awk has higher resolution but it's just your output formatting is truncating. Use printf.
â dubiousjim
Nov 28 '12 at 15:25
No changes in result value after using printf. Question edited accordingly.
â Ketan
Nov 28 '12 at 15:32
As @manatwork has pointed out, thatgsub
is unnecessary. The problem isgsub
works on strings, not numbers, so a conversion is done first usingCONVFMT
, and the default value for that is%.6g
.
â jw013
Nov 28 '12 at 15:46
@jw013, As I mentioned in the question, my original problem requires gsub since I need to substitute a number with a 1% increment. Agreed, in the simplified example, it is not required.
â Ketan
Nov 28 '12 at 15:48
Perhaps awk has higher resolution but it's just your output formatting is truncating. Use printf.
â dubiousjim
Nov 28 '12 at 15:25
Perhaps awk has higher resolution but it's just your output formatting is truncating. Use printf.
â dubiousjim
Nov 28 '12 at 15:25
No changes in result value after using printf. Question edited accordingly.
â Ketan
Nov 28 '12 at 15:32
No changes in result value after using printf. Question edited accordingly.
â Ketan
Nov 28 '12 at 15:32
As @manatwork has pointed out, that
gsub
is unnecessary. The problem is gsub
works on strings, not numbers, so a conversion is done first using CONVFMT
, and the default value for that is %.6g
.â jw013
Nov 28 '12 at 15:46
As @manatwork has pointed out, that
gsub
is unnecessary. The problem is gsub
works on strings, not numbers, so a conversion is done first using CONVFMT
, and the default value for that is %.6g
.â jw013
Nov 28 '12 at 15:46
@jw013, As I mentioned in the question, my original problem requires gsub since I need to substitute a number with a 1% increment. Agreed, in the simplified example, it is not required.
â Ketan
Nov 28 '12 at 15:48
@jw013, As I mentioned in the question, my original problem requires gsub since I need to substitute a number with a 1% increment. Agreed, in the simplified example, it is not required.
â Ketan
Nov 28 '12 at 15:48
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
10
down vote
accepted
$ echo 0.4970436865354813 | awk -v CONVFMT=%.17g 'gsub($1, $1*1.1); print'
0.54674805518902947
Or rather here:
$ echo 0.4970436865354813 | awk 'printf "%.17gn", $1*1.1'
0.54674805518902947
is probably the best you can achieve. Use bc
instead for arbitrary precision.
$ echo '0.4970436865354813 * 1.1' | bc -l
.54674805518902943
If you want arbitrary precision inAWK
you can use the-M
flag and set thePREC
value to a large number
â Robert Benson
Apr 3 at 20:09
2
@RobertBenson, only with GNU awk and only with recent versions (4.1 or above, so not at the time that answer was written) and only when MPFR was enabled at compile time though.
â Stéphane Chazelas
Apr 3 at 20:37
add a comment |Â
up vote
0
down vote
For higher precision with (GNU) awk (with bignum compiled in) use:
$ echo '0.4970436865354813' | awk -M -v PREC=100 'printf("%.18fn", $1)'
0.497043686535481300
The PREC=100 means 100 bits instead of the default 53 bits.
If that awk is not available, use bc
$ echo '0.4970436865354813*1.1' | bc -l
.54674805518902943
Or you will need to learn to live with the inherent imprecision of floats.
In your original lines there are several issues:
- A factor of 1.1 is 10% increase, not 1% (should be a 1.01 multiplier). I'll use 10%.
The conversion format from a string to a (floating) number is given by CONVFMT. Its default value is
%.6g
. That limits the values to 6 decimal digits (after the dot). That is applied to the result of the gsub change of$1
.$ a='0.4970436865354813'
$ echo "$a" | awk 'printf("%.16fn", $1*1.1)'
0.5467480551890295
$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.16fn", $1)'
0.5467480000000000The printf format
g
removes trailing zeros:$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.16gn", $1)'
0.546748
$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.17gn", $1)'
0.54674800000000001Both issues could be solved with:
$ echo "$a" | awk 'printf("%.17gn", $1*1.1)'
0.54674805518902947Or
$ echo "$a" | awk -v CONVFMT=%.30g 'gsub($1, $1*1.1); printf("%.17fn", $1)'
0.54674805518902947
But don't get the idea that this means higher precision. The internal number representation is still a float in double size. That means 53 bits of precision and with that you could only be sure of 15 correct decimal digits, even if many times up to 17 digits look correct. That's a mirage.
$ echo "$a" | awk -v CONVFMT=%.30g 'gsub($1, $1*1.1; printf("%.30fn", $1)'
0.546748055189029469325134868996
The correct value is:
$ echo "scale=18; 0.4970436865354813 * 1.1" | bc
.54674805518902943
Which could be also calculated with (GNU) awk if the bignum library has been compiled in:
$ echo "$a" | awk -M -v PREC=100 -v CONVFMT=%.30g 'printf("%.30fn", $1)'
0.497043686535481300000000000000
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
$ echo 0.4970436865354813 | awk -v CONVFMT=%.17g 'gsub($1, $1*1.1); print'
0.54674805518902947
Or rather here:
$ echo 0.4970436865354813 | awk 'printf "%.17gn", $1*1.1'
0.54674805518902947
is probably the best you can achieve. Use bc
instead for arbitrary precision.
$ echo '0.4970436865354813 * 1.1' | bc -l
.54674805518902943
If you want arbitrary precision inAWK
you can use the-M
flag and set thePREC
value to a large number
â Robert Benson
Apr 3 at 20:09
2
@RobertBenson, only with GNU awk and only with recent versions (4.1 or above, so not at the time that answer was written) and only when MPFR was enabled at compile time though.
â Stéphane Chazelas
Apr 3 at 20:37
add a comment |Â
up vote
10
down vote
accepted
$ echo 0.4970436865354813 | awk -v CONVFMT=%.17g 'gsub($1, $1*1.1); print'
0.54674805518902947
Or rather here:
$ echo 0.4970436865354813 | awk 'printf "%.17gn", $1*1.1'
0.54674805518902947
is probably the best you can achieve. Use bc
instead for arbitrary precision.
$ echo '0.4970436865354813 * 1.1' | bc -l
.54674805518902943
If you want arbitrary precision inAWK
you can use the-M
flag and set thePREC
value to a large number
â Robert Benson
Apr 3 at 20:09
2
@RobertBenson, only with GNU awk and only with recent versions (4.1 or above, so not at the time that answer was written) and only when MPFR was enabled at compile time though.
â Stéphane Chazelas
Apr 3 at 20:37
add a comment |Â
up vote
10
down vote
accepted
up vote
10
down vote
accepted
$ echo 0.4970436865354813 | awk -v CONVFMT=%.17g 'gsub($1, $1*1.1); print'
0.54674805518902947
Or rather here:
$ echo 0.4970436865354813 | awk 'printf "%.17gn", $1*1.1'
0.54674805518902947
is probably the best you can achieve. Use bc
instead for arbitrary precision.
$ echo '0.4970436865354813 * 1.1' | bc -l
.54674805518902943
$ echo 0.4970436865354813 | awk -v CONVFMT=%.17g 'gsub($1, $1*1.1); print'
0.54674805518902947
Or rather here:
$ echo 0.4970436865354813 | awk 'printf "%.17gn", $1*1.1'
0.54674805518902947
is probably the best you can achieve. Use bc
instead for arbitrary precision.
$ echo '0.4970436865354813 * 1.1' | bc -l
.54674805518902943
edited Aug 21 at 12:27
Isaac
7,1311835
7,1311835
answered Nov 28 '12 at 15:41
Stéphane Chazelas
285k53525864
285k53525864
If you want arbitrary precision inAWK
you can use the-M
flag and set thePREC
value to a large number
â Robert Benson
Apr 3 at 20:09
2
@RobertBenson, only with GNU awk and only with recent versions (4.1 or above, so not at the time that answer was written) and only when MPFR was enabled at compile time though.
â Stéphane Chazelas
Apr 3 at 20:37
add a comment |Â
If you want arbitrary precision inAWK
you can use the-M
flag and set thePREC
value to a large number
â Robert Benson
Apr 3 at 20:09
2
@RobertBenson, only with GNU awk and only with recent versions (4.1 or above, so not at the time that answer was written) and only when MPFR was enabled at compile time though.
â Stéphane Chazelas
Apr 3 at 20:37
If you want arbitrary precision in
AWK
you can use the -M
flag and set the PREC
value to a large numberâ Robert Benson
Apr 3 at 20:09
If you want arbitrary precision in
AWK
you can use the -M
flag and set the PREC
value to a large numberâ Robert Benson
Apr 3 at 20:09
2
2
@RobertBenson, only with GNU awk and only with recent versions (4.1 or above, so not at the time that answer was written) and only when MPFR was enabled at compile time though.
â Stéphane Chazelas
Apr 3 at 20:37
@RobertBenson, only with GNU awk and only with recent versions (4.1 or above, so not at the time that answer was written) and only when MPFR was enabled at compile time though.
â Stéphane Chazelas
Apr 3 at 20:37
add a comment |Â
up vote
0
down vote
For higher precision with (GNU) awk (with bignum compiled in) use:
$ echo '0.4970436865354813' | awk -M -v PREC=100 'printf("%.18fn", $1)'
0.497043686535481300
The PREC=100 means 100 bits instead of the default 53 bits.
If that awk is not available, use bc
$ echo '0.4970436865354813*1.1' | bc -l
.54674805518902943
Or you will need to learn to live with the inherent imprecision of floats.
In your original lines there are several issues:
- A factor of 1.1 is 10% increase, not 1% (should be a 1.01 multiplier). I'll use 10%.
The conversion format from a string to a (floating) number is given by CONVFMT. Its default value is
%.6g
. That limits the values to 6 decimal digits (after the dot). That is applied to the result of the gsub change of$1
.$ a='0.4970436865354813'
$ echo "$a" | awk 'printf("%.16fn", $1*1.1)'
0.5467480551890295
$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.16fn", $1)'
0.5467480000000000The printf format
g
removes trailing zeros:$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.16gn", $1)'
0.546748
$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.17gn", $1)'
0.54674800000000001Both issues could be solved with:
$ echo "$a" | awk 'printf("%.17gn", $1*1.1)'
0.54674805518902947Or
$ echo "$a" | awk -v CONVFMT=%.30g 'gsub($1, $1*1.1); printf("%.17fn", $1)'
0.54674805518902947
But don't get the idea that this means higher precision. The internal number representation is still a float in double size. That means 53 bits of precision and with that you could only be sure of 15 correct decimal digits, even if many times up to 17 digits look correct. That's a mirage.
$ echo "$a" | awk -v CONVFMT=%.30g 'gsub($1, $1*1.1; printf("%.30fn", $1)'
0.546748055189029469325134868996
The correct value is:
$ echo "scale=18; 0.4970436865354813 * 1.1" | bc
.54674805518902943
Which could be also calculated with (GNU) awk if the bignum library has been compiled in:
$ echo "$a" | awk -M -v PREC=100 -v CONVFMT=%.30g 'printf("%.30fn", $1)'
0.497043686535481300000000000000
add a comment |Â
up vote
0
down vote
For higher precision with (GNU) awk (with bignum compiled in) use:
$ echo '0.4970436865354813' | awk -M -v PREC=100 'printf("%.18fn", $1)'
0.497043686535481300
The PREC=100 means 100 bits instead of the default 53 bits.
If that awk is not available, use bc
$ echo '0.4970436865354813*1.1' | bc -l
.54674805518902943
Or you will need to learn to live with the inherent imprecision of floats.
In your original lines there are several issues:
- A factor of 1.1 is 10% increase, not 1% (should be a 1.01 multiplier). I'll use 10%.
The conversion format from a string to a (floating) number is given by CONVFMT. Its default value is
%.6g
. That limits the values to 6 decimal digits (after the dot). That is applied to the result of the gsub change of$1
.$ a='0.4970436865354813'
$ echo "$a" | awk 'printf("%.16fn", $1*1.1)'
0.5467480551890295
$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.16fn", $1)'
0.5467480000000000The printf format
g
removes trailing zeros:$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.16gn", $1)'
0.546748
$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.17gn", $1)'
0.54674800000000001Both issues could be solved with:
$ echo "$a" | awk 'printf("%.17gn", $1*1.1)'
0.54674805518902947Or
$ echo "$a" | awk -v CONVFMT=%.30g 'gsub($1, $1*1.1); printf("%.17fn", $1)'
0.54674805518902947
But don't get the idea that this means higher precision. The internal number representation is still a float in double size. That means 53 bits of precision and with that you could only be sure of 15 correct decimal digits, even if many times up to 17 digits look correct. That's a mirage.
$ echo "$a" | awk -v CONVFMT=%.30g 'gsub($1, $1*1.1; printf("%.30fn", $1)'
0.546748055189029469325134868996
The correct value is:
$ echo "scale=18; 0.4970436865354813 * 1.1" | bc
.54674805518902943
Which could be also calculated with (GNU) awk if the bignum library has been compiled in:
$ echo "$a" | awk -M -v PREC=100 -v CONVFMT=%.30g 'printf("%.30fn", $1)'
0.497043686535481300000000000000
add a comment |Â
up vote
0
down vote
up vote
0
down vote
For higher precision with (GNU) awk (with bignum compiled in) use:
$ echo '0.4970436865354813' | awk -M -v PREC=100 'printf("%.18fn", $1)'
0.497043686535481300
The PREC=100 means 100 bits instead of the default 53 bits.
If that awk is not available, use bc
$ echo '0.4970436865354813*1.1' | bc -l
.54674805518902943
Or you will need to learn to live with the inherent imprecision of floats.
In your original lines there are several issues:
- A factor of 1.1 is 10% increase, not 1% (should be a 1.01 multiplier). I'll use 10%.
The conversion format from a string to a (floating) number is given by CONVFMT. Its default value is
%.6g
. That limits the values to 6 decimal digits (after the dot). That is applied to the result of the gsub change of$1
.$ a='0.4970436865354813'
$ echo "$a" | awk 'printf("%.16fn", $1*1.1)'
0.5467480551890295
$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.16fn", $1)'
0.5467480000000000The printf format
g
removes trailing zeros:$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.16gn", $1)'
0.546748
$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.17gn", $1)'
0.54674800000000001Both issues could be solved with:
$ echo "$a" | awk 'printf("%.17gn", $1*1.1)'
0.54674805518902947Or
$ echo "$a" | awk -v CONVFMT=%.30g 'gsub($1, $1*1.1); printf("%.17fn", $1)'
0.54674805518902947
But don't get the idea that this means higher precision. The internal number representation is still a float in double size. That means 53 bits of precision and with that you could only be sure of 15 correct decimal digits, even if many times up to 17 digits look correct. That's a mirage.
$ echo "$a" | awk -v CONVFMT=%.30g 'gsub($1, $1*1.1; printf("%.30fn", $1)'
0.546748055189029469325134868996
The correct value is:
$ echo "scale=18; 0.4970436865354813 * 1.1" | bc
.54674805518902943
Which could be also calculated with (GNU) awk if the bignum library has been compiled in:
$ echo "$a" | awk -M -v PREC=100 -v CONVFMT=%.30g 'printf("%.30fn", $1)'
0.497043686535481300000000000000
For higher precision with (GNU) awk (with bignum compiled in) use:
$ echo '0.4970436865354813' | awk -M -v PREC=100 'printf("%.18fn", $1)'
0.497043686535481300
The PREC=100 means 100 bits instead of the default 53 bits.
If that awk is not available, use bc
$ echo '0.4970436865354813*1.1' | bc -l
.54674805518902943
Or you will need to learn to live with the inherent imprecision of floats.
In your original lines there are several issues:
- A factor of 1.1 is 10% increase, not 1% (should be a 1.01 multiplier). I'll use 10%.
The conversion format from a string to a (floating) number is given by CONVFMT. Its default value is
%.6g
. That limits the values to 6 decimal digits (after the dot). That is applied to the result of the gsub change of$1
.$ a='0.4970436865354813'
$ echo "$a" | awk 'printf("%.16fn", $1*1.1)'
0.5467480551890295
$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.16fn", $1)'
0.5467480000000000The printf format
g
removes trailing zeros:$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.16gn", $1)'
0.546748
$ echo "$a" | awk 'gsub($1, $1*1.1); printf("%.17gn", $1)'
0.54674800000000001Both issues could be solved with:
$ echo "$a" | awk 'printf("%.17gn", $1*1.1)'
0.54674805518902947Or
$ echo "$a" | awk -v CONVFMT=%.30g 'gsub($1, $1*1.1); printf("%.17fn", $1)'
0.54674805518902947
But don't get the idea that this means higher precision. The internal number representation is still a float in double size. That means 53 bits of precision and with that you could only be sure of 15 correct decimal digits, even if many times up to 17 digits look correct. That's a mirage.
$ echo "$a" | awk -v CONVFMT=%.30g 'gsub($1, $1*1.1; printf("%.30fn", $1)'
0.546748055189029469325134868996
The correct value is:
$ echo "scale=18; 0.4970436865354813 * 1.1" | bc
.54674805518902943
Which could be also calculated with (GNU) awk if the bignum library has been compiled in:
$ echo "$a" | awk -M -v PREC=100 -v CONVFMT=%.30g 'printf("%.30fn", $1)'
0.497043686535481300000000000000
answered Aug 20 at 22:07
Isaac
7,1311835
7,1311835
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%2funix.stackexchange.com%2fquestions%2f57006%2fawk-high-precision-arithmetic%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
Perhaps awk has higher resolution but it's just your output formatting is truncating. Use printf.
â dubiousjim
Nov 28 '12 at 15:25
No changes in result value after using printf. Question edited accordingly.
â Ketan
Nov 28 '12 at 15:32
As @manatwork has pointed out, that
gsub
is unnecessary. The problem isgsub
works on strings, not numbers, so a conversion is done first usingCONVFMT
, and the default value for that is%.6g
.â jw013
Nov 28 '12 at 15:46
@jw013, As I mentioned in the question, my original problem requires gsub since I need to substitute a number with a 1% increment. Agreed, in the simplified example, it is not required.
â Ketan
Nov 28 '12 at 15:48