bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Incorrect output with OFMT="%.15e"


From: Andrew J. Schorr
Subject: Re: Incorrect output with OFMT="%.15e"
Date: Mon, 8 May 2023 11:10:57 -0400
User-agent: Mutt/1.5.21 (2010-09-15)

Hi,

On Sun, May 07, 2023 at 08:15:48PM -0700, Keith Thompson wrote:
> Ah, yes, I agree that it's not a bug, and using printf rather than
> OFMT is the right thing to do (if you want scientific notation)
> and not a workaround.
> 
> It's a little counterintuitive if you're accustomed to languages that
> distinguish between integers and non-integers by type rather than by
> value, but that's awk for you.

It is also discussed here in the gawk manual:

https://www.gnu.org/software/gawk/manual/html_node/Strings-And-Numbers.html

"As a special case, if a number is an integer, then the result of converting it 
to a string is always an integer, no matter what the value of CONVFMT may be."

You may ask why OFMT and CONVFMT are handled the same way. I can think of 3
reasons:

1. It's the same code path inside gawk.

2. Historically, there was no distinction between OFMT and CONVFMT, but it was
added to address some corner cases. You can find a discussion of this in the
Posix AWK spec.

3. The POSIX awk spec simply codifies how Unix awk has always worked, and this
is how it works.

As you say, using printf to request explicitly what you want is the best
solution.  The OFMT and CONVFMT logic is intended to do the right thing, and it
does so in the vast majority of usage cases.

Regards,
Andy



reply via email to

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