tags:

views:

54

answers:

2

I'm trying to figure out the best way to put complex numbers into my math-oriented iPhone app. I've found the mac osx manual page for complex numbers, which looks like a reasonable start. Three questions:

(1) Is this the best starting point?
(2) I will need an object-oriented wrapper -- can NSValue do this somehow? Or should I just write my own? (not difficult, but I'd rather use a built-in one if it exists).
(3) Is there a built-in way to get a string representation of a complex?

Thanks.

+1  A: 

As far as I know, there's no built-in complex number support in Cocoa (other than the C libraries). It sounds like you have a good approach. I haven't looked at it carefully yet, but here's a cocoa calculator with source code that supports complex numbers--you might be able to get some ideas.

eman
I'm upvoting this cos that's a cool little calc. But he implemented complex numbers from scratch. This gave him arbitrary precision, but it's a lot more work than I'm willing to do.
William Jockusch
+1  A: 
  1. Seems reasonable.
  2. I would just create a class of your own which has a complex ivar. It might be possible to subclass NSNumber (and thus NSValue) to do something sane, but you'll have to be a bit careful.
  3. No, just get the real and imaginary parts and print those.
Jason Foreman
I'm likely to accept this unless someone comes up with something better in the next few days.
William Jockusch