NO

Author Topic: Complex Float gives surprising result  (Read 17175 times)

JohnF

  • Guest
Re: Complex Float gives surprising result
« Reply #30 on: August 25, 2008, 08:04:45 PM »
Ok, if you use code that returns a pointer make sure you use the code I showed you. :)

Btw, I think using the void routine, the one that doesn't return anything is more efficient.

John
« Last Edit: August 25, 2008, 09:58:31 PM by JohnF »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Complex Float gives surprising result
« Reply #31 on: September 23, 2008, 05:27:50 PM »
The complex data types (float complex, double complex, long double complex) have a memory layout where the imaginary part immediately follows the real part, which (IIRC) is a requirement. It may look like a structure, but I don't consider it as such. It would perhaps be convenient, for cross-language development, if float complex were passed like a struct, but this would cause me an endless stream of problems (much more than struct's). 

I started in the computer business 1981, and have managed to stay away from complex numbers so far. I suspect I'm not the only one. In other words: I personally think the addition of complex numbers in C99 was a mistake, but since it's part of the current standard I have (finally) added it. I will not, however, do more than absolutely necessary to make it work.
/Pelle

JohnF

  • Guest
Re: Complex Float gives surprising result
« Reply #32 on: September 23, 2008, 06:41:38 PM »
Welcome back!

John

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Complex Float gives surprising result
« Reply #33 on: September 23, 2008, 08:59:33 PM »
Thank you!
/Pelle

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Complex Float gives surprising result
« Reply #34 on: September 24, 2008, 11:22:05 AM »
Nice to hear you again Pelle!  :D

I agree with you about complex numbers (life is already complex enough to me!  ;) ).
Anyway please consider only the issue related to automatic variables returned by functions, this could be a bomb also when cross-language is not involved (especially if some function modify it when the stack has been reused!).
« Last Edit: September 24, 2008, 11:24:35 AM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Complex Float gives surprising result
« Reply #35 on: September 24, 2008, 03:43:56 PM »
Anyway please consider only the issue related to automatic variables returned by functions, this could be a bomb also when cross-language is not involved (especially if some function modify it when the stack has been reused!).
I have just glanced through this thread, so I may be missing something obvious...

Structures with a size different from 1/2/4/8 bytes, and all complex numbers, are returned using a "hidden" pointer to a location provided by the caller (for example, a function with a 'void' parameter list, and return type of 'float _Complex', will actually receive one pointer argument). The callee will copy the relevant bytes to the provided location, using the hidden pointer. When compiling a function that receives such a struct or complex number, the compiler will sometimes (when needed) create temporary stack objects for this. AFAIK, all of this is working as it should...

Is this what you are talking about? Is there a problem here?
/Pelle

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Complex Float gives surprising result
« Reply #36 on: September 24, 2008, 04:16:29 PM »
Unfortunately I don't have time to check extensively. As far I can see you always copy contents of the returned structure, don't pass the pointer so there should be no problem. I don't see, in my trials the creation of the "hidden" pointer that you mention, but this should be due to my limited checks. I don't know if does exist a construct that insert an operation between the function call and the result assignement that could create problems.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide