sxiong
July 31, 2014, 11:51am
1
Hi All,
I would like to set decimal point to 16 in the following bash script but it has syntax error at }:
awk '{printf"%.16e", (a<500,a++,$1/(a*1.1212121212121229e-02))}' input.dat >output.dat
How may I set it in the correct way please? Thank you very much!
RudiC
July 31, 2014, 12:10pm
2
I'm pretty sure your construct (a<500,a++,$1/(a*1.1212121212121229e-02)) is not understood by awk. What be your intention?
Please explain in English what this awk script is supposed to do. I can make a few wild guesses, but none of them make any sense to me.
Then note that awk uses double precision floating point arithmetic (which gives you about 15-17 significant digits on individual values). Rounding errors may significantly reduce the precision of your results depending on the ranges of values being multiplied and divided.
What does the data in input.dat look like?
sxiong
July 31, 2014, 12:26pm
4
Thanks for your kind replies. I want to divide the data in my input.dat by a times a constant as a goes from 1 to 499. I used
awk '{print (a<500,a++,$1/(a*1.121212121212121229e-02))}' input.dat >output.dat
and got the desired values with 6 digits although the code is far from perfect. Here is part of the input:
1.0805574801665273e+01
1.0631847858835727e+01
1.0461243720756954e+01
1.0293701484862595e+01
1.0129161486782039e+01
and output:
1 0 963.74
1 1 474.123
1 2 311.01
1 3 229.522
1 4 180.682
So I'd like to find a way to set the decimal point of output data to 16. Would you please shed light on the problem? Thanks a lot!
Print two integers and one floating point?
awk '{printf "%d %d %.16f\n",(a<500),a++,$1/(a*1.121212121212121229e-02)}' input.dat
You get exponential notation if you change f to e .
sxiong
July 31, 2014, 12:44pm
6
That is exactly what I want. Thank you very much! I forgot I have three columns in the output... Thanks again!
RudiC
July 31, 2014, 12:49pm
7
Please use code tags as required by forum rules!
How about
awk '{print a<500,a++,$1/(a*1.121212121212121229e-02)}' OFMT="%.16g" file
1 0 963.7404552836596
1 1 474.1229450561879
1 2 311.0099484549365
1 3 229.5217222976119
1 4 180.6823400344904