operations on complex numbers in C99

K

Keith Thompson

those who know me have no need of my name said:
in comp.lang.c i read:


that is not a general restriction on functions, which may certainly return
a modifiable lvalue. though creal and cimag do not.

I wrote the quoted text ("creal() and cimag() are functions, ...").
Please don't snip attribution lines.
 
R

Richard Tobin

Good point. I cannot, then, see any way that a function may return a
modifiable lvalue.
[/QUOTE]
It cannot, functions calls evaluate either to void or rvalue
expressions, that was my point.

I can't see any way that you can write a function returning a
modifiable lvalue (or any lvalue at all), but that doesn't mean that a
standard library function couldn't be defined to return one.

-- Richard
 
K

Keith Thompson

I can't see any way that you can write a function returning a
modifiable lvalue (or any lvalue at all), but that doesn't mean that a
standard library function couldn't be defined to return one.

Except that no standard library function is defined to return an
lvalue.

I suppose, theoretically, that such a function could be defined in a
future version of the standard, but I sincerely hope that doesn't
happen (unless a mechanism is added to allow user-defined functions to
return lvalues).

<OT>Perhaps the OP who claimed that functions can return modifiable
lvalues was thinking of C++?</OT>
 
R

Richard Tobin

I can't see any way that you can write a function returning a
modifiable lvalue (or any lvalue at all), but that doesn't mean that a
standard library function couldn't be defined to return one.
[/QUOTE]
Except that no standard library function is defined to return an
lvalue.

Right, but the example that started this was just the sort of thing
the might plausibly be made to do so:

creal(myvar)=8.0;

One can imagine that being implemented as a macro in such a way that
it *would* work, for example

#define creal(x) ((x)._realpart)

which would make it natural to wish for a function to be able to do
the same.

Of course, it would be simpler to standardise such a thing it by
requiring it to be a macro, and allowing the macro to use some kind of
implementation-defined magic function if it wished to (errno is an
existing example, I think).

-- Richard
 
K

Keith Thompson

Right, but the example that started this was just the sort of thing
the might plausibly be made to do so:

creal(myvar)=8.0;

One can imagine that being implemented as a macro in such a way that
it *would* work, for example

#define creal(x) ((x)._realpart)

which would make it natural to wish for a function to be able to do
the same.

Of course, it would be simpler to standardise such a thing it by
requiring it to be a macro, and allowing the macro to use some kind of
implementation-defined magic function if it wished to (errno is an
existing example, I think).

Exactly. Making it a macro would make sense (and I'd argue for
calling it CREAL rather than creal). Making it a "function" that
behaves in a way that no user-defined function can possibly behave
would be silly.
 
A

Army1987

Except that no standard library function is defined to return an
lvalue.

Right, but the example that started this was just the sort of thing
the might plausibly be made to do so:

creal(myvar)=8.0;[/QUOTE]

Where will we stop? What about atan(myvar) = 1 to set myvar to
tan(1)? :)
One can imagine that being implemented as a macro in such a way that
it *would* work, for example

#define creal(x) ((x)._realpart)

which would make it natural to wish for a function to be able to do
the same.
That would sound great, but for consistency with the rest of the
language I'd prefer setreal(z, x).
Much like ferror() which doesn't return a lvalue (but, depending
on how the FILE object is made, it could be very straightforward
to write a macro which does), and we have to use clearerr() to set
it to zero (and have no way to set it to one).
 
J

jacob navia

Except that no standard library function is defined to return an
lvalue.

Right, but the example that started this was just the sort of thing
the might plausibly be made to do so:

creal(myvar)=8.0;

One can imagine that being implemented as a macro in such a way that
it *would* work, for example

#define creal(x) ((x)._realpart)

which would make it natural to wish for a function to be able to do
the same.
[/QUOTE]

This is EXACTLY how it is implemented in lcc-win32.
This makes access to the parts of a complex number VERY
efficient.

The correct way is it however:

double _Complex z1;

....

z1 = 22.0+cimag(z1)*I; // Sets the real part to 22
or
z1 = creal(z1)+22*I; // Sets the imaginary part to 22
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top