2012-10-19 63 views
10

對於以下代碼,在Objective-C中已經定義了「複數」嗎?

- Objective-C知道如何將「i」添加到複數中?當我在Complex.m文件中將「real」和「imaginary」定義爲double值時,我認爲Xcode只會知道「real」和「imaginary」是double值。例如,如果我將「myComplex.imaginary = 7;」替換爲main.m文件中複數的結尾,到「myComplex.imaginary = 7i;」該行的輸出變爲0.00000i,如果我添加任何其他字母,程序將無法運行,這是爲什麼?

基本上我覺得Xcode已經知道「真實」和「虛構」的意思,但我沒有詳細說明這一點,所以我有點困惑。

另外,我應該注意,我沒有創建下面的代碼,因爲我無法自己弄清楚這個問題,這段代碼是從我的圖書論壇的成員中複製的。

// Complex.h 

#include <Foundation/Foundation.h> 

@interface Complex : NSObject 
@property double real, imaginary; 
-(void) print; 
-(Complex *) add: (Complex *) complexNum; 
-(Complex *) subtract: (Complex *) complexNum; 
-(Complex *) multiply: (Complex *) complexNum; 
-(Complex *) divide: (Complex *) complexNum; 
@end 

// Complex.m 

#import "Complex.h" 

@implementation Complex 
@synthesize real, imaginary; 

-(void) print 
{ 
    NSLog(@"%f + %fi", real, imaginary); 
} 
-(Complex *) add: (Complex *) complexNum 
{ 
    Complex *result = [[Complex alloc]init]; 
    result.real = real + complexNum.real; 
    result.imaginary = imaginary + complexNum.imaginary; 
    return result; 
} 
-(Complex *) subtract: (Complex *) complexNum 
{ 
    Complex *result = [[Complex alloc]init]; 
    result.real = real - complexNum.real; 
    result.imaginary = imaginary - complexNum.imaginary; 
    return result; 
} 
-(Complex *) multiply: (Complex *) complexNum 
{ 
    Complex *result = [[Complex alloc]init]; 
    result.real = real * complexNum.real; 
    result.imaginary = imaginary * complexNum.imaginary; 
    return result; 
} 
-(Complex *) divide: (Complex *) complexNum 
{ 
    Complex *result = [[Complex alloc]init]; 
    result.real = real/complexNum.real; 
    result.imaginary = imaginary/complexNum.imaginary; 
    return result; 
} 
@end 

// 
// main.m 
// Complex 

#include <Foundation/Foundation.h> 
#import "Complex.h" 

int main(int argc, const char *argv[]) { 

    @autoreleasepool { 
     Complex *myComplex = [[Complex alloc]init]; 
     Complex *totalComplex = [[Complex alloc]init]; 
     Complex *yourComplex = [[Complex alloc]init]; 

     myComplex.real = 5.3; 
     myComplex.imaginary = 7; 
     [myComplex print]; 
     NSLog(@"+"); 

     yourComplex.real = 2.7; 
     yourComplex.imaginary = 4; 
     [yourComplex print]; 
     NSLog(@"="); 

     totalComplex = [myComplex add: yourComplex]; 
     [totalComplex print]; 
    } 
    return 0; 
} 
+5

哦,不!一個非易怒的英語中的n00b問題!發生了什麼事? (+1) – 2012-10-19 18:31:12

回答

12

複數類型在C99定義,該目標C的現代版是的超集。 The actual syntax is like

#include <complex.h> 

... 

complex double z = 2.7 + 3.4*I; 
complex double w = 4.5 - 1.7*I; 
complex double t = z*w; 
printf("%g + %gi", creal(t), cimag(t)); 

i後綴是extension coming from GCC。 Xcode使用的編譯器(clang)具有與GCC兼容的大多數功能,因此您可以編寫3.4i並且沒有錯誤。


而對於您的問題,

  • 怎樣的Objective-C知道要加一個 「我」,以複數?

如果你指的是產出,沒有Objective-C的不知道要加一個「我」。它打印出 「我」 只是因爲你告訴它

-(void) print 
{ 
    NSLog(@"%f + %fi", real, imaginary); 
//    ^
} 
  • ,如果我把 「myComplex.imaginary = 7;」到「myComplex.imaginary = 7i;」輸出該行成爲0.00000i

因爲7I是虛數,並且myComplex.imaginary是一個「雙」,因此數。 C標準建議,當在實數和虛數之間轉換時,您會得到零(C99§G.4.2/ 1)。因此,你寫的是有效的是myComplex.imaginary = 0.0;

  • 如果我添加任何其他字母,程序將無法運行,爲什麼是這樣?

其實你可以寫東西像7.0if。再次,這是Objective-C改編的C語言。您可以添加f以將默認類型「double」的十進制數轉換爲「float」,並且GCC添加了一項額外功能,您可以添加一個i以將實數轉爲虛數。其他足夠像7.0x將導致編譯器停止,因爲它不知道x的含義。

+0

哈哈,就是這樣,顯然我趕不上你了) – 2012-10-19 18:45:32

+0

謝謝!我完全理解,甚至沒有注意到NSLog中的「%fi」,直到下一章。 – Ronald

7

C99增加了原生支持複數,所以現在他們都容易處理,因爲普通浮點或整數。沒有更多醜陋的結構!推測通過使用浮點數表示法的技巧,_Complex_I宏和等效的I宏有一個值,當乘以一個實數時,產生多種類型double complexfloat complexcomplex是一個新的類型修飾符關鍵字,也在C99中介紹)。所以用這個新的便利功能,您可以在C一樣容易

#include <complex.h> 

double complex z1 = 2.0 + 3.0 * I; 
double complex z2 = 1.5 - 2.0 * I; 
double complex prod = z1 * z2; 

printf("Product = %f + %f\n", creal(prod), cimag(prod)); 

執行復數計算請檢查the GNU explanation關於這一點。

i後綴是GNU擴展到C99語言,因此它是非標準。儘管如此,Xcode使用的編譯器(GCC和Clang)都實現了這種擴展。

(旁註:Xcode中知道沒有這個請不要與編譯器混淆的Xcode IDE本身不執行編輯 - 它背後的編譯器做的。)

+0

謝謝,GNU解釋中的等式很有幫助。 – Ronald

1

有兩個嚴重的錯誤在課堂上覆雜的實現 - 複數乘以和絕對錯誤的方式劃分!僅僅乘以或除以兩個複數的實部和虛部是絕對不夠的。在這種情況下,你必須使用乘法和除法公式,我認爲Google包含很多關於它的條目。現在它是錯誤的代碼,它必須被重寫。

對於乘法它必須是這樣的

-(Complex *)mul:(Complex *)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    res.re = re * n.re - im * n.im; 
    res.im = re * n.im + im * n.re; 
    return res; 
} 
2

這裏是一個複雜的數字我已經爲我的項目的目的而開發的操作類。可能會對某人有用。它包含標準的加法,減法,乘法和除法。此外它還有計算複數的模數和自變量的方法。而且,最後,它具有計算轉彎因子(復指數)類方法是「蝴蝶」算法有用,當處理快速傅立葉變換

#import <Foundation/Foundation.h> 

@interface Complex : NSObject 
@property double re, im; 
-(Complex *)add :(Complex *) n; 
-(Complex *)sub :(Complex *) n; 
-(Complex *)mul :(Complex *) n; 
-(Complex *)div :(Complex *) n; 
+(Complex *)wkn :(int) k :(int) n; 
-(double)mod; 
-(double)arg; 
@end 

#import "Complex.h" 

@implementation Complex 
@synthesize re, im; 
// Addition of two complex numbers 
-(Complex *)add:(Complex *)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    res.re = re + n.re; 
    res.im = im + n.im; 
    return res; 
} 
// Subtraction of two complex numbers 
-(Complex *)sub:(Complex *)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    res.re = re - n.re; 
    res.im = im - n.im; 
    return res; 
} 
// Multiplication of two complex numbers 
-(Complex *)mul:(Complex *)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    res.re = re * n.re - im * n.im; 
    res.im = re * n.im + im * n.re; 
    return res; 
} 
// Division of two complex numbers 
-(Complex *)div: (Complex *)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    double A = (pow(n.re, 2.0) + pow(n.im, 2.0)); 
    res.re = (re * n.re - im * n.im)/A; 
    res.im = (im * n.re - re * n.im)/A; 
    return res; 
} 
// Modulus of complex number 
-(double)mod 
{ 
    double res = sqrt(pow(re, 2.0) + pow(im, 2.0)); 
    return res; 
} 
// Argument of complex number 
-(double)arg 
{ 
    double res; int quad; 
    if (re == 0 && im > 0) res = M_PI_2; 
    else if (re == 0 && im < 0) res = 3 * M_PI_2; 
    else 
    { 
     if (re > 0 && im >= 0) quad = 1; 
     else if (re < 0 && im >= 0) quad = 2; 
     else if (re < 0 && im < 0) quad = 3; 
     else if (re > 0 && im < 0) quad = 4; 
     double temp = atan(im/re); 
     switch (quad) 
     { 
      case 1: 
       res = temp; 
       break; 
      case 4: 
       res = 2 * M_PI + temp; 
       break; 
      case 2: case 3: 
       res = M_PI + temp; 
       break; 
     } 
    } 
    return res; 
} 
// Turning factor calculation for "butterfly" FFT algorithm 
+(Complex *)wkn:(int)k :(int)n 
{ 
    Complex *res = [[Complex alloc]init]; 
    res.re = cos(2 * M_PI * k/n); 
    res.im = -sin(2 * M_PI * k/n); 
    return res; 
} 

@end 

感謝您的耐心)

相關問題