0

Example input

42 -0.400000000000000022

I want to add 9'000'000'000'000'000'000 to the 1st column, and add 30 to the 2nd column.

$ echo 42 -0.400000000000000022 | awk '{ $1 += 9000000000000000000; $2 += 30 } { print }'
9000000000000000000 29.6

Computation for the 1st column is wrong, but the 2nd column is OK.

From the documentation and the man page, there's a --bignum option which should help me for the big integer computation.

$ echo 42 -0.400000000000000022 | awk --bignum '{ $1 += 9000000000000000000; $2 += 30 } { print }' 
9000000000000000042 30

Now the 1st column is OK, but the 2nd one isn't!

Here's my AWK version, running on 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/.

What's even weirder is that I tested this inside an Ubuntu 16.04 docker container, and the output is correct for both column when using --bignum.

I actually don't know what to look for to fix this.

Unda
  • 1,827
  • 3
  • 23
  • 35
  • 3
    The sounds like a bug. gawk 4.1.3 is 6 years old and has at least 1 known bug in this area (see https://lists.gnu.org/archive/html/bug-gawk/2018-09/msg00009.html), can you update to the current version, 5.1.0? I notice though that you used `'` (apostrophe) rather than `,` (comma) as the thousands separator in the text in your question ("I want to add `9'00...`") so you must not be in a C/POSIX locale - idk if it'll have any effect but try setting `export LC_ALL=C` before your calculation to see if that makes any difference. – Ed Morton Aug 11 '21 at 13:35
  • 3
    @EdMorton I used `'` as the thousands separator only to make the question clearer, but I'm not using it anywhere in my scripts. But good catch anyway ! I had all my env variables using `fr_FR.UTF-8`, and after changing them to `en_US.UTF-8`, the output is fine ! – Unda Aug 11 '21 at 13:35
  • 1
    Great. I'd still recommend you update your awk version though to get that bug fix plus others plus additional enhancements. I'd also recommend using `LC_ALL=C` (or `LC_ALL=POSIX`) to avoid any other surprises. – Ed Morton Aug 11 '21 at 13:41

1 Answers1

0

I also recommend this syntax with big numbers. Use LC_ALL=C:

$ echo 42 -0.400000000000000022 | LC_ALL=C awk --bignum '{ $1 += 9000000000000000000; $2 += 30 } { print }'
9000000000000000042 29.6

Successfully tested with GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0-p13, GNU MP 6.2.0)

Arnaud Valmary
  • 2,039
  • 9
  • 14