Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Operator ~ with complex arguments

1 view
Skip to first unread message

jacob navia

unread,
Jul 9, 2008, 10:44:45 AM7/9/08
to
Consider this code:

< 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

Wojtek Lerch

unread,
Jul 9, 2008, 10:51:56 AM7/9/08
to
"jacob navia" <ja...@nospam.com> wrote in message
news:g52isv$beq$1...@aioe.org...

> Consider this code:
>
> < code >
> #include <complex.h>
> int main(void)
> {
> double complex c = 0.4+2.9I;
> c = ~c;
> }
> < end code >
>
> Is this legal?

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.

jacob navia

unread,
Jul 9, 2008, 10:55:39 AM7/9/08
to

AARGH I oversaw that, I just looked at the definition of the
operator. Thanks

Jack Klein

unread,
Jul 9, 2008, 11:01:12 AM7/9/08
to
On Wed, 09 Jul 2008 16:44:45 +0200, jacob navia <ja...@nospam.com>
wrote in comp.lang.c:

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

jacob navia

unread,
Jul 9, 2008, 11:09:28 AM7/9/08
to

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

cr88192

unread,
Jul 9, 2008, 9:55:08 PM7/9/08
to

"jacob navia" <ja...@nospam.com> wrote in message
news:g52isv$beq$1...@aioe.org...

> Consider this code:
>
> < code >
> #include <complex.h>
> int main(void)
> {
> double complex c = 0.4+2.9I;
> c = ~c;
> }
> < end code >
>
> Is this legal?
>

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.

lawrenc...@siemens.com

unread,
Jul 9, 2008, 9:35:38 PM7/9/08
to
In comp.std.c jacob navia <ja...@nospam.com> wrote:
>
> AARGH I oversaw that, I just looked at the definition of the
> operator. Thanks

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

0 new messages