Store and perform operations with huge numbers in iOS

前端 未结 3 1712
情歌与酒
情歌与酒 2020-12-19 19:07

How could I handle operations with a number like: 485345883069611330679681969652579614157566565218188487507235474776734576700196328825241646476514920257289805718335793417439

3条回答
  •  离开以前
    2020-12-19 19:27

    What you need is a library that provides support for operations on integers of arbitrary length. However, from what I've been able to find out, there are no such libraries written in Objective-C.

    You are nevertheless in luck as Objective-C is a superset of C. That makes it possible for you to use C libraries such as those described in the answers to this somewhat dated SO question.

    Also, since the Clang compiler supports C++ and combining Objective-C and C++ code, you can probably use something like big int.

    Note that none of the built-in types is even close to being big enough to represent numbers with as many digits as your examples. The biggest available integer type is unsigned long long, if you don't need negative numbers, and its size is 8 bytes/64 bits, which gives you a range of 0-18446744073709551615, or 20 digits max.

提交回复
热议问题