awkとgawkの整数値の取扱の違い
記事の更新日:2026/09/11
1.概要
awkをよく利用します。awkで処理に困ったことがあり、gawkに変更しました。awkの中でテキストを数値にint関数で変更をしていたのですが、桁数が大きくなるとint関数を利用しているのですが、浮動小数点に変わっていまいました。これは想定外の動きでした。処理ではintである必要があったので、対処策を探してgawkに変えることにしました。gawkで当面は問題なく処理できるのですが、gawkの場合も桁数が更に大きくなるとint関数の結果が正確ではありません。浮動小数点には変わらないのですが、微妙に数値が変わります。この内容を記述します。
私の評価は以下の通りです。
(1) awkで桁数が大きな値をint関数で数値計算すると、結果が浮動小数点になります
(2) これを改善するには、awkをgawkに変えることで改善できます
(3) awkをgawkに入れ替えるとシンボリックリンクが変わるだけです
(4) gawkでも更に大きな桁数では課題が残ります
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桁までは整数になりますが、それ以上は浮動小数点になります
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
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 gawkgawkの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
3.所見
gawkを必要とする経験をkernel buildでしました。awkは標準で導入されているのですが、gawkでないと失敗してしまいます。awkとgawkはシンボリックリンクが入れ替えて置き換えられるようです。詳細は参考資料を参照願います。
本来はプログラムを作成して対処すべきことですが、awkはデータ処理に優れているので利用してしまいます。32bit整数値は、符号付きの場合は 2,147,483,647です。awkは10桁処理なので32bit整数値の壁の影響です。gawkは16桁処理できています。内部処理で工夫がされていると思います。
コメント
コメントを投稿