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

Re: Conflicts between C standard and 754-2008



Hossam,
754 addition is commutative. The missing associativity causes the problem, when you add more terms. but even for 2 operands we have
x - y == -y + x != -(y - x)
for directed roundings
Juergen

Am 06.01.2011 12:39, schrieb Hossam A. H. Fahmy:
754 and 1788 members,

I followed the steps of Michel and tried to see if there are any other
issues with the C standard that conflict with 754-2008. I used the links
provided by Fred Tydeman on the current versions of the standard.
(http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf  and
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1539.pdf)

I found that on page 452 of document n1256 (also on page 511 of n1539)
within the annex on floating point, C allows the transformations

x - y  = x + (-y) = (-y) + x

I am a bit troubled by this since it means that C assumes that FP
addition is commutative in general which is not true. It is specifically
wrong if applied on more than two terms. For example,

#include<stdio.h>

int main (void)
{
float x=1e2, y=1e30, z=1e30;

printf("\n x=%g,  y=%g,  z=%g", x, y, z);
printf("\n  z-y+x=%g\n",  z-y+x);
printf("\n  z+x-y=%g\n",  z+x-y);
printf("\n  x-y+z=%g\n",  x-y+z);
printf("\n  x+z-y=%g\n",  x+z-y);
printf("\n -y+z+x=%g\n", -y+z+x);
printf("\n -y+x+z=%g\n", -y+x+z);
}

when compiled with gcc without any optimizations yields:

  x=100,  y=1e+30,  z=1e+30
   z-y+x=100

   z+x-y=0

   x-y+z=0

   x+z-y=0

  -y+z+x=100

  -y+x+z=0

which is the expected behavior from a 754-2008 compliant system.
However, if the compiler used the transformation mentioned by the C
standard the results would not be the same.

Is my understanding correct and this is indeed a conflict? Comments
anyone?
--
Hossam A. H. Fahmy
Associate Professor
Electronics and Communications Engineering
Cairo University
Egypt



--
o Prof. Dr. Juergen Wolff von Gudenberg, Lehrstuhl fuer Informatik II
    / \          Universitaet Wuerzburg, Am Hubland, D-97074 Wuerzburg
InfoII o         Tel.: +49 931 / 31 86602
  / \  Uni       E-Mail: wolff@xxxxxxxxxxxxxxxxxxxxxxxxxxx
 o   o Wuerzburg


754 | revision | FAQ | references | list archive