scryptomath


Namescryptomath JSON
Version 0.0.6 PyPI version JSON
download
home_pageNone
SummaryMath library to support writing Python tests for Scrypto blueprints
upload_time2024-11-20 17:30:49
maintainerNone
docs_urlNone
authorNone
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": null,
    "name": "scryptomath",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "scrypto, math",
    "author": null,
    "author_email": "Ociswap <dev@ociswap.com>",
    "download_url": "https://files.pythonhosted.org/packages/24/15/de0183efdccfdd468dc04f781217b48fd9af16c158168c4070eb75a5b529/scryptomath-0.0.6.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.6",
    "project_urls": {
        "Homepage": "https://github.com/ociswap/scrypto-math"
    },
    "split_keywords": [
        "scrypto",
        " math"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c5fe45cc4caf5dbc2a4c84f85b30258fcc90a60047430511e01f4296a0d2d9c1",
                "md5": "f9b74f32163ee891cd76e17a2513fd5c",
                "sha256": "8602c1482caf36822e104337a743c404eccc846cfb684ed08279fd268d580cbb"
            },
            "downloads": -1,
            "filename": "scryptomath-0.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f9b74f32163ee891cd76e17a2513fd5c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 5829,
            "upload_time": "2024-11-20T17:30:47",
            "upload_time_iso_8601": "2024-11-20T17:30:47.862997Z",
            "url": "https://files.pythonhosted.org/packages/c5/fe/45cc4caf5dbc2a4c84f85b30258fcc90a60047430511e01f4296a0d2d9c1/scryptomath-0.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2415de0183efdccfdd468dc04f781217b48fd9af16c158168c4070eb75a5b529",
                "md5": "324abf4860f7ecb0bf48c27d69d7896b",
                "sha256": "4c59936b8548e070aa05822474abae06e482e2317b891ac77ec125eed4a5b0dc"
            },
            "downloads": -1,
            "filename": "scryptomath-0.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "324abf4860f7ecb0bf48c27d69d7896b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 5135,
            "upload_time": "2024-11-20T17:30:49",
            "upload_time_iso_8601": "2024-11-20T17:30:49.678837Z",
            "url": "https://files.pythonhosted.org/packages/24/15/de0183efdccfdd468dc04f781217b48fd9af16c158168c4070eb75a5b529/scryptomath-0.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-20 17:30:49",
    "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.65182s