This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Complex integer division
- From: "Joseph S. Myers" <jsm28 at cam dot ac dot uk>
- To: <gcc at gcc dot gnu dot org>
- Date: Sat, 17 Nov 2001 23:24:35 +0000 (GMT)
- Subject: Complex integer division
GCC supports complex integer types as an extension (whereas C99 only
allows complex floating point types).
How ought division of such numbers to work? That is, when can / and %
applied to such numbers yield undefined behavior, and exactly which values
should the results have? Presumably (a/b)*b + a%b equals a, as for normal
integer division, and the algebraic value of a%b divided by b should have
real and imaginary parts both strictly between 1 and -1 (or some similar
constraint), but is there a more precise specification? For normal
integer division, C99 precisely defines the results.
PR c/2995, though it claims to be a problem with casts, in fact reflects a
problem with how GCC handles complex integer division: using the normal
mathematical formulas without regard for how C integers behave. A simple
testcase which will divide by zero but clearly shouldn't is:
int
main (void)
{
_Complex int x = 1;
x = x / 65536;
return 0;
}
I don't think this is a reasonable way to do complex integer division; the
only corner case for normal integer division that causes problems is the
most negative integer modulo -1 (which ought to be 0 rather than an
exception), and making a lot of cases simply not work for dividing complex
integers seems bad. (C99 in fact requires that for floating point
division the usual mathematical formulas - the only way GCC supports -
should not be used by default because of undue overflow and underflow;
CX_LIMITED_RANGE defaults to "off".)
Suggestions on the right way to fix this?
--
Joseph S. Myers
jsm28@cam.ac.uk