是“复数"已经在Objective-C中定义? [英] Are "Complex Numbers" already defined in Objective-C?

查看:119
本文介绍了是“复数"已经在Objective-C中定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下代码,

-Objective-C如何知道在复数上添加"i"?当我在Complex.m文件中将实数"和虚数"定义为双精度值时,我发现Xcode只会知道实数"和虚数"是双精度值.

-How does Objective-C know to add an "i" to complex numbers? When I defined "real" and "imaginary" as double values in the Complex.m file I figured Xcode would ONLY know that "real" and "imaginary" are double values.

-例如,如果我在main.m文件中的复数末尾添加"i",则如果将"myComplex.imaginary = 7;"设置为变成"myComplex.imaginary = 7i;"该行的输出变为0.00000i,如果我添加任何其他字母,该程序将根本无法运行,为什么?

-If I add an "i" to the end of a complex number in main.m file, for example, if I turn "myComplex.imaginary = 7;" into "myComplex.imaginary = 7i;" the output for that line becomes 0.00000i, if I add any other letter, the program will simply not run, why is this?

在我看来,Xcode已经知道真实"和虚构"的含义,我所关注的书没有对此进行详细说明,所以我有些困惑.

Basically it appears to me that the meaning of "real" and "imaginary" are already known to Xcode, the book I'm following did not specify this so I'm a little confused.

此外,我应该注意,由于我自己无法解决问题,因此未创建以下代码,这些代码是从我的图书论坛的成员中复制的.

Also, I should note that I did not create the following code as I couldn't figure out the problem on my own, this code was copied from a member of my books forum.

//  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;
}

推荐答案

C99 中定义了复数类型. Objective-C的现代版本是它的超集. 实际语法就像:

Complex number types are defined in C99, which the modern version of Objective-C is a superset of. 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后缀是来自GCC的扩展名. Xcode使用的编译器(clang)具有与GCC兼容的大多数功能,因此您可以编写3.4i并且没有错误.

That i suffix is an extension coming from GCC. The compiler (clang) used by Xcode has most features being compatible with GCC, thus you can write 3.4i and have no errors.

对于您的问题,

  • Objective-C如何知道在复数上添加"i"?
  • How does Objective-C know to add an "i" to complex numbers?

如果您的意思是输出,那么没有Objective-C会添加"i".它打印"i"的唯一原因是您告诉了它

If you mean the output, no Objective-C does not know to add an "i". It prints the "i" only because you told it to

-(void) print
{
    NSLog(@"%f + %fi", real, imaginary);
//                 ^
}

  • 如果我打开"myComplex.imaginary = 7;"变成"myComplex.imaginary = 7i;"该行的输出为0.00000i
  • if I turn "myComplex.imaginary = 7;" into "myComplex.imaginary = 7i;" the output for that line becomes 0.00000i

因为7i是一个虚数,而myComplex.imaginary是一个双",因此是一个实数数. C标准建议在实数和虚数之间转换时,您将获得零(C99§G.4.2/1).因此,实际上您写的是myComplex.imaginary = 0.0;.

Because 7i is an imaginary number, and myComplex.imaginary is a "double", thus a real number. The C standard recommends that, when converting between real and imaginary numbers, you'll get zero (C99 §G.4.2/1). Thus effectively what you've written is myComplex.imaginary = 0.0;.

  • 如果我添加任何其他字母,该程序将无法运行,为什么?
  • if I add any other letter, the program will simply not run, why is this?

实际上,您可以编写诸如7.0if之类的东西.同样,这是Objective-C适应的C事物.您可以添加f将十进制数字从默认类型"double"转换为"float",并且GCC添加了一项额外功能,您可以添加i将实数转换为虚数.其他类似7.0x的条件将导致编译器停止运行,因为它不知道x的含义.

Actually you can write things like 7.0if. Again, this is a C thing, which Objective-C has adapted. You are allowed to add an f to turn a decimal number from the the default type "double" to "float", and GCC adds an extra feature that you can add an i to turn a real number to an imaginary number. Other suffices like 7.0x will cause the compiler to stop because it doesn't know what x means.

这篇关于是“复数"已经在Objective-C中定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆