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: j . eh
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 10:05:40 -0500
User-agent: Mutt/1.4.1i

On Sun, Oct 07, 2012 at 10:39:30PM -0500, Peng Yu wrote:
> Hi,
> 
> The following "print" command only print 6 digits. I do not want to
> explicitly specify the number of digits.

Why not? You have to use printf or OFMT, gawk is not smart enough (yet)
to know how many digits are required for your computations.

> 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.
                                    ^^^^

If precision of floating-point numbers is important for your calculation,
I would advise you to learn a bit more about floating-point number
representation in hardware. Then, you will know what is wrong with
your statement above.

What you want is support for hexadecimal floating-point format (%a) for
both reading and writing, but gawk does not support it. The next
best thing is to use "%.17f" with printf/OFMT.



> I checked the
> manual, but I don't see a way to do so. Does anybody know how to do
> it? Thanks!
> 
> ~/linux/test/awk/lang/function/print$ cat main.sh
> #!/usr/bin/env bash
> 
> echo | awk 'BEGIN{print 1/3}'
> ~/linux/test/awk/lang/function/print$ ./main.sh
> 0.333333
> 
> -- 
> Regards,
> Peng



reply via email to

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