[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: gawk number to string bug
From: |
Andrew J. Schorr |
Subject: |
Re: gawk number to string bug |
Date: |
Mon, 19 Dec 2005 17:12:55 -0500 |
User-agent: |
Mutt/1.4.1i |
On Mon, Dec 19, 2005 at 11:52:42PM +0200, Eli Zaretskii wrote:
> > From: "Andrew J. Schorr" <address@hidden>
> >
> > Perhaps I'm missing something subtle, but why wouldn't something
> > as simple as the attached patch do the trick (i.e. simply convert
> > the floating point value to a long, then convert that value back
> > to floating-point, and see whether it matches the original value)?
>
> I don't think you can trust a long-to-double conversion to be safe.
> It could, for example, trigger a FP exception, whose effects are
> practically unpredictable.
Here is section 6.3.1.4 of the ANSI C99 standard:
6.3.1.4 Real floating and integer
1 When a finite value of real floating type is converted to an integer
type other than _Bool, the fractional part is discarded (i.e., the value is
truncated toward zero). If the value of the integral part cannot be
represented by the integer type, the behavior is undefined.
2 When a value of integer type is converted to a real floating type, if
the value being converted can be represented exactly in the new type, it is
unchanged. If the value being converted is in the range of values that can
be represented but cannot be represented exactly, the result is either the
nearest higher or nearest lower representable value, chosen in an
implementation-defined manner. If the value being converted is outside the
range of values that can be represented, the behavior is undefined.
There is no mention of floating-point exceptions. Are you sure
that a floating-point exception is possible here? I think that there
are many places in gawk where integer types are converted to AWKNUM (e.g.
when the results of an integer calculation, such as evaluating a logical
expression, must be converted to an AWKNUM). Are you saying that this
is unsafe? Or only dangerous for "large" values?
> I think Aharon's fix is safer. (It would be simpler to convert to
> `long long', but arguably `long long' is not yet widespread enough to
> assume its availability.)
Could be. I'm not expert on this.
Regards,
Andy
Re: gawk number to string bug, David Ellsworth, 2005/12/19