Talk:Arithmetic/Complex: Difference between revisions

C99 is correct
(I see now...sorry)
(C99 is correct)
Line 1:
Ada and C++ both still need negation. I forgot to put it in the Java example before but I added I it later. Sorry for the confusion. --[[User:Mwn3d|Mwn3d]] 10:13, 9 March 2008 (MDT)
:The Ada entry has been amended to include negation. --[[User:Waldorf|Waldorf]] 14:40, 9 March 2008 (MDT)
 
==C99 example incorrect?==
I believe the C example is incorrect. The current written example relies upon operator overloading which is not supported by C99.--[[User:Waldorf|Waldorf]] 09:10, 11 March 2008 (MDT)
:I did some looking; there are complex built-in types in the C99 standard, with defined implicit casts up and down to the other numeric primitives. --[[User:IanOsgood|IanOsgood]] 10:36, 11 March 2008 (MDT)
Line 9 ⟶ 11:
::::::I agree with what you say. The task requires addition and multiplication of two complex numbers, and inversion and negation of a single complex number. The C solution uses C arithmetic operators. C99 does not provide operator overloading. This means that the arithmetic operators can only be used if implicit conversion to a primitive type occurs. Either the example is wrong because it improperly uses arithmetic operators for complex arithmetic, or it is wrong because implicit conversions result in real arithmetic, not complex arithmetic. The truth can be easily demonstrated by printing the results of the operations in C.--[[User:Waldorf|Waldorf]] 09:30, 13 March 2008 (MDT)
:::::::OK...looking into it you're right. I offer two solutions. 1: I added an example which defines a struct and the desired operations, 2: rework the complex.h solution to use creal and cimag functions (from [http://en.wikipedia.org/wiki/Complex.h complex.h]) as part of other functions to do the math. --[[User:Mwn3d|Mwn3d]] 10:01, 13 March 2008 (MDT)
:::::Waldorf, I don't know what you're going on about. When I compile this with '''gcc -std=c99''' I get correct results (once I corrected the constants). The implicit conversions only happen when there are type mismatches, but all the operations are between complex types. For 1.0/a, there is an ''up'' conversion to 1+0i before dividing. The standard operators are all defined correctly for complex types, and other library functions like '''conj()''' for the conjugate are defined for complex-specific operations. --[[User:IanOsgood|IanOsgood]] 10:47, 13 March 2008 (MDT)
Anonymous user