NetBSD-Bugs archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: bin/48017: awk(1) fails to treat var as integer (may be related to #47840)
The following reply was made to PR bin/48017; it has been noted by GNATS.
From: Steffen "Daode" Nurpmeso <sdaoden%gmail.com@localhost>
To: gnats-bugs%NetBSD.org@localhost
Cc:
Subject: Re: bin/48017: awk(1) fails to treat var as integer (may be related
to #47840)
Date: Wed, 10 Jul 2013 10:53:13 +0200
David Holland <dholland-bugs%netbsd.org@localhost> wrote:
| sprintf witih %d doesn't produce an number value; it produces a
| string value, which you have to coerce to a number by adding zero to
| it to get it to behave like a number.
(Adding +0 was my final solution too, because GNU awk(1) didn't
make it by the (presumably more expensive, too) sprintf("%X")
call just as all other tested awk(1)s did.)
So there is a problem with the implicit type conversion, since
echo f001 f00d |\
awk '{ a=sprintf("%d", "0x" $1); b=sprintf("%d", "0x" $2); while (a < b) {
print a; a++; }}'
works just fine?!? I think the relevant parts from POSIX are
the value of an expression shall be implicitly converted to the
type needed for the context in which it is used.
[.]
A numeric value that is exactly equal to the value of an integer
(see Concepts Derived from the ISO C Standard) shall be converted
to a string by the equivalent of a call to the sprintf function
(see String Functions) with the string "%d" as the fmt argument
and the numeric value being converted as the first and only expr
argument.
[.]
This volume of POSIX.1-2008 specifies no explicit conversions
between numbers and strings. An application can force an
expression to be treated as a number by adding zero to it, or can
force it to be treated as a string by concatenating the null
string ( "" ) to it.
[.]
A string value shall be considered a numeric string if it comes
from one of the following:
[.]
1. Field variables
[.]
8. Variable assignment from another numeric string variable
[...]
and an implementation-dependent condition corresponding to either
case (a) or (b) below is met.
[.]
b. After all the following conversions have been applied, the
resulting string would lexically be recognized as a NUMBER
token as described by the lexical conventions in Grammar :
[.]
Whether or not a string is a numeric string shall be relevant only
in contexts where that term is used in this section.
And because the `Table: Expressions in Decreasing Precedence in awk'
contains the line
expr < expr Less than Numeric None
i believe its a bug. (That hopefully gets fixed by someone who
yet has some experience with the awk codebase.)
| David A. Holland
| dholland%netbsd.org@localhost
--steffen
Home |
Main Index |
Thread Index |
Old Index