NSDecimalNumber for Large Number Operations on iPhone

I need to use a large number for accuracy in my application, float or double is not enough. I also have int and float numbers, and I have to do operations with all of them.

I think NSDecimalNumber is good for the accuracy I need, but I would like to do operations with other types of numbers, and this is a complex formula. Therefore, I do not consider it appropriate to use this class to execute complex formulas (it is too difficult to use the functions decimalWith ... or decimalBy ...) when you have a lot of things.

Does anyone know what to use to easily manipulate large numbers and perform operations with them with different types (float, decimal, int)?

Thanks.

+4
source share
2 answers

NSDecimalNumbers are just wrappers around NSDecimal structs, which have tons of useful functions to manipulate without the need for new objects.

I used them a little and came up with some useful additions to those that were built in: https://github.com/davedelong/DDMathParser/blob/master/DDMathParser/_DDDecimalFunctions.m

I would recommend using NSDecimal unless you have a good reason not to.

+7
source

Thanks guys! I finally used the double type, which is enough for me. I was confused because I used NSLog with %f to print my number, and that was not what I wanted. Instead, I used %e to verify that the number in scientific notation was correct, and it is. So I just do all my calculations using a double number, and it works.

0
source

Source: https://habr.com/ru/post/1341709/


All Articles