Re: [dev] dwm-5.6

From: Szabolcs Nagy <>
Date: Mon, 20 Jul 2009 23:29:32 +0200

On 7/20/09, sqweek <> wrote:
>> for example
>> (double)atoi("1")/atoi("3") == (double)atoi("1")/atoi("3")
>> gives false with gcc on my x86 machine eventhough the two expressions
>> are semantically equivalent
> Interesting indeed, I get the same result here. However, if you store
> the division results in variables[1] you do get equality... strange.
> Maybe related to the 80-bit floating point format used internally on
> the cpu?

gcc (without optimization) generates different instructions for the
two divisions, the first one uses 80bit float registers, the second
one is not

if you bind the result to variables then the difference is obviously
gone however

  if ( (a = (double)atoi("1")/atoi("3"))
   == (b = (double)atoi("1")/atoi("3")) )
      puts("not eq");

  if (a == b)
      puts("not eq");

will print
  not eq
with old gcc (< 4), which is even more surprising behaviour

> I wonder if this is a gcc bug - the above expression gives me true in
> the plan 9 compilers. Longstanding one though, I see it in gcc 3.3.6,
> 3.4.6 and 4.3.2 (ie, every compiler on my system).

in c89 and the original c99 standard this is not a bug, but
'unspecified behaviour', as the standard tries to pose very limited
requirements on floating point arithmetic, so division with different
precisions is allowed

but the revised c99 standard (c99 + technical corrections ==
n1256.pdf) does not allow this, the cast operator should round
everything to double precision, so with -std=c99 it should work

for details i refer to the comp.lang.c thread i started about this issue
Received on Mon Jul 20 2009 - 21:29:32 UTC

This archive was generated by hypermail 2.2.0 : Mon Jul 20 2009 - 21:36:01 UTC