算术运算后的函数应用

Eli*_*eth 1 awk

如何编写程序应首先计算然后应用t函数?我希望得到六个小数位的结果,而不是圆形.谢谢.

  /^$/ { flag=0; next; }
/D Format/ { flag=0; next; }
/F Format/ { flag=1; next; }
/^ 9 / { print t($5) "\n" t($6);  } 
/^10 / { print t($5) "\n" t($6);  } 
/^11 / { print t($5*180/3.141592653589) "\n" t($6*180/3.141592653589);  }
/^15 / { print t($5*100) "\n" t($6*100);  }
/^16 / { print t($5) "\n" t($6);  } 
/^20 / { print t($5*10) "\n" t($6*10);  }
/^23 / { print t($5) "\n" t($6);  } 
/^24 / { print t($5) "\n" t($6);  }
function t(n, s) {
s=index(n,".");
return (s ? substr(n,1,s+6) : n);
}
Run Code Online (Sandbox Code Playgroud)

输入是

 SUM OF ABSOLUTE VALUES OF CHECKS IS 0.844670D-13


                      Input-Output in F Format

No.  Curve    Input Param.        Correction     Output Param.    Standard Deviation
 9      0     43.8999000000     -0.2148692026     43.6850307974      0.1066086900
10      0      0.0883000000     -0.0081173828      0.0801826172      0.0006755954
11      0      2.5816650000      0.1530838229      2.7347488229      0.0114687081
15      0      0.2175000000      0.0018561462      0.2193561462      0.0017699976
16      0     80.4198910000      3.4449399961     83.8648309961      0.1158732928
20      0      1.9424000000      0.3078499311      2.2502499311      0.0047924544
23      0      3.5047300000      0.4315780848      3.9363080848      0.0052905759
24      0      5.5942300000      1.8976306735      7.4918606735      0.0092102115
26      0  54804.4046000000     -0.0029799077  54804.4016200923      0.0006133608


                      Input-Output in D Format

No.  Curve    Input Param.        Correction     Output Param.    Standard Deviation
 9      0  0.4389990000D+02 -0.2148692026D+00  0.4368503080D+02  0.1066086900D+00
10      0  0.8830000000D-01 -0.8117382819D-02  0.8018261718D-01  0.6755954153D-03
11      0  0.2581665000D+01  0.1530838229D+00  0.2734748823D+01  0.1146870812D-01
15      0  0.2175000000D+00  0.1856146162D-02  0.2193561462D+00  0.1769997586D-02
16      0  0.8041989100D+02  0.3444939996D+01  0.8386483100D+02  0.1158732928D+00
20      0  0.1942400000D+01  0.3078499311D+00  0.2250249931D+01  0.4792454358D-02
23      0  0.3504730000D+01  0.4315780848D+00  0.3936308085D+01  0.5290575930D-02
24      0  0.5594230000D+01  0.1897630674D+01  0.7491860674D+01  0.9210211480D-02
26      0  0.5480440460D+05 -0.2979907673D-02  0.5480440162D+05  0.6133608199D-03
Run Code Online (Sandbox Code Playgroud)

我想得到像输出

43.685030
0.106608
0.080182
0.000675
156.68965
0.657068
21.935614
0.176999
83.864830
0.115873
22.502499
0.047924
3.936308
0.005290
7.491860
0.009210
Run Code Online (Sandbox Code Playgroud)

这是第一个表中第5和第6列的数字,但第26行除外.

但我得到的不是这些数字(错误是格式 - 2个小数位而不是6个并且从两个表中打印数字而不是一个)

43.685030
0.106608
0.080182
0.000675
156.69
0.657109
21.9356
0.177
83.864830
0.115873
22.5025
0.047924
3.936308
0.005290
7.491860
0.009210
0.436850
0.106608
0.801826
0.675595
15.669
6.57109
21.9356
17.7
0.838648
0.115873
2.25025
4.79245
0.393630
0.529057
0.749186
0.921021
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ed *_*ton 5

我在这里猜测 - 你试图解决的问题是你期望从这个计算中获得更高的精度吗?

$ cat tst.awk
BEGIN { print t(2.7347488229 * 180 / 3.141592653589) }
function t(n,     s) {
    s=index(n,".")
    return (s ? substr(n,1,s+6) : n)
}

$ awk -f tst.awk
156.69
Run Code Online (Sandbox Code Playgroud)

如果是这样,那么请注意将CONVFMT设置为高于截断后要结束的精度的效果:

$ cat tst.awk
BEGIN { print t(2.7347488229 * 180 / 3.141592653589) }
function t(n,     s) {
    CONVFMT="%0.17f"
    s=index(n,".")
    return (s ? substr(n,1,s+6) : n)
}

$ awk -f tst.awk
156.689565
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为substr()的效果是将数字转换为字符串,此时应用CONVFMT的值,请参阅https://www.gnu.org/software/gawk/manual/gawk.html#Strings - 数字.

您可以在BEGIN部分设置CONVFMT,但我将其设置在需要其效果的位置旁边,因此如果您愿意,可以调整以不影响脚本的其余部分:

$ cat tst.awk
BEGIN { print t(2.7347488229 * 180 / 3.141592653589) }
function t(n,     s, origConvfmt) {
    origConvfmt=CONVFMT
    CONVFMT="%0.17f"
    s=index(n,".")
    return (s ? substr(n,1,s+6) : n)
    CONVFMT=origConvfmt
}

$ awk -f tst.awk
156.689565
Run Code Online (Sandbox Code Playgroud)

最后 - 为了摆脱冗余,请考虑将脚本重写为:

$ cat tst.awk
BEGIN { CONVFMT="%0.17f" }

!NF { flag=0 }
$NF == "Format" { flag=($NF-1 == "F" ? 1 : 0) }

$1 !~ /^[0-9]+$/ { next }

$1 == 11 { prt(180,3.141592653589); next }
$1 == 15 { prt(100,1); next }
$1 == 20 { prt(10,1); next }
$1 != 26 { prt(1,1) }

function prt(mult, div) {
    print trunc($5 * mult / div) ORS trunc($6 * mult / div)
}

function trunc(n,       s) {
    s=index(n,".")
    return (s ? substr(n,1,s+6) : n)
}
Run Code Online (Sandbox Code Playgroud)

编辑1 鉴于您更新的要求不打印第二个表的内容:

$ cat tst.awk
BEGIN {  CONVFMT="%0.17f" }

/D Format/ { exit }

$1 !~ /^[0-9]+$/ { next }

$1 == 11 { prt(180,3.141592653589); next }
$1 == 15 { prt(100,1); next }
$1 == 20 { prt(10,1); next }
$1 != 26 { prt(1,1) }

function prt(mult, div) {
    print trunc($5 * mult / div) ORS trunc($6 * mult / div)
}

function trunc(n,       s) {
    s=index(n,".")
    return (s ? substr(n,1,s+6) : n)
}
Run Code Online (Sandbox Code Playgroud)

.

$ awk -f tst.awk file
43.685030
0.106608
0.080182
0.000675
156.689565
0.657108
21.935614
0.176999
83.864830
0.115873
22.502499
0.047924
3.936308
0.005290
7.491860
0.009210
Run Code Online (Sandbox Code Playgroud)