iPhone — working with complex numbers

二次信任 提交于 2019-12-11 07:24:00

问题


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?


回答1:


  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.



回答2:


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.



来源:https://stackoverflow.com/questions/3340219/iphone-working-with-complex-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!