< code >
#include <complex.h>
int main(void)
{
double complex c = 0.4+2.9I;
c = ~c;
}
< end code >
Is this legal?
The standard says about the ~ operator:
<quote>
6.5.5.3
The result of the ~ operator is the bitwise complement of its (promoted)
operand (that is, each bit in the result is set if and only if the
corresponding bit in the converted operand is not set). The integer
promotions are performed on the operand, and the result has the
promoted type. If the promoted type is an unsigned type, the expression
~E is equivalent to the maximum value representable in that type minus
E.
< end quote>
This means that I should "promote" the complex to double,
ignoring the imaginary part, then converting the resulting double
to integer, NOT to unsigned integer.
0.4 + 2.9I --> 0.4 --> 0
THEN I should apply ~ to that, resulting into the integer -1. Then,
I should convert that -1 to double, then to double complex.
This looks quite hairy. Did I get it right?
Thanks in advance for your time.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
It's a constraint violation.
6.5.3.3 Unary arithmetic operators
Constraints
1 The operand of the unary + or - operator shall have arithmetic type; of
the ~ operator, integer type; of the ! operator, scalar type.
AARGH I oversaw that, I just looked at the definition of the
operator. Thanks
Unless I am missing, applying the unary ~ operator to anything other
than an integer operand is a constraint violation, requiring a
diagnostic:
"6.5.3.3 Unary arithmetic operators
Constraints
1 The operand of the unary + or - operator shall have arithmetic type;
of the ~ operator, integer type; of the ! operator, scalar type."
I haven't actually used the <complex.h> types or functions myself, but
I just took a quick look at that section of the standard and see
nothing there that overrules this constraint for complex types.
Of course, once you emit the required diagnostic for the constraint
violation, you can do whatever you want with the expression.
Or am I missing something?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
No I do not think so. I oversaw that sentence and started reading at the
definition of the operator. My fault. Thanks for your time
not as per the standard, but this was a good place IMO to put a conjugate
operator...
~(a+bi)=(a-bi)
was also used for quaternions.
but, at least for complexes, the standard provides a function for this.
When consulting the C standard, *always* look at all the parents of the
applicable subclause, too. They frequently contain vital information.
--
Larry Jones
Philistines. -- Calvin