开发者

NSDecimalNumber for big number operations on iPhone

开发者 https://www.devze.com 2023-02-14 04:12 出处:网络
I need to use big number for precision in my application, float or double are not enough. I also have int and float numbers, and I have to do operations with all of them.

I need to use big number for precision in my application, float or double are not enough. I also have int and float numbers, and I have to do operations with all of them.

I think that NSDecimalNumber is good for the precision I need, but I would like to do operations with other kind of numbers and it is complex f开发者_StackOverflowormula. So I doesn't look appropriate to use this class in order to do complex formulas (too complicated to use the functions decimalWith... or decimalBy...) when you have lots of things.

Does anyone know what to use in order to manipulate big numbers easily, and do operations on them with different types (float, decimal, int)?

Thank you.


NSDecimalNumbers are simply wrappers around NSDecimal structs, which have a bunch of useful functions for manipulation without requiring the allocation of new objects.

I've used them a bit, and have come up with some other useful additions to those built-in: https://github.com/davedelong/DDMathParser/blob/master/DDMathParser/_DDDecimalFunctions.m

I would recommend using NSDecimals unless you can come up with a compelling reason not to.


Thanks guys! I finally used the double type that is enough for me. I was confused because I was using NSLog with %f to print my number and it wasn't what I wanted. I used %e instead to check the number in scientific notation was the right one, and it is. So I just do all my calculations using double number and it is working.

0

精彩评论

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