bug-gawk
[Top][All Lists]
Advanced

[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: Peng Yu
Subject: Re: [bug-gawk] How to print the just enough number digits so the machine representation of the number does not change?
Date: Mon, 8 Oct 2012 09:39:28 -0500

On Mon, Oct 8, 2012 at 2:53 AM, Eli Zaretskii <address@hidden> wrote:
>> 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.

In perl, it is not needed to specify the format explicitly. Does
anybody who are familiar with the implementation of gawk why such
feature is not available? If this feature is indeed not available, it
is better be added for convenience. After all, in a lot of situations,
people do not want to lose precession and people are lazy to specify
the format explicitly.

~/linux/test/perl/man/perlfunc/print$ ./main3.pl
0.333333333333333
~/linux/test/perl/man/perlfunc/print$ cat ./main3.pl
#!/usr/bin/env perl

use strict;
use warnings;

my $x=1/3;
print "$x\n";

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]