{"id":47145076,"url":"https://github.com/eobermuhlner/kotlin-big-math","last_synced_at":"2026-03-12T23:34:04.026Z","repository":{"id":57715604,"uuid":"97307166","full_name":"eobermuhlner/kotlin-big-math","owner":"eobermuhlner","description":"Kotlin library for BigDecimal math functions (pow, sqrt, log, sin, ...) using arbitrary precision.","archived":false,"fork":false,"pushed_at":"2020-06-22T14:29:43.000Z","size":76,"stargazers_count":16,"open_issues_count":2,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2023-07-28T00:15:20.567Z","etag":null,"topics":["bigdecimal","kotlin","mathematical-functions","newton-raphson","precision","taylor-series"],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","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/eobermuhlner.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}},"created_at":"2017-07-15T09:55:38.000Z","updated_at":"2023-02-24T11:22:45.000Z","dependencies_parsed_at":"2022-09-03T07:11:21.812Z","dependency_job_id":null,"html_url":"https://github.com/eobermuhlner/kotlin-big-math","commit_stats":null,"previous_names":[],"tags_count":2,"template":null,"template_full_name":null,"purl":"pkg:github/eobermuhlner/kotlin-big-math","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eobermuhlner%2Fkotlin-big-math","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eobermuhlner%2Fkotlin-big-math/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eobermuhlner%2Fkotlin-big-math/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eobermuhlner%2Fkotlin-big-math/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eobermuhlner","download_url":"https://codeload.github.com/eobermuhlner/kotlin-big-math/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eobermuhlner%2Fkotlin-big-math/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30449176,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-12T21:31:01.033Z","status":"ssl_error","status_checked_at":"2026-03-12T21:30:43.161Z","response_time":114,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["bigdecimal","kotlin","mathematical-functions","newton-raphson","precision","taylor-series"],"created_at":"2026-03-12T23:34:03.466Z","updated_at":"2026-03-12T23:34:04.016Z","avatar_url":"https://github.com/eobermuhlner.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n[![Build Status](https://travis-ci.org/eobermuhlner/kotlin-big-math.svg?branch=master)](https://travis-ci.org/eobermuhlner/kotlin-big-math)\n[![Maven Central](https://img.shields.io/maven-central/v/ch.obermuhlner/kotlin-big-math.svg)](https://search.maven.org/artifact/ch.obermuhlner/kotlin-big-math)\n\n# kotlin-big-math\nKotlin library for BigDecimal math functions (pow, sqrt, log, sin, ...) using arbitrary precision.\n\n## Introduction\n\nThis library depends on the [big-math](https://github.com/eobermuhlner/big-math) library\nand provides additional Kotlin features to do calculations with arbitrary precision.\n\nIt provides two different approaches to do the calculations\n- `BigDecimal` + `DefaultBigDecimalMath` : recommended approach\n- `BigFloat` : experimental approach\n\nThe [big-math](https://github.com/eobermuhlner/big-math) library\nprovides advanced math functions (`pow`, `sqrt`, `log`, `sin`, ...).\n\nThe `kotlin-big-math` library enhances the `big-math` library with the kotlin specific operators and infix methods.\nAdditionally it solves several of the problems that the standard kotlib library has when using `BigDecimal`.\n \n\n# BigDecimal + DefaultBigDecimalMath\n\nBy using the following imports you can use the standard `BigDecimal` class \nand the mathematical functions in `DefaultBigDecimalMath` in a simple but powerful manner:\n```kotlin\nimport java.math.BigDecimal\nimport java.math.BigDecimal.*\nimport ch.obermuhlner.math.big.DefaultBigDecimalMath.*\nimport ch.obermuhlner.math.big.kotlin.bigdecimal.*\n```\n\n## BigDecimal Operators\n\n* `+` operator\n* `-` operator\n* `*` operator\n* `/` operator\n* `%` operator\n\n\n* `++` operator\n* `--` operator\n\n\n* `pow` infix method\n* `root` infix method\n\n\n* `valueOf(Int)` method (equivalent to `BigDecimal(Int)` constructor)\n\n### Using BigDecimal + DefaultBigDecimalMath in Kotlin\n\nThe usage of `DefaultBigDecimalMath` follows the same rules as described in the [big-math](https://github.com/eobermuhlner/big-math#defaultbigdecimalmath) library.\n\nTo specify a temporary local `MathContext` you can either\nuse `createLocalMathContext()` method with `use`:\n```kotlin\ncreateLocalMathContext(10).use {\n    // do some calculations using DefaultBigDecimalMath\n}\n```\n\nAlternatively you can use the `withLocalMathContext()` method \n```kotlin\nwithLocalMathContext(10) {\n    // do some calculations using DefaultBigDecimalMath\n}\n```\n\nFull example:\n\n```kotlin\nimport java.math.BigDecimal\nimport java.math.BigDecimal.*\nimport ch.obermuhlner.math.big.DefaultBigDecimalMath.*\nimport ch.obermuhlner.math.big.kotlin.bigdecimal.*\n\nfun main(args: Array\u003cString\u003e) {\n\tsimpleExample()\n\tcreateLocalMathContextExample()\n\twithLocalMathContextExample()\n\tpiChudnovskyExample()\n}\n\nfun simpleExample() {\n\tval v1 = valueOf(2) / 3\n\n\tprintln(v1)\n}\n\nfun createLocalMathContextExample() {\n\tprintln(\"Pi[default]: \" + pi())\n\n\tcreateLocalMathContext(5).use {\n\t\tprintln(\"Pi[5]: \" + pi())\n\n\t\tcreateLocalMathContext(10).use {\n\t\t\tprintln(\"Pi[10]: \" + pi())\n\t\t}\n\n\t\tprintln(\"Pi[5]: \" + pi())\n\t}\n\n\tprintln(\"Pi[default]: \" + pi())\n}\n\nfun withLocalMathContextExample() {\n\tprintln(\"Pi[default]: \" + pi())\n\n\twithLocalMathContext(5) {\n\t\tprintln(\"Pi[5]: \" + pi())\n\n\t\twithLocalMathContext(10) {\n\t\t\tprintln(\"Pi[10]: \" + pi())\n\t\t}\n\n\t\tprintln(\"Pi[5]: \" + pi())\n\t}\n\n\tprintln(\"Pi[default]: \" + pi())\n}\n\nfun piChudnovskyExample() {\n\twithLocalMathContext(100) {\n\t\tprintln(piChudnovsky())\n\t}\n}\n\nfun piChudnovsky() : BigDecimal {\n\tval valueDivisor = (valueOf(640320) pow 3) / 24\n\n\tvar sumA = valueOf(1)\n\tvar sumB = valueOf(0)\n\n\tvar a = valueOf(1)\n\tvar dividendTerm1 = valueOf(5) // -(6*k - 5)\n\tvar dividendTerm2 = valueOf(-1) // 2*k - 1\n\tvar dividendTerm3 = valueOf(-1) // 6*k - 1\n\n\tval precision = currentMathContext().precision\n\tval iterationCount = (precision+13) / 14 + 1\n\tfor (k in 1 .. iterationCount) {\n\t\tdividendTerm1 += -6\n\t\tdividendTerm2 += 2\n\t\tdividendTerm3 += 6\n\t\t\n\t\tval dividend = dividendTerm1 * dividendTerm2 * dividendTerm3\n\t\tval divisor = (valueOf(k) pow 3) * valueDivisor\n\t\ta *= dividend / divisor\n\t\tval b = a * k\n\t\t\n\t\tsumA += a\n\t\tsumB += b\n\t}\n\t\n\tval factor = sqrt(valueOf(10005)) * 426880;\n\tval pi = factor / (sumA * 13591409 + sumB * 545140134);\n\t\n\treturn pi;\n}\n```\n\nThis produces the following output:\n```\n0.6666666666666666666666666666666667\nPi[default]: 3.141592653589793238462643383279503\nPi[5]: 3.1416\nPi[10]: 3.141592654\nPi[5]: 3.1416\nPi[default]: 3.141592653589793238462643383279503\nPi[default]: 3.141592653589793238462643383279503\nPi[5]: 3.1416\nPi[10]: 3.141592654\nPi[5]: 3.1416\nPi[default]: 3.141592653589793238462643383279503\n3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117069\n```\n\n\n# BigFloat\n\nThe `BigFloat` class is a wrapper around `BigDecimal` which simplifies the consistent usage of the `MathContext`\nand provides a simpler API for calculations.\n\nThis is an experimental implementation.\nYour mileage may vary.\n\nUse the following imports:\n```kotlin\nimport ch.obermuhlner.math.big.BigFloat\nimport ch.obermuhlner.math.big.BigFloat.*\nimport ch.obermuhlner.math.big.kotlin.bigfloat.*\n```\n\n## BigFloat Operators\n\n* `+` operator\n* `-` operator\n* `*` operator\n* `/` operator\n\n\n* `++` operator\n* `--` operator\n\n\n* `pow` infix method\n* `root` infix method\n\n### Using BigFloat in Kotlin\n\nThe usage of `BigFloat` follows the same rules as described in the [big-math](https://github.com/eobermuhlner/big-math#bigfloat) library.\n\n```kotlin\nimport ch.obermuhlner.math.big.BigFloat\nimport ch.obermuhlner.math.big.BigFloat.*\nimport ch.obermuhlner.math.big.kotlin.bigfloat.*\n\nfun main(args: Array\u003cString\u003e) {\n\tsimpleExample()\n\tpiExample()\n}\n\nfun simpleExample() {\n\tval context = context(100)\n\tval v1 = context.valueOf(2) / 3\n\n\tprintln(v1)\n}\n\nfun piExample() {\n\tprintln(piChudnovsky(100))\n}\n\nfun piChudnovsky(precision: Int) : BigFloat {\n\tval context = context(precision + 10)\n\t\n\tval valueDivisor = (context.valueOf(640320) pow 3) / 24\n\n\tvar sumA = context.valueOf(1)\n\tvar sumB = context.valueOf(0)\n\n\tvar a = context.valueOf(1)\n\tvar dividendTerm1 = context.valueOf(5) // -(6*k - 5)\n\tvar dividendTerm2 = context.valueOf(-1) // 2*k - 1\n\tvar dividendTerm3 = context.valueOf(-1) // 6*k - 1\n\t\n\tval iterationCount = (context.getPrecision()+13) / 14 + 1\n\tfor (k in 1 .. iterationCount) {\n\t\tdividendTerm1 += -6\n\t\tdividendTerm2 += 2\n\t\tdividendTerm3 += 6\n\t\t\n\t\tval dividend = dividendTerm1 * dividendTerm2 * dividendTerm3\n\t\tval divisor = (context.valueOf(k) pow 3) * valueDivisor\n\t\ta *= dividend / divisor\n\t\tval b = a * k\n\t\t\n\t\tsumA += a\n\t\tsumB += b\n\t}\n\t\n\tval factor = sqrt(context.valueOf(10005)) * 426880;\n\tval pi = factor / (sumA * 13591409 + sumB * 545140134);\n\t\n\treturn context(precision).valueOf(pi);\n}\n```\n\nThis produces the following output:\n```\n0.6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667\n3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068\n```\n\n## Comparison with standard kotlin library\n\nThe standard kotlin library already provides operators for `BigDecimal` but there are several problems with this solution.\n\n### Standard Kotlin: Precision problem in division\n\nThe precision of the standard operators is determined from the arguments.\nIn the case of the division this can lead to unexpected results!\n\n```kotlin\n    // Code example with standard kotlin library (without kotlin-big-math)\n    \n    val v = valueOf(2) / valueOf(3)\n    println(v) // prints: 1 !!!\n```\n\nThis problem is solved in `kotlin-big-math` because the precision is specified outside the function call.\n```kotlin\n    // Code example with kotlin-big-math\n\n    val v = valueOf(2) / valueOf(3)\n    println(v) // prints: 0.6666666666666666666666666666666667\n```\n\n### Standard Kotlin: Missing `valueOf(Int)` function\n\nThe Java `BigDecimal` class has two overloaded `valueOf()` functions for the types `Double` and `Long` but not for `Int`. \n```kotlin\n    // Code example with standard kotlin library (without kotlin-big-math)\n    \n    val n = 2\n    val v0 = valueOf(n) // Compile Error!\n```\nThis code does not compile but gives the following error:\n```\nCompile Error: None of the following functions can be called with the arguments supplied.\n   valueOf(Double) defined in java.math.BigDecimal\n   valueOf(Long) defined in java.math.BigDecimal\n```\n\nThis problem is solved in `kotlin-big-math` by providing a `fun valueOf(Int): BigDecimal`.\n```kotlin\n    // Code example with kotlin-big-math\n\n    val n = 2\n    val v = valueOf(n)\n```\n\n### Standard Kotlin: Missing overloaded operators for Int, Double, Long\n \nThe standard kotlin library does not provide overloaded operators for mixing `Int`, `Double`, `Long` with `BigDecimal`.\nThis leads to unnecessary clutter:\n```kotlin\n    // Code example with standard kotlin library (without kotlin-big-math)\n    \n    val v1 = valueOf(2)\n    val v2 = v1 * valueOf(3)\n    val v3 = valueOf(4) * v1\n```\n\nThis problem is solved in `kotlin-big-math` by providing a overloaded operators where one argument is `Int`, `Double` or `Long`.\n```kotlin\n    // Code example with kotlin-big-math\n\n    val v1 = valueOf(2)\n    val v2 = v1 * 3\n    val v3 = 4.0 * v1\n```\n\n\n## Using kotlin-big-math in your projects\n\nTo use the kotlin library you can either download the newest version of the .jar file from the\n[published releases on Github](https://github.com/eobermuhlner/kotlin-big-math/releases/)\nor use the following dependency to\n[Maven Central](https://search.maven.org/#search%7Cga%7C1%7Ckotlin-big-math)\nin your build script (please verify the version number to be the newest release):\n\n### Use kotlin-big-math in Maven Build\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ech.obermuhlner\u003c/groupId\u003e\n    \u003cartifactId\u003ekotlin-big-math\u003c/artifactId\u003e\n    \u003cversion\u003e2.3.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Use kotlin-big-math in Gradle Build\n\n```gradle\nrepositories {\n  mavenCentral()\n}\n\ndependencies {\n  compile 'ch.obermuhlner:kotlin-big-math:2.3.0'\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feobermuhlner%2Fkotlin-big-math","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feobermuhlner%2Fkotlin-big-math","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feobermuhlner%2Fkotlin-big-math/lists"}