yut23-utils 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- yut23_utils-0.1.0/.github/workflows/test.yml +39 -0
- yut23_utils-0.1.0/.gitignore +160 -0
- yut23_utils-0.1.0/LICENSE.txt +11 -0
- yut23_utils-0.1.0/PKG-INFO +42 -0
- yut23_utils-0.1.0/README.md +21 -0
- yut23_utils-0.1.0/pyproject.toml +103 -0
- yut23_utils-0.1.0/ruff_defaults.toml +534 -0
- yut23_utils-0.1.0/src/yut23_utils/__about__.py +4 -0
- yut23_utils-0.1.0/src/yut23_utils/__init__.py +5 -0
- yut23_utils-0.1.0/src/yut23_utils/fp.py +107 -0
- yut23_utils-0.1.0/tests/__init__.py +3 -0
- yut23_utils-0.1.0/tests/test_fp.py +117 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
name: test
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
concurrency:
|
|
10
|
+
group: test-${{ github.head_ref }}
|
|
11
|
+
cancel-in-progress: true
|
|
12
|
+
|
|
13
|
+
env:
|
|
14
|
+
PYTHONUNBUFFERED: "1"
|
|
15
|
+
FORCE_COLOR: "1"
|
|
16
|
+
|
|
17
|
+
jobs:
|
|
18
|
+
run:
|
|
19
|
+
name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }}
|
|
20
|
+
runs-on: ${{ matrix.os }}
|
|
21
|
+
strategy:
|
|
22
|
+
fail-fast: false
|
|
23
|
+
matrix:
|
|
24
|
+
os: [ubuntu-latest, windows-latest, macos-latest]
|
|
25
|
+
python-version: ['3.9', '3.10', '3.11', '3.12']
|
|
26
|
+
|
|
27
|
+
steps:
|
|
28
|
+
- uses: actions/checkout@v4
|
|
29
|
+
|
|
30
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
31
|
+
uses: actions/setup-python@v4
|
|
32
|
+
with:
|
|
33
|
+
python-version: ${{ matrix.python-version }}
|
|
34
|
+
|
|
35
|
+
- name: Install Hatch
|
|
36
|
+
run: pip install --upgrade hatch
|
|
37
|
+
|
|
38
|
+
- name: Run tests
|
|
39
|
+
run: hatch run cov
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# poetry
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
102
|
+
#poetry.lock
|
|
103
|
+
|
|
104
|
+
# pdm
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
106
|
+
#pdm.lock
|
|
107
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
108
|
+
# in version control.
|
|
109
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
110
|
+
.pdm.toml
|
|
111
|
+
|
|
112
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
113
|
+
__pypackages__/
|
|
114
|
+
|
|
115
|
+
# Celery stuff
|
|
116
|
+
celerybeat-schedule
|
|
117
|
+
celerybeat.pid
|
|
118
|
+
|
|
119
|
+
# SageMath parsed files
|
|
120
|
+
*.sage.py
|
|
121
|
+
|
|
122
|
+
# Environments
|
|
123
|
+
.env
|
|
124
|
+
.venv
|
|
125
|
+
env/
|
|
126
|
+
venv/
|
|
127
|
+
ENV/
|
|
128
|
+
env.bak/
|
|
129
|
+
venv.bak/
|
|
130
|
+
|
|
131
|
+
# Spyder project settings
|
|
132
|
+
.spyderproject
|
|
133
|
+
.spyproject
|
|
134
|
+
|
|
135
|
+
# Rope project settings
|
|
136
|
+
.ropeproject
|
|
137
|
+
|
|
138
|
+
# mkdocs documentation
|
|
139
|
+
/site
|
|
140
|
+
|
|
141
|
+
# mypy
|
|
142
|
+
.mypy_cache/
|
|
143
|
+
.dmypy.json
|
|
144
|
+
dmypy.json
|
|
145
|
+
|
|
146
|
+
# Pyre type checker
|
|
147
|
+
.pyre/
|
|
148
|
+
|
|
149
|
+
# pytype static type analyzer
|
|
150
|
+
.pytype/
|
|
151
|
+
|
|
152
|
+
# Cython debug symbols
|
|
153
|
+
cython_debug/
|
|
154
|
+
|
|
155
|
+
# PyCharm
|
|
156
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
157
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
158
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
159
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
160
|
+
#.idea/
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Copyright (c) 2023-present Eric T. Johnson.
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: yut23-utils
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Common code I find myself reaching for from multiple different repositories.
|
|
5
|
+
Project-URL: Documentation, https://github.com/yut23/yut23-utils#readme
|
|
6
|
+
Project-URL: Issues, https://github.com/yut23/yut23-utils/issues
|
|
7
|
+
Project-URL: Source, https://github.com/yut23/yut23-utils
|
|
8
|
+
Author-email: yut23 <yut23@gvljohnsons.com>
|
|
9
|
+
License-Expression: BSD-3-Clause
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
18
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# yut23_utils
|
|
23
|
+
|
|
24
|
+
[](https://pypi.org/project/yut23-utils)
|
|
25
|
+
[](https://pypi.org/project/yut23-utils)
|
|
26
|
+
|
|
27
|
+
-----
|
|
28
|
+
|
|
29
|
+
**Table of Contents**
|
|
30
|
+
|
|
31
|
+
- [Installation](#installation)
|
|
32
|
+
- [License](#license)
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```console
|
|
37
|
+
pip install yut23-utils
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
`yut23-utils` is distributed under the terms of the [BSD-3-Clause](https://spdx.org/licenses/BSD-3-Clause.html) license.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# yut23_utils
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/yut23-utils)
|
|
4
|
+
[](https://pypi.org/project/yut23-utils)
|
|
5
|
+
|
|
6
|
+
-----
|
|
7
|
+
|
|
8
|
+
**Table of Contents**
|
|
9
|
+
|
|
10
|
+
- [Installation](#installation)
|
|
11
|
+
- [License](#license)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```console
|
|
16
|
+
pip install yut23-utils
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## License
|
|
20
|
+
|
|
21
|
+
`yut23-utils` is distributed under the terms of the [BSD-3-Clause](https://spdx.org/licenses/BSD-3-Clause.html) license.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling", "hatch-regex-commit"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "yut23-utils"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = 'Common code I find myself reaching for from multiple different repositories.'
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "BSD-3-Clause"
|
|
12
|
+
keywords = []
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "yut23", email = "yut23@gvljohnsons.com" },
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Programming Language :: Python",
|
|
19
|
+
"Programming Language :: Python :: 3.9",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: Implementation :: CPython",
|
|
24
|
+
"Programming Language :: Python :: Implementation :: PyPy",
|
|
25
|
+
]
|
|
26
|
+
dependencies = []
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Documentation = "https://github.com/yut23/yut23-utils#readme"
|
|
30
|
+
Issues = "https://github.com/yut23/yut23-utils/issues"
|
|
31
|
+
Source = "https://github.com/yut23/yut23-utils"
|
|
32
|
+
|
|
33
|
+
[tool.hatch.version]
|
|
34
|
+
source = "regex_commit"
|
|
35
|
+
path = "src/yut23_utils/__about__.py"
|
|
36
|
+
tag_sign = false
|
|
37
|
+
|
|
38
|
+
[tool.hatch.envs.default]
|
|
39
|
+
dependencies = [
|
|
40
|
+
"coverage[toml]>=6.5",
|
|
41
|
+
"pytest",
|
|
42
|
+
"hypothesis",
|
|
43
|
+
]
|
|
44
|
+
[tool.hatch.envs.default.scripts]
|
|
45
|
+
test = "pytest {args:tests}"
|
|
46
|
+
test-cov = "coverage run -m pytest {args:tests}"
|
|
47
|
+
cov-report = [
|
|
48
|
+
"- coverage combine",
|
|
49
|
+
"coverage report",
|
|
50
|
+
]
|
|
51
|
+
cov = [
|
|
52
|
+
"test-cov",
|
|
53
|
+
"cov-report",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
[[tool.hatch.envs.all.matrix]]
|
|
57
|
+
python = ["3.9", "3.10", "3.11", "3.12"]
|
|
58
|
+
|
|
59
|
+
[tool.hatch.envs.types]
|
|
60
|
+
dependencies = [
|
|
61
|
+
"mypy>=1.0.0",
|
|
62
|
+
]
|
|
63
|
+
[tool.hatch.envs.types.scripts]
|
|
64
|
+
check = "mypy --install-types --non-interactive {args:src/yut23_utils tests}"
|
|
65
|
+
|
|
66
|
+
[tool.isort]
|
|
67
|
+
known_first_party = ["yut23_utils"]
|
|
68
|
+
|
|
69
|
+
[tool.black]
|
|
70
|
+
target-version = ["py39"]
|
|
71
|
+
line-length = 88
|
|
72
|
+
skip-string-normalization = false
|
|
73
|
+
|
|
74
|
+
[tool.ruff]
|
|
75
|
+
target-version = "py39"
|
|
76
|
+
line-length = 88
|
|
77
|
+
extend = "ruff_defaults.toml"
|
|
78
|
+
|
|
79
|
+
[tool.hatch.envs.hatch-static-analysis]
|
|
80
|
+
config-path = "ruff_defaults.toml"
|
|
81
|
+
|
|
82
|
+
[tool.ruff.lint.extend-per-file-ignores]
|
|
83
|
+
# we expect unused imports in __init__.py, as it shouldn't have anything else
|
|
84
|
+
"__init__.py" = ["F401"]
|
|
85
|
+
|
|
86
|
+
[tool.coverage.run]
|
|
87
|
+
source_pkgs = ["yut23_utils", "tests"]
|
|
88
|
+
branch = true
|
|
89
|
+
parallel = true
|
|
90
|
+
omit = [
|
|
91
|
+
"src/yut23_utils/__about__.py",
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
[tool.coverage.paths]
|
|
95
|
+
yut23_utils = ["src/yut23_utils", "*/yut23-utils/src/yut23_utils"]
|
|
96
|
+
tests = ["tests", "*/yut23-utils/tests"]
|
|
97
|
+
|
|
98
|
+
[tool.coverage.report]
|
|
99
|
+
exclude_lines = [
|
|
100
|
+
"no cov",
|
|
101
|
+
"if __name__ == .__main__.:",
|
|
102
|
+
"if TYPE_CHECKING:",
|
|
103
|
+
]
|
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
line-length = 120
|
|
2
|
+
|
|
3
|
+
[format]
|
|
4
|
+
docstring-code-format = true
|
|
5
|
+
docstring-code-line-length = 80
|
|
6
|
+
|
|
7
|
+
[lint]
|
|
8
|
+
select = [
|
|
9
|
+
"A001",
|
|
10
|
+
"A002",
|
|
11
|
+
"A003",
|
|
12
|
+
"ARG001",
|
|
13
|
+
"ARG002",
|
|
14
|
+
"ARG003",
|
|
15
|
+
"ARG004",
|
|
16
|
+
"ARG005",
|
|
17
|
+
"ASYNC100",
|
|
18
|
+
"ASYNC101",
|
|
19
|
+
"ASYNC102",
|
|
20
|
+
"B002",
|
|
21
|
+
"B003",
|
|
22
|
+
"B004",
|
|
23
|
+
"B005",
|
|
24
|
+
"B006",
|
|
25
|
+
"B007",
|
|
26
|
+
"B008",
|
|
27
|
+
"B009",
|
|
28
|
+
"B010",
|
|
29
|
+
"B011",
|
|
30
|
+
"B012",
|
|
31
|
+
"B013",
|
|
32
|
+
"B014",
|
|
33
|
+
"B015",
|
|
34
|
+
"B016",
|
|
35
|
+
"B017",
|
|
36
|
+
"B018",
|
|
37
|
+
"B019",
|
|
38
|
+
"B020",
|
|
39
|
+
"B021",
|
|
40
|
+
"B022",
|
|
41
|
+
"B023",
|
|
42
|
+
"B024",
|
|
43
|
+
"B025",
|
|
44
|
+
"B026",
|
|
45
|
+
"B028",
|
|
46
|
+
"B029",
|
|
47
|
+
"B030",
|
|
48
|
+
"B031",
|
|
49
|
+
"B032",
|
|
50
|
+
"B033",
|
|
51
|
+
"B034",
|
|
52
|
+
"B904",
|
|
53
|
+
"B905",
|
|
54
|
+
"BLE001",
|
|
55
|
+
"C400",
|
|
56
|
+
"C401",
|
|
57
|
+
"C402",
|
|
58
|
+
"C403",
|
|
59
|
+
"C404",
|
|
60
|
+
"C405",
|
|
61
|
+
"C406",
|
|
62
|
+
"C408",
|
|
63
|
+
"C409",
|
|
64
|
+
"C410",
|
|
65
|
+
"C411",
|
|
66
|
+
"C413",
|
|
67
|
+
"C414",
|
|
68
|
+
"C415",
|
|
69
|
+
"C416",
|
|
70
|
+
"C417",
|
|
71
|
+
"C418",
|
|
72
|
+
"C419",
|
|
73
|
+
"COM818",
|
|
74
|
+
"DTZ001",
|
|
75
|
+
"DTZ002",
|
|
76
|
+
"DTZ003",
|
|
77
|
+
"DTZ004",
|
|
78
|
+
"DTZ005",
|
|
79
|
+
"DTZ006",
|
|
80
|
+
"DTZ007",
|
|
81
|
+
"DTZ011",
|
|
82
|
+
"DTZ012",
|
|
83
|
+
"E101",
|
|
84
|
+
"E401",
|
|
85
|
+
"E402",
|
|
86
|
+
"E501",
|
|
87
|
+
"E701",
|
|
88
|
+
"E702",
|
|
89
|
+
"E703",
|
|
90
|
+
"E711",
|
|
91
|
+
"E712",
|
|
92
|
+
"E713",
|
|
93
|
+
"E714",
|
|
94
|
+
"E721",
|
|
95
|
+
"E722",
|
|
96
|
+
"E731",
|
|
97
|
+
"E741",
|
|
98
|
+
"E742",
|
|
99
|
+
"E743",
|
|
100
|
+
"E902",
|
|
101
|
+
"E999",
|
|
102
|
+
"EM101",
|
|
103
|
+
"EM102",
|
|
104
|
+
"EM103",
|
|
105
|
+
"EXE001",
|
|
106
|
+
"EXE002",
|
|
107
|
+
"EXE003",
|
|
108
|
+
"EXE004",
|
|
109
|
+
"EXE005",
|
|
110
|
+
"F401",
|
|
111
|
+
"F402",
|
|
112
|
+
"F403",
|
|
113
|
+
"F404",
|
|
114
|
+
"F405",
|
|
115
|
+
"F406",
|
|
116
|
+
"F407",
|
|
117
|
+
"F501",
|
|
118
|
+
"F502",
|
|
119
|
+
"F503",
|
|
120
|
+
"F504",
|
|
121
|
+
"F505",
|
|
122
|
+
"F506",
|
|
123
|
+
"F507",
|
|
124
|
+
"F508",
|
|
125
|
+
"F509",
|
|
126
|
+
"F521",
|
|
127
|
+
"F522",
|
|
128
|
+
"F523",
|
|
129
|
+
"F524",
|
|
130
|
+
"F525",
|
|
131
|
+
"F541",
|
|
132
|
+
"F601",
|
|
133
|
+
"F602",
|
|
134
|
+
"F621",
|
|
135
|
+
"F622",
|
|
136
|
+
"F631",
|
|
137
|
+
"F632",
|
|
138
|
+
"F633",
|
|
139
|
+
"F634",
|
|
140
|
+
"F701",
|
|
141
|
+
"F702",
|
|
142
|
+
"F704",
|
|
143
|
+
"F706",
|
|
144
|
+
"F707",
|
|
145
|
+
"F722",
|
|
146
|
+
"F811",
|
|
147
|
+
"F821",
|
|
148
|
+
"F822",
|
|
149
|
+
"F823",
|
|
150
|
+
"F841",
|
|
151
|
+
"F842",
|
|
152
|
+
"F901",
|
|
153
|
+
"FA100",
|
|
154
|
+
"FA102",
|
|
155
|
+
"FBT001",
|
|
156
|
+
"FBT002",
|
|
157
|
+
"FLY002",
|
|
158
|
+
"G001",
|
|
159
|
+
"G002",
|
|
160
|
+
"G003",
|
|
161
|
+
"G004",
|
|
162
|
+
"G010",
|
|
163
|
+
"G101",
|
|
164
|
+
"G201",
|
|
165
|
+
"G202",
|
|
166
|
+
"I001",
|
|
167
|
+
"I002",
|
|
168
|
+
"ICN001",
|
|
169
|
+
"ICN002",
|
|
170
|
+
"ICN003",
|
|
171
|
+
"INP001",
|
|
172
|
+
"INT001",
|
|
173
|
+
"INT002",
|
|
174
|
+
"INT003",
|
|
175
|
+
"ISC003",
|
|
176
|
+
"N801",
|
|
177
|
+
"N802",
|
|
178
|
+
"N803",
|
|
179
|
+
"N804",
|
|
180
|
+
"N805",
|
|
181
|
+
"N806",
|
|
182
|
+
"N807",
|
|
183
|
+
"N811",
|
|
184
|
+
"N812",
|
|
185
|
+
"N813",
|
|
186
|
+
"N814",
|
|
187
|
+
"N815",
|
|
188
|
+
"N816",
|
|
189
|
+
"N817",
|
|
190
|
+
"N818",
|
|
191
|
+
"N999",
|
|
192
|
+
"PERF101",
|
|
193
|
+
"PERF102",
|
|
194
|
+
"PERF401",
|
|
195
|
+
"PERF402",
|
|
196
|
+
"PGH001",
|
|
197
|
+
"PGH002",
|
|
198
|
+
"PGH005",
|
|
199
|
+
"PIE790",
|
|
200
|
+
"PIE794",
|
|
201
|
+
"PIE796",
|
|
202
|
+
"PIE800",
|
|
203
|
+
"PIE804",
|
|
204
|
+
"PIE807",
|
|
205
|
+
"PIE808",
|
|
206
|
+
"PIE810",
|
|
207
|
+
"PLC0105",
|
|
208
|
+
"PLC0131",
|
|
209
|
+
"PLC0132",
|
|
210
|
+
"PLC0205",
|
|
211
|
+
"PLC0208",
|
|
212
|
+
"PLC0414",
|
|
213
|
+
"PLC3002",
|
|
214
|
+
"PLE0100",
|
|
215
|
+
"PLE0101",
|
|
216
|
+
"PLE0116",
|
|
217
|
+
"PLE0117",
|
|
218
|
+
"PLE0118",
|
|
219
|
+
"PLE0241",
|
|
220
|
+
"PLE0302",
|
|
221
|
+
"PLE0307",
|
|
222
|
+
"PLE0604",
|
|
223
|
+
"PLE0605",
|
|
224
|
+
"PLE1142",
|
|
225
|
+
"PLE1205",
|
|
226
|
+
"PLE1206",
|
|
227
|
+
"PLE1300",
|
|
228
|
+
"PLE1307",
|
|
229
|
+
"PLE1310",
|
|
230
|
+
"PLE1507",
|
|
231
|
+
"PLE1700",
|
|
232
|
+
"PLE2502",
|
|
233
|
+
"PLE2510",
|
|
234
|
+
"PLE2512",
|
|
235
|
+
"PLE2513",
|
|
236
|
+
"PLE2514",
|
|
237
|
+
"PLE2515",
|
|
238
|
+
"PLR0124",
|
|
239
|
+
"PLR0133",
|
|
240
|
+
"PLR0206",
|
|
241
|
+
"PLR0402",
|
|
242
|
+
"PLR1701",
|
|
243
|
+
"PLR1711",
|
|
244
|
+
"PLR1714",
|
|
245
|
+
"PLR1722",
|
|
246
|
+
"PLR2004",
|
|
247
|
+
"PLR5501",
|
|
248
|
+
"PLW0120",
|
|
249
|
+
"PLW0127",
|
|
250
|
+
"PLW0129",
|
|
251
|
+
"PLW0131",
|
|
252
|
+
"PLW0406",
|
|
253
|
+
"PLW0602",
|
|
254
|
+
"PLW0603",
|
|
255
|
+
"PLW0711",
|
|
256
|
+
"PLW1508",
|
|
257
|
+
"PLW1509",
|
|
258
|
+
"PLW1510",
|
|
259
|
+
"PLW2901",
|
|
260
|
+
"PLW3301",
|
|
261
|
+
"PT001",
|
|
262
|
+
"PT002",
|
|
263
|
+
"PT003",
|
|
264
|
+
"PT006",
|
|
265
|
+
"PT007",
|
|
266
|
+
"PT008",
|
|
267
|
+
"PT009",
|
|
268
|
+
"PT010",
|
|
269
|
+
"PT011",
|
|
270
|
+
"PT012",
|
|
271
|
+
"PT013",
|
|
272
|
+
"PT014",
|
|
273
|
+
"PT015",
|
|
274
|
+
"PT016",
|
|
275
|
+
"PT017",
|
|
276
|
+
"PT018",
|
|
277
|
+
"PT019",
|
|
278
|
+
"PT020",
|
|
279
|
+
"PT021",
|
|
280
|
+
"PT022",
|
|
281
|
+
"PT023",
|
|
282
|
+
"PT024",
|
|
283
|
+
"PT025",
|
|
284
|
+
"PT026",
|
|
285
|
+
"PT027",
|
|
286
|
+
"PYI001",
|
|
287
|
+
"PYI002",
|
|
288
|
+
"PYI003",
|
|
289
|
+
"PYI004",
|
|
290
|
+
"PYI005",
|
|
291
|
+
"PYI006",
|
|
292
|
+
"PYI007",
|
|
293
|
+
"PYI008",
|
|
294
|
+
"PYI009",
|
|
295
|
+
"PYI010",
|
|
296
|
+
"PYI011",
|
|
297
|
+
"PYI012",
|
|
298
|
+
"PYI013",
|
|
299
|
+
"PYI014",
|
|
300
|
+
"PYI015",
|
|
301
|
+
"PYI016",
|
|
302
|
+
"PYI017",
|
|
303
|
+
"PYI018",
|
|
304
|
+
"PYI019",
|
|
305
|
+
"PYI020",
|
|
306
|
+
"PYI021",
|
|
307
|
+
"PYI024",
|
|
308
|
+
"PYI025",
|
|
309
|
+
"PYI026",
|
|
310
|
+
"PYI029",
|
|
311
|
+
"PYI030",
|
|
312
|
+
"PYI032",
|
|
313
|
+
"PYI033",
|
|
314
|
+
"PYI034",
|
|
315
|
+
"PYI035",
|
|
316
|
+
"PYI036",
|
|
317
|
+
"PYI041",
|
|
318
|
+
"PYI042",
|
|
319
|
+
"PYI043",
|
|
320
|
+
"PYI044",
|
|
321
|
+
"PYI045",
|
|
322
|
+
"PYI046",
|
|
323
|
+
"PYI047",
|
|
324
|
+
"PYI048",
|
|
325
|
+
"PYI049",
|
|
326
|
+
"PYI050",
|
|
327
|
+
"PYI051",
|
|
328
|
+
"PYI052",
|
|
329
|
+
"PYI053",
|
|
330
|
+
"PYI054",
|
|
331
|
+
"PYI055",
|
|
332
|
+
"PYI056",
|
|
333
|
+
"RET503",
|
|
334
|
+
"RET504",
|
|
335
|
+
"RET505",
|
|
336
|
+
"RET506",
|
|
337
|
+
"RET507",
|
|
338
|
+
"RET508",
|
|
339
|
+
"RSE102",
|
|
340
|
+
"RUF001",
|
|
341
|
+
"RUF002",
|
|
342
|
+
"RUF003",
|
|
343
|
+
"RUF005",
|
|
344
|
+
"RUF006",
|
|
345
|
+
"RUF007",
|
|
346
|
+
"RUF008",
|
|
347
|
+
"RUF009",
|
|
348
|
+
"RUF010",
|
|
349
|
+
"RUF011",
|
|
350
|
+
"RUF012",
|
|
351
|
+
"RUF013",
|
|
352
|
+
"RUF015",
|
|
353
|
+
"RUF016",
|
|
354
|
+
"RUF100",
|
|
355
|
+
"RUF200",
|
|
356
|
+
"S101",
|
|
357
|
+
"S102",
|
|
358
|
+
"S103",
|
|
359
|
+
"S104",
|
|
360
|
+
"S105",
|
|
361
|
+
"S106",
|
|
362
|
+
"S107",
|
|
363
|
+
"S108",
|
|
364
|
+
"S110",
|
|
365
|
+
"S112",
|
|
366
|
+
"S113",
|
|
367
|
+
"S301",
|
|
368
|
+
"S302",
|
|
369
|
+
"S303",
|
|
370
|
+
"S304",
|
|
371
|
+
"S305",
|
|
372
|
+
"S306",
|
|
373
|
+
"S307",
|
|
374
|
+
"S308",
|
|
375
|
+
"S310",
|
|
376
|
+
"S311",
|
|
377
|
+
"S312",
|
|
378
|
+
"S313",
|
|
379
|
+
"S314",
|
|
380
|
+
"S315",
|
|
381
|
+
"S316",
|
|
382
|
+
"S317",
|
|
383
|
+
"S318",
|
|
384
|
+
"S319",
|
|
385
|
+
"S320",
|
|
386
|
+
"S321",
|
|
387
|
+
"S323",
|
|
388
|
+
"S324",
|
|
389
|
+
"S501",
|
|
390
|
+
"S506",
|
|
391
|
+
"S508",
|
|
392
|
+
"S509",
|
|
393
|
+
"S601",
|
|
394
|
+
"S602",
|
|
395
|
+
"S604",
|
|
396
|
+
"S605",
|
|
397
|
+
"S606",
|
|
398
|
+
"S607",
|
|
399
|
+
"S608",
|
|
400
|
+
"S609",
|
|
401
|
+
"S612",
|
|
402
|
+
"S701",
|
|
403
|
+
"SIM101",
|
|
404
|
+
"SIM102",
|
|
405
|
+
"SIM103",
|
|
406
|
+
"SIM105",
|
|
407
|
+
"SIM107",
|
|
408
|
+
"SIM108",
|
|
409
|
+
"SIM109",
|
|
410
|
+
"SIM110",
|
|
411
|
+
"SIM112",
|
|
412
|
+
"SIM114",
|
|
413
|
+
"SIM115",
|
|
414
|
+
"SIM116",
|
|
415
|
+
"SIM117",
|
|
416
|
+
"SIM118",
|
|
417
|
+
"SIM201",
|
|
418
|
+
"SIM202",
|
|
419
|
+
"SIM208",
|
|
420
|
+
"SIM210",
|
|
421
|
+
"SIM211",
|
|
422
|
+
"SIM212",
|
|
423
|
+
"SIM220",
|
|
424
|
+
"SIM221",
|
|
425
|
+
"SIM222",
|
|
426
|
+
"SIM223",
|
|
427
|
+
"SIM300",
|
|
428
|
+
"SIM910",
|
|
429
|
+
"SLF001",
|
|
430
|
+
"SLOT000",
|
|
431
|
+
"SLOT001",
|
|
432
|
+
"SLOT002",
|
|
433
|
+
"T100",
|
|
434
|
+
"T201",
|
|
435
|
+
"T203",
|
|
436
|
+
"TCH001",
|
|
437
|
+
"TCH002",
|
|
438
|
+
"TCH003",
|
|
439
|
+
"TCH004",
|
|
440
|
+
"TCH005",
|
|
441
|
+
"TD004",
|
|
442
|
+
"TD005",
|
|
443
|
+
"TD006",
|
|
444
|
+
"TD007",
|
|
445
|
+
"TID251",
|
|
446
|
+
"TID252",
|
|
447
|
+
"TID253",
|
|
448
|
+
"TRY002",
|
|
449
|
+
"TRY003",
|
|
450
|
+
"TRY004",
|
|
451
|
+
"TRY200",
|
|
452
|
+
"TRY201",
|
|
453
|
+
"TRY300",
|
|
454
|
+
"TRY301",
|
|
455
|
+
"TRY302",
|
|
456
|
+
"TRY400",
|
|
457
|
+
"TRY401",
|
|
458
|
+
"UP001",
|
|
459
|
+
"UP003",
|
|
460
|
+
"UP004",
|
|
461
|
+
"UP005",
|
|
462
|
+
"UP006",
|
|
463
|
+
"UP007",
|
|
464
|
+
"UP008",
|
|
465
|
+
"UP009",
|
|
466
|
+
"UP010",
|
|
467
|
+
"UP011",
|
|
468
|
+
"UP012",
|
|
469
|
+
"UP013",
|
|
470
|
+
"UP014",
|
|
471
|
+
"UP015",
|
|
472
|
+
"UP017",
|
|
473
|
+
"UP018",
|
|
474
|
+
"UP019",
|
|
475
|
+
"UP020",
|
|
476
|
+
"UP021",
|
|
477
|
+
"UP022",
|
|
478
|
+
"UP023",
|
|
479
|
+
"UP024",
|
|
480
|
+
"UP025",
|
|
481
|
+
"UP026",
|
|
482
|
+
"UP027",
|
|
483
|
+
"UP028",
|
|
484
|
+
"UP029",
|
|
485
|
+
"UP030",
|
|
486
|
+
"UP031",
|
|
487
|
+
"UP032",
|
|
488
|
+
"UP033",
|
|
489
|
+
"UP034",
|
|
490
|
+
"UP035",
|
|
491
|
+
"UP036",
|
|
492
|
+
"UP037",
|
|
493
|
+
"UP038",
|
|
494
|
+
"UP039",
|
|
495
|
+
"UP040",
|
|
496
|
+
"W291",
|
|
497
|
+
"W292",
|
|
498
|
+
"W293",
|
|
499
|
+
"W505",
|
|
500
|
+
"W605",
|
|
501
|
+
"YTT101",
|
|
502
|
+
"YTT102",
|
|
503
|
+
"YTT103",
|
|
504
|
+
"YTT201",
|
|
505
|
+
"YTT202",
|
|
506
|
+
"YTT203",
|
|
507
|
+
"YTT204",
|
|
508
|
+
"YTT301",
|
|
509
|
+
"YTT302",
|
|
510
|
+
"YTT303",
|
|
511
|
+
]
|
|
512
|
+
|
|
513
|
+
[lint.per-file-ignores]
|
|
514
|
+
"**/scripts/*" = [
|
|
515
|
+
"INP001",
|
|
516
|
+
"T201",
|
|
517
|
+
]
|
|
518
|
+
"**/tests/**/*" = [
|
|
519
|
+
"PLC1901",
|
|
520
|
+
"PLR2004",
|
|
521
|
+
"PLR6301",
|
|
522
|
+
"S",
|
|
523
|
+
"TID252",
|
|
524
|
+
]
|
|
525
|
+
|
|
526
|
+
[lint.flake8-tidy-imports]
|
|
527
|
+
ban-relative-imports = "all"
|
|
528
|
+
|
|
529
|
+
[lint.isort]
|
|
530
|
+
known-first-party = ["yut23_utils"]
|
|
531
|
+
|
|
532
|
+
[lint.flake8-pytest-style]
|
|
533
|
+
fixture-parentheses = false
|
|
534
|
+
mark-parentheses = false
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2023-present Eric T. Johnson
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
"""Tools for working with floating-point numbers."""
|
|
5
|
+
|
|
6
|
+
import math
|
|
7
|
+
import struct
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from functools import cached_property
|
|
10
|
+
from typing import cast
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def float_to_int(x: float, /) -> int:
|
|
14
|
+
return cast(int, struct.unpack("<Q", struct.pack("<d", x))[0])
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def int_to_float(q: int, /) -> float:
|
|
18
|
+
return cast(float, struct.unpack("<d", struct.pack("<Q", q))[0])
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def ulp_diff(a: float, b: float) -> int:
|
|
22
|
+
"""Return the (signed) number of representable FP64 values in the range [a, b)."""
|
|
23
|
+
if not math.isfinite(a) or not math.isfinite(b):
|
|
24
|
+
raise ValueError("only finite values can be compared")
|
|
25
|
+
if a == b:
|
|
26
|
+
return 0
|
|
27
|
+
if a > b:
|
|
28
|
+
# pylint: disable-next=arguments-out-of-order
|
|
29
|
+
return -ulp_diff(b, a)
|
|
30
|
+
if math.copysign(1.0, a) != math.copysign(1.0, b):
|
|
31
|
+
# different signs: split the interval at zero
|
|
32
|
+
return ulp_diff(a, -0.0) + ulp_diff(0.0, b)
|
|
33
|
+
return abs(float_to_int(a) - float_to_int(b))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def compare_ulp(a: float, b: float, /, ulps: int) -> bool:
|
|
37
|
+
"""Check if two numbers match to within a specified number of FP64 ULPs."""
|
|
38
|
+
if ulps < 0:
|
|
39
|
+
raise ValueError("ulps must be non-negative")
|
|
40
|
+
return abs(ulp_diff(a, b)) <= ulps
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True)
|
|
44
|
+
class FloatInspector:
|
|
45
|
+
float_val: float
|
|
46
|
+
int_val: int = field(init=False)
|
|
47
|
+
|
|
48
|
+
SIGN_MASK = 0x8000000000000000
|
|
49
|
+
EXP_MASK = 0x7FF0000000000000
|
|
50
|
+
MANT_MASK = 0x000FFFFFFFFFFFFF
|
|
51
|
+
|
|
52
|
+
def __post_init__(self):
|
|
53
|
+
object.__setattr__(self, "int_val", float_to_int(self.float_val))
|
|
54
|
+
|
|
55
|
+
@cached_property
|
|
56
|
+
def raw_sign(self) -> int:
|
|
57
|
+
return (self.int_val & self.SIGN_MASK) >> 63
|
|
58
|
+
|
|
59
|
+
@cached_property
|
|
60
|
+
def raw_exponent(self) -> int:
|
|
61
|
+
return (self.int_val & self.EXP_MASK) >> 52
|
|
62
|
+
|
|
63
|
+
@cached_property
|
|
64
|
+
def raw_mantissa(self) -> int:
|
|
65
|
+
return self.int_val & self.MANT_MASK
|
|
66
|
+
|
|
67
|
+
def __str__(self) -> str:
|
|
68
|
+
return (
|
|
69
|
+
f"FloatData({float(self)} = "
|
|
70
|
+
f"{self.raw_sign} * 2^{self.exponent} * {self.mantissa}; "
|
|
71
|
+
f"s={self.raw_sign}, e={self.raw_exponent:011b}, m={self.raw_mantissa:052b})"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
def __repr__(self) -> str:
|
|
75
|
+
f = float(self)
|
|
76
|
+
return f"FloatData({f}; {f.hex()})"
|
|
77
|
+
|
|
78
|
+
def __float__(self) -> float:
|
|
79
|
+
return self.float_val
|
|
80
|
+
|
|
81
|
+
@cached_property
|
|
82
|
+
def exponent(self) -> int:
|
|
83
|
+
return self.raw_exponent - 1023
|
|
84
|
+
|
|
85
|
+
@cached_property
|
|
86
|
+
def mantissa(self) -> float:
|
|
87
|
+
frac = self.raw_mantissa / (1 << 52)
|
|
88
|
+
if self.raw_exponent == 0:
|
|
89
|
+
# zero and subnormals
|
|
90
|
+
frac *= 2
|
|
91
|
+
else:
|
|
92
|
+
frac += 1
|
|
93
|
+
if self.raw_sign == 1:
|
|
94
|
+
frac *= -1
|
|
95
|
+
return frac
|
|
96
|
+
|
|
97
|
+
def is_negative(self) -> bool:
|
|
98
|
+
return bool(self.raw_sign)
|
|
99
|
+
|
|
100
|
+
def is_inf(self) -> bool:
|
|
101
|
+
return self.raw_exponent == 0x7FF and self.raw_mantissa == 0
|
|
102
|
+
|
|
103
|
+
def is_nan(self) -> bool:
|
|
104
|
+
return self.raw_exponent == 0x7FF and self.raw_mantissa != 0
|
|
105
|
+
|
|
106
|
+
def is_subnormal(self) -> bool:
|
|
107
|
+
return self.raw_exponent == 0 and self.raw_mantissa != 0
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2023-present Eric T. Johnson
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
from hypothesis import assume, example, given, note
|
|
10
|
+
from hypothesis import strategies as st
|
|
11
|
+
|
|
12
|
+
from yut23_utils.fp import (
|
|
13
|
+
FloatInspector,
|
|
14
|
+
compare_ulp,
|
|
15
|
+
float_to_int,
|
|
16
|
+
int_to_float,
|
|
17
|
+
ulp_diff,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@given(st.floats())
|
|
22
|
+
def test_float_to_int(x_flt: float) -> None:
|
|
23
|
+
x_int = float_to_int(x_flt)
|
|
24
|
+
assert isinstance(x_int, int)
|
|
25
|
+
assert 0 <= x_int < 1 << 64
|
|
26
|
+
x_flt_2 = int_to_float(x_int)
|
|
27
|
+
if math.isnan(x_flt):
|
|
28
|
+
assert math.isnan(x_flt_2)
|
|
29
|
+
else:
|
|
30
|
+
assert x_flt_2 == x_flt
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@given(st.integers(min_value=0, max_value=(1 << 64) - 1))
|
|
34
|
+
def test_int_to_float(x_int: int) -> None:
|
|
35
|
+
x_flt = int_to_float(x_int)
|
|
36
|
+
assert float_to_int(x_flt) == x_int
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@st.composite
|
|
40
|
+
def float_pairs(draw: st.DrawFn, max_ulps: int) -> tuple[float, float, int]:
|
|
41
|
+
a = draw(st.floats(allow_nan=False, allow_infinity=False))
|
|
42
|
+
ulps = draw(st.integers(min_value=-max_ulps, max_value=max_ulps))
|
|
43
|
+
|
|
44
|
+
b = a
|
|
45
|
+
direction = math.copysign(math.inf, ulps)
|
|
46
|
+
for _ in range(abs(ulps)):
|
|
47
|
+
b = math.nextafter(b, direction)
|
|
48
|
+
assume(not math.isinf(b))
|
|
49
|
+
|
|
50
|
+
note(f"a = {FloatInspector(a)}\nb = {FloatInspector(b)}")
|
|
51
|
+
return a, b, ulps
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@given(float_pairs(max_ulps=100))
|
|
55
|
+
@example((0.0, -5e-324, -1))
|
|
56
|
+
@example((5e-324, 0.0, -1))
|
|
57
|
+
@example((5e-324, -0.0, -1))
|
|
58
|
+
@example((-5e-324, 5e-324, 2))
|
|
59
|
+
def test_ulp_diff(args: tuple[float, float, int]) -> None:
|
|
60
|
+
a, b, ulps = args
|
|
61
|
+
assert ulp_diff(a, b) == ulps
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@pytest.mark.parametrize(
|
|
65
|
+
"a,b",
|
|
66
|
+
[
|
|
67
|
+
(math.inf, 3.14),
|
|
68
|
+
(math.inf, math.inf),
|
|
69
|
+
(-math.inf, math.inf),
|
|
70
|
+
(math.nan, -2.718),
|
|
71
|
+
(math.nan, math.nan),
|
|
72
|
+
],
|
|
73
|
+
)
|
|
74
|
+
def test_ulp_diff_errors(a: float, b: float) -> None:
|
|
75
|
+
with pytest.raises(ValueError):
|
|
76
|
+
ulp_diff(a, b)
|
|
77
|
+
with pytest.raises(ValueError):
|
|
78
|
+
ulp_diff(b, a)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@given(float_pairs(max_ulps=100))
|
|
82
|
+
@example((-5e-324, 5e-324, 2))
|
|
83
|
+
def test_compare_ulp(args: tuple[float, float, int]) -> None:
|
|
84
|
+
a, b, ulps = args
|
|
85
|
+
assert compare_ulp(a, b, abs(ulps))
|
|
86
|
+
if abs(ulps) > 0:
|
|
87
|
+
assert not compare_ulp(a, b, abs(ulps) - 1)
|
|
88
|
+
assert compare_ulp(a, b, abs(ulps) + 1)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_compare_ulp_errors() -> None:
|
|
92
|
+
with pytest.raises(ValueError):
|
|
93
|
+
compare_ulp(1.0, 1.0, -1)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@given(st.floats())
|
|
97
|
+
def test_FloatInspector(x: float) -> None:
|
|
98
|
+
fi = FloatInspector(x)
|
|
99
|
+
note(f"fi={fi} (subnormal: {fi.is_subnormal()})")
|
|
100
|
+
if not math.isnan(x):
|
|
101
|
+
assert float(fi) == x
|
|
102
|
+
# convert to an int to check raw NaN representation
|
|
103
|
+
assert float_to_int(float(fi)) == float_to_int(x)
|
|
104
|
+
|
|
105
|
+
# try manually reconstructing the value from the mantissa and exponent
|
|
106
|
+
if math.isfinite(x):
|
|
107
|
+
reconstructed = math.ldexp(fi.mantissa, fi.exponent)
|
|
108
|
+
assert reconstructed == x
|
|
109
|
+
|
|
110
|
+
# check interrogator methods
|
|
111
|
+
# need to use copysign rather than `x < 0.0` to handle -0.0 properly
|
|
112
|
+
is_negative = math.copysign(1.0, x) == -1.0
|
|
113
|
+
assert fi.is_negative() == is_negative
|
|
114
|
+
assert fi.is_inf() == math.isinf(x)
|
|
115
|
+
assert fi.is_nan() == math.isnan(x)
|
|
116
|
+
is_subnormal = 0 < abs(x) < sys.float_info.min
|
|
117
|
+
assert fi.is_subnormal() == is_subnormal
|