decimo 0.1.0.dev0__py3-none-any.whl
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.
- decimo/__init__.py +157 -0
- decimo/_decimo.pyi +18 -0
- decimo/py.typed +0 -0
- decimo-0.1.0.dev0.dist-info/METADATA +85 -0
- decimo-0.1.0.dev0.dist-info/RECORD +6 -0
- decimo-0.1.0.dev0.dist-info/WHEEL +4 -0
decimo/__init__.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""decimo: Arbitrary-precision decimal arithmetic for Python, powered by Mojo.
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
from decimo import Decimal
|
|
5
|
+
|
|
6
|
+
a = Decimal("1.5")
|
|
7
|
+
b = Decimal("2.3")
|
|
8
|
+
print(a + b) # 3.8
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
__version__ = "0.1.0.dev0"
|
|
12
|
+
__all__ = ["Decimal", "BigDecimal"]
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
from ._decimo import BigDecimal as _BigDecimal
|
|
16
|
+
except ImportError as _err:
|
|
17
|
+
raise ImportError(
|
|
18
|
+
"decimo requires a compiled Mojo extension (_decimo native module).\n"
|
|
19
|
+
"This package does not yet include pre-built wheels.\n"
|
|
20
|
+
"Build from source:\n"
|
|
21
|
+
" git clone https://github.com/forfudan/decimo && cd decimo\n"
|
|
22
|
+
" pixi run buildpy\n"
|
|
23
|
+
" pip install -e python/\n"
|
|
24
|
+
) from _err
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Decimal:
|
|
28
|
+
"""Arbitrary-precision decimal number.
|
|
29
|
+
|
|
30
|
+
This is a thin Python wrapper around decimo's Mojo-native BigDecimal type.
|
|
31
|
+
All heavy arithmetic is performed in Mojo at near-native speed.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
__slots__ = ("_inner",)
|
|
35
|
+
|
|
36
|
+
def __init__(self, value="0"):
|
|
37
|
+
if isinstance(value, Decimal):
|
|
38
|
+
self._inner = value._inner
|
|
39
|
+
elif isinstance(value, _BigDecimal):
|
|
40
|
+
self._inner = value
|
|
41
|
+
else:
|
|
42
|
+
self._inner = _BigDecimal(str(value))
|
|
43
|
+
|
|
44
|
+
# --- String ---
|
|
45
|
+
|
|
46
|
+
def __str__(self):
|
|
47
|
+
return self._inner.to_string()
|
|
48
|
+
|
|
49
|
+
def __repr__(self):
|
|
50
|
+
return self._inner.to_repr()
|
|
51
|
+
|
|
52
|
+
# --- Arithmetic ---
|
|
53
|
+
|
|
54
|
+
def __add__(self, other):
|
|
55
|
+
if not isinstance(other, Decimal):
|
|
56
|
+
other = Decimal(other)
|
|
57
|
+
result = Decimal.__new__(Decimal)
|
|
58
|
+
result._inner = self._inner.add(other._inner)
|
|
59
|
+
return result
|
|
60
|
+
|
|
61
|
+
def __radd__(self, other):
|
|
62
|
+
return Decimal(other).__add__(self)
|
|
63
|
+
|
|
64
|
+
def __sub__(self, other):
|
|
65
|
+
if not isinstance(other, Decimal):
|
|
66
|
+
other = Decimal(other)
|
|
67
|
+
result = Decimal.__new__(Decimal)
|
|
68
|
+
result._inner = self._inner.sub(other._inner)
|
|
69
|
+
return result
|
|
70
|
+
|
|
71
|
+
def __rsub__(self, other):
|
|
72
|
+
return Decimal(other).__sub__(self)
|
|
73
|
+
|
|
74
|
+
def __mul__(self, other):
|
|
75
|
+
if not isinstance(other, Decimal):
|
|
76
|
+
other = Decimal(other)
|
|
77
|
+
result = Decimal.__new__(Decimal)
|
|
78
|
+
result._inner = self._inner.mul(other._inner)
|
|
79
|
+
return result
|
|
80
|
+
|
|
81
|
+
def __rmul__(self, other):
|
|
82
|
+
return Decimal(other).__mul__(self)
|
|
83
|
+
|
|
84
|
+
def __truediv__(self, other):
|
|
85
|
+
if not isinstance(other, Decimal):
|
|
86
|
+
other = Decimal(other)
|
|
87
|
+
result = Decimal.__new__(Decimal)
|
|
88
|
+
result._inner = self._inner.div(other._inner)
|
|
89
|
+
return result
|
|
90
|
+
|
|
91
|
+
def __neg__(self):
|
|
92
|
+
result = Decimal.__new__(Decimal)
|
|
93
|
+
result._inner = self._inner.neg()
|
|
94
|
+
return result
|
|
95
|
+
|
|
96
|
+
def __abs__(self):
|
|
97
|
+
result = Decimal.__new__(Decimal)
|
|
98
|
+
result._inner = self._inner.abs_()
|
|
99
|
+
return result
|
|
100
|
+
|
|
101
|
+
def __pos__(self):
|
|
102
|
+
return self # no-op
|
|
103
|
+
|
|
104
|
+
# --- Comparison ---
|
|
105
|
+
|
|
106
|
+
def __eq__(self, other):
|
|
107
|
+
if not isinstance(other, Decimal):
|
|
108
|
+
try:
|
|
109
|
+
other = Decimal(other)
|
|
110
|
+
except Exception:
|
|
111
|
+
return NotImplemented
|
|
112
|
+
return self._inner.eq(other._inner)
|
|
113
|
+
|
|
114
|
+
def __lt__(self, other):
|
|
115
|
+
if not isinstance(other, Decimal):
|
|
116
|
+
try:
|
|
117
|
+
other = Decimal(other)
|
|
118
|
+
except Exception:
|
|
119
|
+
return NotImplemented
|
|
120
|
+
return self._inner.lt(other._inner)
|
|
121
|
+
|
|
122
|
+
def __le__(self, other):
|
|
123
|
+
if not isinstance(other, Decimal):
|
|
124
|
+
try:
|
|
125
|
+
other = Decimal(other)
|
|
126
|
+
except Exception:
|
|
127
|
+
return NotImplemented
|
|
128
|
+
return self._inner.le(other._inner)
|
|
129
|
+
|
|
130
|
+
def __gt__(self, other):
|
|
131
|
+
if not isinstance(other, Decimal):
|
|
132
|
+
try:
|
|
133
|
+
other = Decimal(other)
|
|
134
|
+
except Exception:
|
|
135
|
+
return NotImplemented
|
|
136
|
+
return not self._inner.le(other._inner)
|
|
137
|
+
|
|
138
|
+
def __ge__(self, other):
|
|
139
|
+
if not isinstance(other, Decimal):
|
|
140
|
+
try:
|
|
141
|
+
other = Decimal(other)
|
|
142
|
+
except Exception:
|
|
143
|
+
return NotImplemented
|
|
144
|
+
return not self._inner.lt(other._inner)
|
|
145
|
+
|
|
146
|
+
def __ne__(self, other):
|
|
147
|
+
eq_result = self.__eq__(other)
|
|
148
|
+
if eq_result is NotImplemented:
|
|
149
|
+
return NotImplemented
|
|
150
|
+
return not eq_result
|
|
151
|
+
|
|
152
|
+
def __bool__(self):
|
|
153
|
+
return str(self) not in ("0", "-0")
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
# Also expose as BigDecimal for users who prefer the full name
|
|
157
|
+
BigDecimal = Decimal
|
decimo/_decimo.pyi
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Type stubs for the _decimo Mojo extension module.
|
|
2
|
+
|
|
3
|
+
This file tells Pylance/mypy the interface of the compiled _decimo.so.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
class BigDecimal:
|
|
7
|
+
def __init__(self, value: str) -> None: ...
|
|
8
|
+
def to_string(self) -> str: ...
|
|
9
|
+
def to_repr(self) -> str: ...
|
|
10
|
+
def add(self, other: BigDecimal) -> BigDecimal: ...
|
|
11
|
+
def sub(self, other: BigDecimal) -> BigDecimal: ...
|
|
12
|
+
def mul(self, other: BigDecimal) -> BigDecimal: ...
|
|
13
|
+
def div(self, other: BigDecimal) -> BigDecimal: ...
|
|
14
|
+
def neg(self) -> BigDecimal: ...
|
|
15
|
+
def abs_(self) -> BigDecimal: ...
|
|
16
|
+
def eq(self, other: BigDecimal) -> bool: ...
|
|
17
|
+
def lt(self, other: BigDecimal) -> bool: ...
|
|
18
|
+
def le(self, other: BigDecimal) -> bool: ...
|
decimo/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: decimo
|
|
3
|
+
Version: 0.1.0.dev0
|
|
4
|
+
Summary: Arbitrary-precision decimal and integer arithmetic for Python, powered by Mojo
|
|
5
|
+
Project-URL: Homepage, https://github.com/forfudan/decimo
|
|
6
|
+
Project-URL: Repository, https://github.com/forfudan/decimo
|
|
7
|
+
Project-URL: Issues, https://github.com/forfudan/decimo/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/forfudan/decimo/blob/main/docs/changelog.md
|
|
9
|
+
Author-email: "ZHU Yuhao (朱宇浩)" <dr.yuhao.zhu@outlook.com>
|
|
10
|
+
License: Apache-2.0
|
|
11
|
+
Keywords: arbitrary-precision,arithmetic,bigdecimal,decimal,mojo,numeric
|
|
12
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# decimo
|
|
25
|
+
|
|
26
|
+
**Arbitrary-precision decimal and integer arithmetic for Python, powered by Mojo.**
|
|
27
|
+
|
|
28
|
+
[](https://pypi.org/project/decimo/)
|
|
29
|
+
[](https://github.com/forfudan/decimo/blob/main/LICENSE)
|
|
30
|
+
|
|
31
|
+
> ⚠️ **Pre-Alpha / Placeholder release.**
|
|
32
|
+
> The Python bindings are under active development. A proper installable wheel is coming soon.
|
|
33
|
+
> Full Mojo library is already available — see the [main repository](https://github.com/forfudan/decimo).
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## What is decimo?
|
|
38
|
+
|
|
39
|
+
`decimo` is an arbitrary-precision decimal and integer library, originally written in [Mojo](https://www.modular.com/mojo).
|
|
40
|
+
This package exposes `decimo`'s `BigDecimal` type to Python via a Mojo-built CPython extension module (`_decimo.so`),
|
|
41
|
+
with a thin Python wrapper providing full Pythonic operator support.
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from decimo import Decimal
|
|
45
|
+
|
|
46
|
+
a = Decimal("1.234567890123456789012345678901234567890")
|
|
47
|
+
b = Decimal("9.876543210987654321098765432109876543210")
|
|
48
|
+
|
|
49
|
+
print(a + b) # 11.111111101111111110111111111111111111100
|
|
50
|
+
print(a * b) # 12.193263111263526900...
|
|
51
|
+
print(a / b) # 0.12499999...
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Status
|
|
55
|
+
|
|
56
|
+
| Feature | Status |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `Decimal` (BigDecimal) arithmetic (`+`, `-`, `*`, `/`) | ✅ Working |
|
|
59
|
+
| Comparison operators | ✅ Working |
|
|
60
|
+
| Unary `-`, `abs()`, `bool()` | ✅ Working |
|
|
61
|
+
| Pre-built wheels on PyPI | 🚧 Coming soon |
|
|
62
|
+
| `BigInt` / `Decimal128` Python bindings | 🔜 Planned |
|
|
63
|
+
|
|
64
|
+
## Building from source
|
|
65
|
+
|
|
66
|
+
The extension requires [Mojo](https://docs.modular.com/mojo/manual/get-started/) and [pixi](https://pixi.sh):
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
git clone https://github.com/forfudan/decimo
|
|
70
|
+
cd decimo
|
|
71
|
+
pixi run buildpy
|
|
72
|
+
# .so is built at python/src/decimo/_decimo.so
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Then install in editable mode:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
pip install -e python/
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Links
|
|
82
|
+
|
|
83
|
+
- **GitHub**: <https://github.com/forfudan/decimo>
|
|
84
|
+
- **Changelog**: <https://github.com/forfudan/decimo/blob/main/docs/changelog.md>
|
|
85
|
+
- **Mojo library docs**: <https://github.com/forfudan/decimo/blob/main/docs/api.md>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
decimo/__init__.py,sha256=8-JwcMU7gW8OoV3qryEJWInQrTDFXQLeYtZxee0o10I,4388
|
|
2
|
+
decimo/_decimo.pyi,sha256=7uY7lm8gQIFDedjs6YJ9TLsM2a6EgwQ-ak0VeSM3yMQ,712
|
|
3
|
+
decimo/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
decimo-0.1.0.dev0.dist-info/METADATA,sha256=VaZRKfruWUb3PxecDms6qHnYtVPMoHkoqDaAd9eJnq4,3133
|
|
5
|
+
decimo-0.1.0.dev0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
6
|
+
decimo-0.1.0.dev0.dist-info/RECORD,,
|