开发者

Small numbers in Objective C 2.0

I created a calculator开发者_JAVA技巧 class that does basic +,-, %, * and sin, cos, tan, sqrt and other math functions.

I have all the variables of type double, everything is working fine for big numbers, so I can calculate numbers like 1.35E122, but the problem is with extremely small numbers. For example if I do calculation 1/98556321 I get 0 where I would like to get something 1.01464E-8.

Should I rewrite my code so that I only manipulate NSDecimalNumber's and if so, what do I do with sin and cos math functions that accept only double and long double values.


1/98556321

This division gives you 0 because integer division is performed here - the result is an integer part of division. The following line should give you floating point result:

1/(double)98556321


integer/integer is always an integer

So either you convert the upper or the lower number to decimal

(double)1/98556321

or

1/(double)98556321

Which explicitely convert the number to double.

Happy coding....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜