awkとgawkの整数値の取扱の違い
1.概要
awkをよく利用します。awkで処理に困ったことがあり、gawkに変更したことを記述します。
2.詳細
基本的には同じですが、gawkの方が精度が高いようです。
(a) awk
ubuntu-22.04.5の標準のawkのversionを確認します
awk -W version
mawk 1.3.4 20200120
Copyright 2008-2019,2020, Thomas E. Dickey
Copyright 1991-1996,2014, Michael D. Brennan
random-funcs: srandom/random
regex-funcs: internal
compiled limits:
sprintf buffer 8192
maximum-integer 2147483647
数字の文字列を数値(int)に変換します
echo "1234567890" | awk '{ print int($1) }'
1234567890
echo "12345678901" | awk '{ print int($1) }'
1.23457e+10
10桁までは整数になりますが、それ以上は浮動小数点になります
(2) gawk
sudo apt install gawk
gawkのversionを確認します
$ awk -V
GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)
Copyright (C) 1989, 1991-2020 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/.
同様に数字の文字列を数値(int)に変換します
echo "1234567890" | awk '{ print int($1) }'
1234567890
echo "12345678901" | awk '{ print int($1) }'
12345678901
16桁まで正しいようですが。17桁目から怪しいですね
echo "1234567890123456" | awk '{ print int($1) }'
1234567890123456
echo "12345678901234567" | awk '{ print int($1) }'
12345678901234568
参考
awk と gawk の違い
https://uchy.me/blog/2024051302/
コメント
コメントを投稿