{"id":13995190,"url":"https://github.com/mkrd/Swift-BigInt","last_synced_at":"2025-07-22T21:32:15.095Z","repository":{"id":38037513,"uuid":"51140003","full_name":"mkrd/Swift-BigInt","owner":"mkrd","description":"A lightweight, Arbitrary Precision Arithmetic Library for Swift!","archived":false,"fork":false,"pushed_at":"2024-07-27T11:19:55.000Z","size":6028,"stargazers_count":246,"open_issues_count":9,"forks_count":60,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-11-13T01:47:16.682Z","etag":null,"topics":["arbitrary-precision","bigint","biginteger","bignum","bint","gmp","swift","swift-bigint","xcode"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mkrd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-05T10:23:37.000Z","updated_at":"2024-10-31T10:56:35.000Z","dependencies_parsed_at":"2022-09-20T12:41:46.128Z","dependency_job_id":"d1ce00be-0f85-4e4b-9ebe-67d74b3e96e8","html_url":"https://github.com/mkrd/Swift-BigInt","commit_stats":{"total_commits":178,"total_committers":16,"mean_commits":11.125,"dds":0.7303370786516854,"last_synced_commit":"4a8475ab4980904a96bab4f06fbc5ac714186888"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkrd%2FSwift-BigInt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkrd%2FSwift-BigInt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkrd%2FSwift-BigInt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkrd%2FSwift-BigInt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mkrd","download_url":"https://codeload.github.com/mkrd/Swift-BigInt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227177810,"owners_count":17743170,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arbitrary-precision","bigint","biginteger","bignum","bint","gmp","swift","swift-bigint","xcode"],"created_at":"2024-08-09T14:03:17.700Z","updated_at":"2024-11-29T17:31:10.803Z","avatar_url":"https://github.com/mkrd.png","language":"Swift","readme":"# Swift-BigInt\nSwift-BigInt is a lightweight, and easy-to-use, arbitrary precision arithmetric library for Swift 5. \n\nIt supports whole Numbers (BInt) and Fractions (BDouble) with most of the common math operators. Optimized mathematical functions like factorial or gcd are also implemented and are accessible through BIntMath. For more details, please continue reading.\n\nSome benchmarks are located in Benchmarks.swift, note that these are more than 10 times faster in the release mode, compared to the debug mode of Xcode.\n\n# Survey Time!\n\n[Survey Link](https://forms.gle/9ycR8eUEsCenvMUn7)\n\nWe want to hear your opinion about Swift BigInt! If you have a few minutes, please help us with answering a few questions about how you use this project, and tell us your opinion about it. The survey is completely anonymous, and will be used to evaluate which features will be prioritized in the future.\n\n\n\n\n# Installation\n\n## Drag and Drop\nOne of the main goals of this library is to be lightweight and independent.\n\nSimply drag and drop `Swift-Big-Number-Core.swift` from the `sources` folder into your project!\n\nYes, it's that easy :)\n\n\n## Swift Package Manager\n\nYou can use the [Swift Package Manager](https://swift.org/package-manager/) and specify the  package dependency in your `Package.swift` file by adding this:\n```\n.package(url: \"https://github.com/mkrd/Swift-BigInt.git\", from: \"2.0.0\")\n```\n\n```\nimport BigNumber\n```\n\n\n## CocoaPods\n\nPut the following in your Podfile:\n```\npod 'Swift-BigInt', '~\u003e 2.0'\n```\n\n```\nimport BigNumber\n```\n\n\n# Compatibility\nIt is recommended to use Xcode 9+ and Swift 4+. Issues have been reported with older versions, so you might want to use an older version of this library if you can't update.\n\n\n# Getting Started\nHere is a small example, to showcase some functionalities of this library. If you want to learn more, please continue reading the Usage section below.\n\n```swift\nlet a = BInt(12)\nlet b = BInt(\"-10000000000000000000000000000000000000000000000000000000000000000\")!\n\nprint(b)\n\u003e\u003e\u003e -10000000000000000000000000000000000000000000000000000000000000000\n\nprint(-a * b)\n\u003e\u003e\u003e 120000000000000000000000000000000000000000000000000000000000000000\n\nprint(BInt(200).factorial())\n\u003e\u003e\u003e 788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000\n```\n\n# Usage\n\n## BInt\n\n### Initialization\nYou initialize BInt with `Int`, `UInt`, and `String`. If you use a `String`, the initialized `BInt` will be an optional type, which will be empty if the `String` does not contain an valid number.\n\n```\nBInt(Int)\nBInt(UInt)\nBInt(String)?\nBInt(String, radix: Int)?\n```\n\n### Examples\n```swift\nlet a = BInt(12)\nprint(a)\n\u003e\u003e\u003e 12\n\n\nlet b = BInt(\"-234324176583764598326758236587632649181349105368042856028465298620328782652623\")\nprint(b!)\n\u003e\u003e\u003e -234324176583764598326758236587632649181349105368042856028465298620328782652623\n\n\nlet invalid = BInt(\"I'm not a number\")\nif let c = invalid {\n  print(c)\n} else {\n  print(\"Not a valid number!\")\n}\n\u003e\u003e\u003e Not a valid number!\n\n\nlet d = BInt(\"fff\", radix: 16)\nprint(d)\n\u003e\u003e\u003e 4095\n```\n\n### BInt offers these struct methods\n```swift\nlet big = BInt(\"-143141341\")!\n\nbig.description // Returns \"-143141341\"\n=\u003e print(big) // prints \"-143141341\"\n\nbig.toInt() // returns -143141341 (only works when Int.min \u003c= big \u003c= Int.max)\n\nbig.isPositive() // Returns false\nbig.isNegative() // Returns true\nbig.isZero() // Returns false\n\nbig.negate() // Returns noting, but negates the BInt (mutating func)\n\nbig.rawData() // Returns internal structure\n```\n\n## The following Operators work with BInt\n```swift\n// Operating on Int and BInt result in a typecast to BInt\n\n// Addition\nBIntOrInt  +  BIntOrInt // Returns BInt\nBIntOrInt  += BIntOrInt\n\n//Subtraction\nBIntOrInt -  BIntOrInt // Returns BInt\nBIntOrInt -= BIntOrInt\n\n// Multiplication\nBIntOrInt *  BIntOrInt // Returns BInt\nBIntOrInt *= BIntOrInt\n\n// Exponentiation\nBInt ** Int       // Returns BInt to the power of Int\n\n// Modulo\nBIntOrInt  %  BIntOrInt // Returns BInt\nBInt       %= BInt\n\n// Division\nBInt /  BInt // Returns BInt\nBInt /= BInt\n\n\n// Comparing\nBInt == BInt\nBInt != BInt\nBInt \u003c  BInt\nBInt \u003c= BInt\nBInt \u003e  BInt\nBInt \u003e= BInt\n```\n\n### Implemented BInt math functions\n```swift\nfact(Int) // Returns factorial as BInt\n\ngcd(BInt, BInt) // Returns greatest common divisor as BInt\n\nlcm(BInt, BInt) // Returns lowest common multiple as BInt\n\npermutations(BInt, BInt) // Returns BInt\n\ncombinations(BInt, BInt) // Returns BInt\n```\n\n## BDouble\n\n### BDouble allows these constructors\n```swift\nBDouble(Int)\nBDouble(Double)\nBDouble(String)?\nBDouble(Int, over: Int)\nBDouble(String, over: String)?\nBDouble(String, radix: Int)?\n```\n\n### Examples\n```swift\nlet integer = BDouble(221)\nlet double = BDouble(1.192)\nlet fraction = BDouble(3, over: 4)\nlet stringFraction = BDouble(\"1\" over: \"3421342675925672365438867862653658268376582356831563158967\")!\n```\n\n### BDouble offers these struct methods\n```swift\nlet bigD = BDouble(-12.32)\n\nbigD.description // Returns \"-308/25\"\n=\u003e print(bigD) // prints \"-308/25\"\n\n\nbigD.minimize() // Divides numerator and denominator by their gcd for storage and operation efficiency, usually not necessary, because of automatic minimization\n\nbigD.rawData() // Returns internal structure\n```\n\n## The following Operators work with BDouble\n```swift\n// Needs more operators, interoperability with BInt\n\n// Addition\nBDouble + BDouble // Returns BDouble\n\n// Subtraction\nBDouble - BDouble // Returns BDouble\n\n// Multiplication\nBDouble * BDouble // Returns BDouble\n\n// Division\nBDouble / BDouble // Returns BDouble\n\n// Comparing\nBDouble \u003c BDouble\n/*\nImportant:\na \u003c b \u003c==\u003e b \u003e a\na \u003c= b \u003c==\u003e b \u003e= a\nbut:\na \u003c b \u003c==\u003e !(a \u003e= b)\na \u003c= b \u003c==\u003e !(a \u003e b)\n*/\n\n// More will follow\n```\n\n\n\n# About performance\nBInt about twice as fast as mini-gmp, as of now (not counting the normal gmp, because it needs to be installed and is not portable). For example, BInt can add numbers about 2 times faster than GMP (272ms vs 530ms for fib(100,000)), and multiplication is more than twice as fast. When given the task of calculating and printing factorials successively, BInt performs significantly better than GMP. In addition, GMP is significantly harder to use, while BInt offers an intuitive interface.\n\n\n\n# Contributing\n1. Fork it!\n2. Create your feature branch: `git checkout -b my-new-feature`\n3. Commit your changes: `git commit -am 'Add some feature'`\n4. Push to the branch: `git push origin my-new-feature`\n5. Submit a pull request :D\n","funding_links":[],"categories":["Swift"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmkrd%2FSwift-BigInt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmkrd%2FSwift-BigInt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmkrd%2FSwift-BigInt/lists"}