Ios:objective-c High Accuracy Calculation

Source: Internet
Author: User

A lot of decimal calculations are needed in a recent iOS project, which requires a high level of precision. As we all know, for the computer float,double such floating-point numbers are stored in scientific notation, so there are computational errors, so they can not be directly related to the calculation.

There are corresponding high-precision calculations in each language, such as the BigDecimal class in Java, and the decimal type in C # is used to solve high-precision computational problems, such as currency calculations.

After looking at the API given by Apple, we found a class-----nsdecimalnumber


Let's take a look at the method definition we need in it:


-decimalnumberbyadding :

-Decimalnumberbysubtracting:

-Decimalnumberbymultiplyingby:

-Decimalnumberbydividingby:


The above 4 methods represent the subtraction of our arithmetic in each other.


This should be a good understanding of how to do our high-precision calculations, the following is the example I wrote for understanding, Addendvalue and augendvalue for the nsstring type, A high-precision calculation can be done by converting the number of numbers that require operations into the nsstring type of the corresponding number of digits:

   

Nsdecimalnumber *addendnumber = [Nsdecimalnumber decimalnumberwithstring:addendvalue]; Nsdecimalnumber *augendnumber = [Nsdecimalnumber decimalnumberwithstring:augendvalue]; return [Addendnumber Decimalnumberbyadding:augendnumber];







This article is from the "It-fan" blog, make sure to keep this source http://iteafan.blog.51cto.com/784291/1585053

Ios:objective-c High Accuracy Calculation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.