Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are "Complex Numbers" already defined in Objective-C?

For the following code,

-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.

-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?

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;
}
like image 217
Ronald Avatar asked Oct 19 '12 18:10

Ronald


People also ask

What is complex number in C programming language?

Complex number in C Programming language. Software Engineering. Software Engineering C. More Less. A complex number is a number that can be written in the form x+yi where x and y are real numbers and i is an imaginary number. Therefore a complex number is a combination of: real number.

What is a complex number system?

In mathematics, a complex number is an element of a number system that contains the real numbers and a specific element denoted i, called the imaginary unit, and satisfying the equation i2 = −1. Moreover, every complex number can be expressed in the form a + bi, where a and b are real numbers.

What are the functions of complex library in C++?

The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them. real () – It returns the real part of the complex number. imag () – It returns the imaginary part of the complex number. abs () – It returns the absolute of the complex number.

Is _complex a type or not?

@Snaipe: complex is not a type. It's a macro that expands to _Complex, which is a type specifier, but not a type by itself. The complex types are float _Complex, double _Complex, and long double _Complex. It's not just GCC, it's defined in the standard that _Complex is a type specifier and complex.h has a complex macro that expands to _Complex.


Video Answer


1 Answers

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));

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.


And for your questions,

  • How does Objective-C know to add an "i" to complex numbers?

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);
//                 ^
}
  • if I turn "myComplex.imaginary = 7;" into "myComplex.imaginary = 7i;" the output for that line becomes 0.00000i

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?

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.

like image 71
kennytm Avatar answered Oct 03 '22 06:10

kennytm