输入示例
42 -0.400000000000000022
我想把9'000'000'000'000'000'000
加到第一列,把30
加到第二列。
$ echo 42 -0.400000000000000022 | awk '{ $1 += 9000000000000000000; $2 += 30 } { print }'
9000000000000000000 29.6
第一列的计算错误,但第二列是正确的。
从文档和手册页中,有一个--bignum
选项,应该可以帮助我进行大整数计算。
$ echo 42 -0.400000000000000022 | awk --bignum '{ $1 += 9000000000000000000; $2 += 30 } { print }'
9000000000000000042 30
现在第一列是可以的,但第二列不是!
这是我的AWK版本,在Ubuntu 16.04上运行:
$ awk -V
GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.4, GNU MP 6.1.0)
Copyright (C) 1989, 1991-2015 Free Software Foundation.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
更奇怪的是,我在Ubuntu 16.04 docker容器中测试了这个,当使用--bignum
时,两个列的输出都是正确的。
我真的不知道该怎么解决这个问题。
我也推荐使用这种语法处理大数字。使用LC_ALL=C
:
$ echo 42 -0.400000000000000022 | LC_ALL=C awk --bignum '{ $1 += 9000000000000000000; $2 += 30 } { print }'
9000000000000000042 29.6
GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0-p13, GNU MP 6.2.0)
测试成功