{"id":16287503,"url":"https://github.com/alordash/fastexponentiation","last_synced_at":"2025-03-20T03:30:29.776Z","repository":{"id":114056925,"uuid":"408399609","full_name":"alordash/FastExponentiation","owner":"alordash","description":"Algorithms and functions for fast exponentiation with small error","archived":false,"fork":false,"pushed_at":"2022-01-30T12:38:14.000Z","size":18249,"stargazers_count":9,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T13:54:42.896Z","etag":null,"topics":["approximation","approximation-algorithms","exponent","exponentiation"],"latest_commit_sha":null,"homepage":"https://alordash.github.io/FastExponentiation/publish/wwwroot/","language":"CSS","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alordash.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-09-20T10:29:31.000Z","updated_at":"2024-03-07T18:17:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"e0f61861-3932-4770-b8b4-5017402efd15","html_url":"https://github.com/alordash/FastExponentiation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alordash%2FFastExponentiation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alordash%2FFastExponentiation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alordash%2FFastExponentiation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alordash%2FFastExponentiation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alordash","download_url":"https://codeload.github.com/alordash/FastExponentiation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244543684,"owners_count":20469541,"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":["approximation","approximation-algorithms","exponent","exponentiation"],"created_at":"2024-10-10T19:45:17.627Z","updated_at":"2025-03-20T03:30:29.756Z","avatar_url":"https://github.com/alordash.png","language":"CSS","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fast exponentiation\n\n## Description\n\nThis repository contains realizations of various algorithms for fast exponentiation with small error in C++, C# or Java.  \nMore info: [\"Ускоряем pow\"](https://habr.com/ru/post/584662/)\n\n## List of algorithms\n\n1. \"Old\" approximation\n2. Binary power\n3. Dividing fast power\n4. Fractional fast power\n5. \"Another\" approximation\n\n## Repository structure\n\nThere are 9 projects in **visual studio solution** and 5 java projects used to test custom algorithms, including [web page](https://alordash.github.io/FastExponentiation/publish/wwwroot/) for precision tests.\n\n\u003cimg src=\"Previews/Chart.png\" /\u003e\n\nYou can check results of performance measures in [this excel table](Performance%20results/Results.xlsx).  \nTests ran on: i5-10300H, 19.8 DDR4 GB of usable RAM, 64bit, single threaded  \nC++: MSVC + /O2 + /Oi + /Ot  \nC#: \"Optimize code\" option  \n\n\u003cimg src=\"Previews/С++_Speed.png\" /\u003e\n\n# Algorithms realizations\n\n## [\"Old\" approximation](https://habr.com/ru/company/infopulse/blog/336110/)\n\n### In [C++](https://github.com/alordash/FastExponentiation/blob/main/FastExponentiation/FastMathCpp/FastMath.cpp#L16)\n```c++\ndouble OldApproximatePower(double b, double e) {\n    union {\n        double d;\n        long long i;\n    } u = { b };\n    u.i = (long long)(4606853616395542500L + e * (u.i - 4606853616395542500L));\n    return u.d;\n}\n```\n\u003cdetails\u003e\n\u003csummary\u003eIn \u003ca href=\"https://github.com/alordash/FastExponentiation/blob/main/FastExponentiation/FastMath/FastMath.cs#L20\"\u003eC#\u003c/a\u003e\u003c/summary\u003e\n\n```C#\ndouble OldApproximatePower(double b, double e) {\n    long i = BitConverter.DoubleToInt64Bits(b);\n    i = (long)(FastMath.doubleApproximator + e * (i - FastMath.doubleApproximator));\n    return BitConverter.Int64BitsToDouble(i);\n}\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eIn \u003ca href=\"https://github.com/alordash/FastExponentiation/blob/main/Java/FastMath/src/FastMath.java#L18\"\u003eJava\u003c/a\u003e\u003c/summary\u003e\n\n```java\ndouble OldApproximatePower(double b, double e) {\n    long i = Double.doubleToLongBits(b);\n    i = (long) (FastMath.doubleApproximator + e * (i - FastMath.doubleApproximator));\n    return Double.longBitsToDouble(i);\n}\n```\n\u003c/details\u003e\n\n## [Binary power](https://en.wikipedia.org/wiki/Exponentiation_by_squaring)\n\n### In [C++](https://github.com/alordash/FastExponentiation/blob/main/FastExponentiation/FastMathCpp/FastMath.cpp#L3)\n```c++\ndouble BinaryPower(double b, unsigned long long e) {\n    double v = 1.0;\n    while(e != 0) {\n        if((e \u0026 1) != 0) {\n            v *= b;\n        }\n        b *= b;\n        e \u003e\u003e= 1;\n    }\n    return v;\n}\n```\n\u003cdetails\u003e\n\u003csummary\u003eIn \u003ca href=\"https://github.com/alordash/FastExponentiation/blob/main/FastExponentiation/FastMath/FastMath.cs#L4\"\u003eC#\u003c/a\u003e\u003c/summary\u003e\n\n```c#\ndouble BinaryPower(double b, UInt64 e) {\n    double v = 1d;\n    while(e != 0) {\n        if((e \u0026 1) != 0) {\n            v *= b;\n        }\n        b *= b;\n        e \u003e\u003e= 1;\n    }\n    return v;\n}\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eIn \u003ca href=\"https://github.com/alordash/FastExponentiation/blob/main/Java/FastMath/src/FastMath.java#L2\"\u003eJava\u003c/a\u003e\u003c/summary\u003e\n\n```java\ndouble BinaryPower(double b, long e) {\n    double v = 1d;\n    while (e \u003e 0) {\n        if ((e \u0026 1) != 0) {\n            v *= b;\n        }\n        b *= b;\n        e \u003e\u003e= 1;\n    }\n    return v;\n}\n```\n\u003c/details\u003e\n\n## Dividing fast power\n### In [C++](https://github.com/alordash/FastExponentiation/blob/main/FastExponentiation/FastMathCpp/FastMath.cpp#L28)\n```c++\ndouble FastPowerDividing(double b, double e) {\n    // To avoid undefined behaviour near key points,\n    // we can hardcode results for them, but this\n    // will make function slightly slower.\n    if(b == 1.0 || e == 0.0) {\n        return 1.0;\n    }\n\n    double eAbs = fabs(e);\n    double el = ceil(eAbs);\n    double basePart = OldApproximatePower(b, eAbs / el);\n\n    double result = BinaryPower(basePart, (unsigned long long)el);\n    // Because OldApproximatePower gives inaccurate results\n    // with negative exponent, we can increase precision\n    // by calculating exponent of a number in positive power\n    // and then dividing 1 by result of calculation\n    if(e \u003c 0.0) {\n        return 1.0 / result;\n    }\n    return result;\n}\n```\n\u003cdetails\u003e\n\u003csummary\u003eIn \u003ca href=\"https://github.com/alordash/FastExponentiation/blob/main/FastExponentiation/FastMath/FastMath.cs#L26\"\u003eC#\u003c/a\u003e\u003c/summary\u003e\n\n```c#\ndouble FastPowerDividing(double b, double e) {\n    if(b == 1d || e == 0d) {\n        return 1d;\n    }\n\n    var eAbs = Math.Abs(e);\n    var el = Math.Ceiling(eAbs);\n    var basePart = OldApproximatePower(b, eAbs / el);\n    var result = BinaryPower(basePart, (UInt64)el);\n    \n    if(e \u003c 0d) {\n        return 1d / result;\n    }\n    return result;\n}\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eIn \u003ca href=\"https://github.com/alordash/FastExponentiation/blob/main/Java/FastMath/src/FastMath.java#L24\"\u003eJava\u003c/a\u003e\u003c/summary\u003e\n\n```java\ndouble FastPowerDividing(double b, double e) {\n    if (b == 1d || e == 0d) {\n        return 1d;\n    }\n\n    var eAbs = Math.abs(e);\n    var el = Math.ceil(eAbs);\n    var basePart = OldApproximatePower(b, eAbs / el);\n    var result = BinaryPower(basePart, (long) el);\n    \n    if (e \u003c 0d) {\n        return 1d / result;\n    }\n    return result;\n}\n```\n\u003c/details\u003e\n\n## Fractional fast power\n### In [C++](https://github.com/alordash/FastExponentiation/blob/main/FastExponentiation/FastMathCpp/FastMath.cpp#L58)\n```c++\ndouble FastPowerFractional(double b, double e) {\n    if(b == 1.0 || e == 0.0) {\n        return 1.0;\n    }\n\n    double absExp = fabs(e);\n    unsigned long long eIntPart = (unsigned long long)absExp;\n    double eFractPart = absExp - eIntPart;\n    double result = OldApproximatePower(b, eFractPart) * BinaryPower(b, eIntPart);\n    \n    if(e \u003c 0.0) {\n        return 1.0 / result;\n    }\n    return result;\n}\n```\n\u003cdetails\u003e\n\u003csummary\u003eIn \u003ca href=\"https://github.com/alordash/FastExponentiation/blob/main/FastExponentiation/FastMath/FastMath.cs#L58\"\u003eC#\u003c/a\u003e\u003c/summary\u003e\n\n```c#\ndouble FastPowerFractional(double b, double e) {\n    if(b == 1d || e == 0d) {\n        return 1d;\n    }\n\n    double absExp = Math.Abs(e);\n    UInt64 eIntPart = (UInt64)absExp;\n    double eFractPart = absExp - eIntPart;\n    double result = OldApproximatePower(b, eFractPart) * BinaryPower(b, eIntPart);\n\n    if(e \u003c 0d) {\n        return 1d / result;\n    }\n    return result;\n}\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eIn \u003ca href=\"https://github.com/alordash/FastExponentiation/blob/main/Java/FastMath/src/FastMath.java#L53\"\u003eJava\u003c/a\u003e\u003c/summary\u003e\n\n```java\ndouble FastPowerFractional(double b, double e) {\n    if (b == 1d || e == 0d) {\n        return 1d;\n    }\n\n    double absExp = Math.abs(e);\n    long eIntPart = (long)absExp;\n    double eFractPart = absExp - eIntPart;\n    double result = OldApproximatePower(b, eFractPart) * BinaryPower(b, eIntPart);\n\n    if(e \u003c 0d) {\n        return 1d / result;\n    }\n    return result;\n}\n```\n\u003c/details\u003e\n\n## [Another approximation](https://martin.ankerl.com/2007/10/04/optimized-pow-approximation-for-java-and-c-c/)\n\n### In [C++](https://github.com/alordash/FastExponentiation/blob/main/FastExponentiation/FastMathCpp/FastMath.cpp#L77)\n```c++\ndouble AnotherApproximatePower(double a, double b) {\n    union {\n        double d;\n        int x[2];\n    } u = { a };\n    u.x[1] = (int)(b * (u.x[1] - 1072632447) + 1072632447);\n    u.x[0] = 0;\n    return u.d;\n}\n```\n\u003cdetails\u003e\n\u003csummary\u003eIn \u003ca href=\"https://github.com/alordash/FastExponentiation/blob/main/FastExponentiation/FastMath/FastMath.cs#L77\"\u003eC#\u003c/a\u003e\u003c/summary\u003e\n    \n```c#\ndouble AnotherApproxPower(double a, double b) {\n    int tmp = (int)(BitConverter.DoubleToInt64Bits(a) \u003e\u003e 32);\n    int tmp2 = (int)(b * (tmp - 1072632447) + 1072632447);\n    return BitConverter.Int64BitsToDouble(((long)tmp2) \u003c\u003c 32);\n}\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eIn \u003ca href=\"https://github.com/alordash/FastExponentiation/blob/main/Java/FastMath/src/FastMath.java#L72\"\u003eJava\u003c/a\u003e\u003c/summary\u003e\n\n```java\ndouble AnotherApproxPower(double a, double b) {\n    int tmp = (int)(Double.doubleToLongBits(a) \u003e\u003e 32);\n    int tmp2 = (int)(b * (tmp - 1072632447) + 1072632447);\n    return Double.longBitsToDouble(((long)tmp2) \u003c\u003c 32);\n}\n```\n\u003c/details\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falordash%2Ffastexponentiation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falordash%2Ffastexponentiation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falordash%2Ffastexponentiation/lists"}