scryptomath


Namescryptomath JSON
Version 0.0.4 PyPI version JSON
download
home_page
SummaryMath library to support writing Python tests for Scrypto blueprints
upload_time2024-03-13 02:31:51
maintainer
docs_urlNone
author
requires_python>=3.9
licenseMIT License Copyright (c) 2023 Ociswap Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords scrypto math
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # scryptomath for Python

## Why
In more complex Scrypto blueprints you need to calculate your test values against which you want to compare your outputs first.
A good approach to achieve that is to calculate them on a higher level without optimisations staying close to the mathematical specification.

This allows you to validate your mathematical specification more easily, including numerical precision boundaries, by investigating and resolving differences in the calculation results between the Scrypto and Python implementations.

## Features

### Fixed precision decimal types in Python
This library supports you implementing your Python prototype by providing Scrypto decimal types with fixed precision math in Python:
- `Decimal` (18 decimal places)
- `PreciseDecimal`  (36 decimal places)

Currently the following operations are implemented (more can be added):
- `__abs__`
- `__add__`
- `__sub__`
- `__mul__`
- `__truediv__`  ("normal" division)
- `__pow__`      (exact `pow` result)
- `sqrt()`       (exact `s`)
- `powi()`       (`powi` implementation Scrypto is using to match results and not identical to result of `__pow__`)

However, use this wisely if necessary only because depending on the use case you rather want to calculate your test assertion outputs with very high precision (like 500 decimal places) in Python first. That's what we are doing by default for Ociswap and only use the fixed decimal point types if it is required to match the test values.

Disclaimer: if you are using other operations than the ones above a normal Python `decimal.Decimal` with the full precision you have specified is returned. In that case the return value is not being trimmed down to the fixed decimal places of the Scrypto decimal.

### Code generation for decimal constants in Scrypto

Currently, the macros `dec!` and `pdec!` can't be used for constants in Scrypto.
To workaround that limitation we have implemented Scrypto code generation for initialising decimal constants directly in Scrypto:
```
>>> from scryptomath import Decimal
>>> Decimal(3).powi(4).scrypto
'Decimal(I192::from_digits([7213023705161793536, 4, 0]))'
```

Which can be directly used in your Scrypto blueprint like this:
```
const MY_CONST: Decimal = Decimal(I192::from_digits([7213023705161793536, 4, 0]));
```

Eventually, we assume Scrypto probably will support compile time macros for `dec!` and `pdec!`, but in the meantime this is working very well as a workaround.

## Contribute

If you have any requirements for additional data types just open an issue or pull request. Happy to add more :)

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "scryptomath",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "scrypto,math",
    "author": "",
    "author_email": "Ociswap <dev@ociswap.com>",
    "download_url": "https://files.pythonhosted.org/packages/b1/37/2b698d64e89cd4d211cab72ca3d19e082a1793cdea3d25fa2b9e601b53c6/scryptomath-0.0.4.tar.gz",
    "platform": null,
    "description": "# scryptomath for Python\n\n## Why\nIn more complex Scrypto blueprints you need to calculate your test values against which you want to compare your outputs first.\nA good approach to achieve that is to calculate them on a higher level without optimisations staying close to the mathematical specification.\n\nThis allows you to validate your mathematical specification more easily, including numerical precision boundaries, by investigating and resolving differences in the calculation results between the Scrypto and Python implementations.\n\n## Features\n\n### Fixed precision decimal types in Python\nThis library supports you implementing your Python prototype by providing Scrypto decimal types with fixed precision math in Python:\n- `Decimal` (18 decimal places)\n- `PreciseDecimal`  (36 decimal places)\n\nCurrently the following operations are implemented (more can be added):\n- `__abs__`\n- `__add__`\n- `__sub__`\n- `__mul__`\n- `__truediv__`  (\"normal\" division)\n- `__pow__`      (exact `pow` result)\n- `sqrt()`       (exact `s`)\n- `powi()`       (`powi` implementation Scrypto is using to match results and not identical to result of `__pow__`)\n\nHowever, use this wisely if necessary only because depending on the use case you rather want to calculate your test assertion outputs with very high precision (like 500 decimal places) in Python first. That's what we are doing by default for Ociswap and only use the fixed decimal point types if it is required to match the test values.\n\nDisclaimer: if you are using other operations than the ones above a normal Python `decimal.Decimal` with the full precision you have specified is returned. In that case the return value is not being trimmed down to the fixed decimal places of the Scrypto decimal.\n\n### Code generation for decimal constants in Scrypto\n\nCurrently, the macros `dec!` and `pdec!` can't be used for constants in Scrypto.\nTo workaround that limitation we have implemented Scrypto code generation for initialising decimal constants directly in Scrypto:\n```\n>>> from scryptomath import Decimal\n>>> Decimal(3).powi(4).scrypto\n'Decimal(I192::from_digits([7213023705161793536, 4, 0]))'\n```\n\nWhich can be directly used in your Scrypto blueprint like this:\n```\nconst MY_CONST: Decimal = Decimal(I192::from_digits([7213023705161793536, 4, 0]));\n```\n\nEventually, we assume Scrypto probably will support compile time macros for `dec!` and `pdec!`, but in the meantime this is working very well as a workaround.\n\n## Contribute\n\nIf you have any requirements for additional data types just open an issue or pull request. Happy to add more :)\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2023 Ociswap  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Math library to support writing Python tests for Scrypto blueprints",
    "version": "0.0.4",
    "project_urls": {
        "Homepage": "https://github.com/ociswap/scrypto-math"
    },
    "split_keywords": [
        "scrypto",
        "math"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "92d8785ef1516c7d56b23c36baa61cc08d928d606fea1f1e55321e5142ed0e6f",
                "md5": "e90a2523ff541376509a4ea847586443",
                "sha256": "01a6fefa5cd42f07a81ea4cda29eac0865508469c57631cce25d34c422fc57cd"
            },
            "downloads": -1,
            "filename": "scryptomath-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e90a2523ff541376509a4ea847586443",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 5083,
            "upload_time": "2024-03-13T02:31:49",
            "upload_time_iso_8601": "2024-03-13T02:31:49.765252Z",
            "url": "https://files.pythonhosted.org/packages/92/d8/785ef1516c7d56b23c36baa61cc08d928d606fea1f1e55321e5142ed0e6f/scryptomath-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b1372b698d64e89cd4d211cab72ca3d19e082a1793cdea3d25fa2b9e601b53c6",
                "md5": "f9d54a71226efc2d19a5a0c877f506c8",
                "sha256": "eaa13991abb6b798abb80782fd86b7868473417a14dcd6c6f162d6541102aba7"
            },
            "downloads": -1,
            "filename": "scryptomath-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "f9d54a71226efc2d19a5a0c877f506c8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 4448,
            "upload_time": "2024-03-13T02:31:51",
            "upload_time_iso_8601": "2024-03-13T02:31:51.385170Z",
            "url": "https://files.pythonhosted.org/packages/b1/37/2b698d64e89cd4d211cab72ca3d19e082a1793cdea3d25fa2b9e601b53c6/scryptomath-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-13 02:31:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ociswap",
    "github_project": "scrypto-math",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "scryptomath"
}
        
Elapsed time: 0.21838s