[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] float conversion
From: |
Andrew J. Schorr |
Subject: |
Re: [bug-gawk] float conversion |
Date: |
Thu, 10 Oct 2013 13:34:04 -0400 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
Hi,
On Thu, Oct 10, 2013 at 06:45:14PM +0200, Andreas Mueller wrote:
> $ gawk 'BEGIN { for (i = -1; i <= 1; i += 0.1) { print "int("i") = " int(i)}
> }'
By default, gawk stores numbers in IEEE floating-point representation.
Does this clarify what's going on?
bash-4.1$ gawk 'BEGIN { for (i = -1; i <= 1; i += 0.1) printf "%.16g\n", i}'
-1
-0.9
-0.8
-0.7000000000000001
-0.6000000000000001
-0.5000000000000001
-0.4000000000000001
-0.3000000000000002
-0.2000000000000001
-0.1000000000000001
-1.387778780781446e-16
0.09999999999999987
0.1999999999999999
0.2999999999999999
0.3999999999999999
0.4999999999999999
0.5999999999999999
0.6999999999999998
0.7999999999999998
0.8999999999999998
0.9999999999999998
New in version 4.1 is the -M flag to use the MPFR library. You may find that
helpful. For example:
bash-4.1$ gawk -M -v PREC=100 'BEGIN { i = -1; for (j = 0; j <= 20; j++) {
print "int("i") = " int(i); i += 0.1} }'
int(-1) = -1
int(-0.9) = 0
int(-0.8) = 0
int(-0.7) = 0
int(-0.6) = 0
int(-0.5) = 0
int(-0.4) = 0
int(-0.3) = 0
int(-0.2) = 0
int(-0.1) = 0
int(9.86076e-31) = 0
int(0.1) = 0
int(0.2) = 0
int(0.3) = 0
int(0.4) = 0
int(0.5) = 0
int(0.6) = 0
int(0.7) = 0
int(0.8) = 0
int(0.9) = 0
int(1) = 1
If you want true fixed-point arithmetic, you should probably simulate it using
integers:
bash-4.1$ gawk 'BEGIN { for (i = -10; i <= 10; i += 1) { print "int("i/10") = "
int(i/10)} }'
int(-1) = -1
int(-0.9) = 0
int(-0.8) = 0
int(-0.7) = 0
int(-0.6) = 0
int(-0.5) = 0
int(-0.4) = 0
int(-0.3) = 0
int(-0.2) = 0
int(-0.1) = 0
int(0) = 0
int(0.1) = 0
int(0.2) = 0
int(0.3) = 0
int(0.4) = 0
int(0.5) = 0
int(0.6) = 0
int(0.7) = 0
int(0.8) = 0
int(0.9) = 0
int(1) = 1
Regards,
Andy