%.6g format not interpretted correctly

View: New views
2 Messages — Rating Filter:   Alert me  

%.6g format not interpretted correctly

by EXT-Germain, Marvin E :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


%.6g format not interpretted correctly

This also affects the "print" statement's handling of numbers, since OFMT =  %.6g.
Test program compares .6g format to .6f and .6e. It should be the same as one of them, but isn't.
Execute as:
echo howdy | awk -f testProgram

Gawk version 3.1.3

Red Hat Linux uname -r = 2.6.9-42.ELsmp

TEST PROGRAM:

{
   A = 123.456789;

   print "Value = 123.456789";

   printf "Format = .6f ==> %.6f\n", A;

   printf "Format = .6e ==> %.6e\n", A;

   printf "Format = .6g ==> %.6g\n", A;

   printf "\n";

   A = 12.3456789;

   print "Value = 12.3456789";

   printf "Format = .6f ==> %.6f\n", A;

   printf "Format = .6e ==> %.6e\n", A;

   printf "Format = .6g ==> %.6g\n", A;

   print 123.45678;
   print OFMT;
}


OUTPUT:

Value = 123.456789
Format = .6f ==> 123.456789
Format = .6e ==> 1.234568e+02
Format = .6g ==> 123.457

Value = 12.3456789
Format = .6f ==> 12.345679
Format = .6e ==> 1.234568e+01
Format = .6g ==> 12.3457




Re: %.6g format not interpretted correctly

by Eric Blake :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to EXT-Germain, Marvin E on 9/24/2009 1:15 PM:
> %.6g format not interpretted correctly
>
> This also affects the "print" statement's handling of numbers, since OFMT =  %.6g.
> Test program compares .6g format to .6f and .6e. It should be the same as one of them,

Not a bug.  POSIX is explicit that in printf formatting, for .6g, 6
specified the number of significant digits; but for .6f and .6e it
specifies the number of digits after the decimal (which is not necessarily
the number of significant digits).

- --
Don't work too hard, make some time for fun as well!

Eric Blake             ebb9@...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkq8QUcACgkQ84KuGfSFAYAi0wCeLn0fxqYmGfYNB+ek+SIbiJmn
PxQAnRxLaIslkqoqyRTCTbGRb67gZ30q
=bys3
-----END PGP SIGNATURE-----