[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] How to print the just enough number digits so the machine
From: |
Eli Zaretskii |
Subject: |
Re: [bug-gawk] How to print the just enough number digits so the machine representation of the number does not change? |
Date: |
Mon, 08 Oct 2012 09:53:15 +0200 |
> Date: Sun, 7 Oct 2012 22:39:30 -0500
> From: Peng Yu <address@hidden>
> The following "print" command only print 6 digits. I do not want to
> explicitly specify the number of digits. But I want to print just
> enough number of digits (I think that it probably should be
> 0.3333333333333333148296) so that when the number is read back into
> awk, it is still the same in the machine representation. I checked the
> manual, but I don't see a way to do so. Does anybody know how to do
> it? Thanks!
This should set you up:
BEGIN { printf "%.20g\n", 1/3 }
Explanation of the 20 part:
. Numbers in Gawk are represented as the 'double' data type, which
has at most 17 significant digits.
. You need 3 more places, one for the decimal, the other for the
sign, and one more for the leading zero, if needed. This gives
20.
. The %g format displays the shorter of %f and %e, and always
displays the number of significant digits as instructed, so use
that.