pleccative 0.0.1__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.
- pleccative-0.0.1/PKG-INFO +80 -0
- pleccative-0.0.1/README.md +64 -0
- pleccative-0.0.1/pyproject.toml +76 -0
- pleccative-0.0.1/pyproject.toml.orig +85 -0
- pleccative-0.0.1/src/pleccative/__init__.py +25 -0
- pleccative-0.0.1/src/pleccative/pleccative.py +362 -0
- pleccative-0.0.1/src/pleccative/py.typed +0 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pleccative
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A Python implementation of pleccative numbers
|
|
5
|
+
Author: Dave Pearson
|
|
6
|
+
Author-email: Dave Pearson <davep@davep.org>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Project-URL: Homepage, https://pleccative.davep.dev/
|
|
10
|
+
Project-URL: Repository, https://github.com/davep/pleccative
|
|
11
|
+
Project-URL: Documentation, https://pleccative.davep.dev/
|
|
12
|
+
Project-URL: Source, https://github.com/davep/pleccative
|
|
13
|
+
Project-URL: Issues, https://github.com/davep/pleccative/issues
|
|
14
|
+
Project-URL: Discussions, https://github.com/davep/pleccative/discussions
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# pleccative
|
|
18
|
+
|
|
19
|
+
A zero-dependency Python implementation of **pleccative numbers**, modelled after Python's built-in `complex` type.
|
|
20
|
+
|
|
21
|
+
## What is this?
|
|
22
|
+
|
|
23
|
+
Pleccative numbers are an internet maths meme that asks what happens if you add a third sign—**pleccative** (`|`)—alongside positive (`+`) and negative (`-`), turning the usual two-way number line into a three-way fork radiating from zero at 120° intervals.
|
|
24
|
+
|
|
25
|
+
The meme originated on 2 December 2026 with a viral post on [X](https://x.com/kittenfacerb) by **Snowval (`@kittenfacerb`)** announcing *"i made a third type of number"*, before spreading across [r/mathmemes](https://www.reddit.com/r/mathmemes/), [r/mathematics](https://www.reddit.com/r/mathematics/), and [Lessons in Meme Culture](https://www.youtube.com/@LessonsInMemeCulture) (*"They Invented A Third Number"*).
|
|
26
|
+
|
|
27
|
+
Because it's a meme rather than real mathematics, there's no single official textbook definition. This library implements a clean, consistent three-way system with 120° sign rotations for multiplication and cancellation for addition.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
`pleccative` requires **Python 3.11+** and has no runtime dependencies.
|
|
32
|
+
|
|
33
|
+
With `uv`:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
uv add pleccative
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Or with `pip`:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install pleccative
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Quick start
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from pleccative import Pleccative
|
|
49
|
+
|
|
50
|
+
a = Pleccative("|3") # From a string (just like complex)
|
|
51
|
+
b = Pleccative(-2) # From a real number
|
|
52
|
+
c = Pleccative(5, "|") # From a non-negative magnitude and sign
|
|
53
|
+
|
|
54
|
+
print(b * b) # |4 (negative * negative = pleccative)
|
|
55
|
+
print(a * b) # +6 (pleccative * negative = positive)
|
|
56
|
+
print(a * a) # -9 (pleccative * pleccative = negative)
|
|
57
|
+
print(a + 5) # +2 (different signs cancel)
|
|
58
|
+
print(-b) # |2 (120-degree rotation: + -> - -> | -> +)
|
|
59
|
+
print(~a) # -3 (240-degree rotation: + -> | -> - -> +)
|
|
60
|
+
print(c / b) # -2.5 (exact inverse of multiplication)
|
|
61
|
+
print(b ** 3) # +8 (signs cycle every 3 powers)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Multiplication sign table
|
|
65
|
+
|
|
66
|
+
| × | `+` | `-` | `\|` |
|
|
67
|
+
| :---: | :---: | :---: | :---: |
|
|
68
|
+
| **`+`** | `+` | `-` | `\|` |
|
|
69
|
+
| **`-`** | `-` | `\|` | `+` |
|
|
70
|
+
| **`\|`** | `\|` | `+` | `-` |
|
|
71
|
+
|
|
72
|
+
## Documentation
|
|
73
|
+
|
|
74
|
+
Full usage guides and examples are available at [pleccative.davep.dev](https://pleccative.davep.dev/), including the [Getting Started](https://pleccative.davep.dev/getting_started/) guide.
|
|
75
|
+
|
|
76
|
+
## Licence
|
|
77
|
+
|
|
78
|
+
Licensed under the [MIT License](LICENSE).
|
|
79
|
+
|
|
80
|
+
[//]: # (README.md ends here)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# pleccative
|
|
2
|
+
|
|
3
|
+
A zero-dependency Python implementation of **pleccative numbers**, modelled after Python's built-in `complex` type.
|
|
4
|
+
|
|
5
|
+
## What is this?
|
|
6
|
+
|
|
7
|
+
Pleccative numbers are an internet maths meme that asks what happens if you add a third sign—**pleccative** (`|`)—alongside positive (`+`) and negative (`-`), turning the usual two-way number line into a three-way fork radiating from zero at 120° intervals.
|
|
8
|
+
|
|
9
|
+
The meme originated on 2 December 2026 with a viral post on [X](https://x.com/kittenfacerb) by **Snowval (`@kittenfacerb`)** announcing *"i made a third type of number"*, before spreading across [r/mathmemes](https://www.reddit.com/r/mathmemes/), [r/mathematics](https://www.reddit.com/r/mathematics/), and [Lessons in Meme Culture](https://www.youtube.com/@LessonsInMemeCulture) (*"They Invented A Third Number"*).
|
|
10
|
+
|
|
11
|
+
Because it's a meme rather than real mathematics, there's no single official textbook definition. This library implements a clean, consistent three-way system with 120° sign rotations for multiplication and cancellation for addition.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
`pleccative` requires **Python 3.11+** and has no runtime dependencies.
|
|
16
|
+
|
|
17
|
+
With `uv`:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
uv add pleccative
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Or with `pip`:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install pleccative
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick start
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from pleccative import Pleccative
|
|
33
|
+
|
|
34
|
+
a = Pleccative("|3") # From a string (just like complex)
|
|
35
|
+
b = Pleccative(-2) # From a real number
|
|
36
|
+
c = Pleccative(5, "|") # From a non-negative magnitude and sign
|
|
37
|
+
|
|
38
|
+
print(b * b) # |4 (negative * negative = pleccative)
|
|
39
|
+
print(a * b) # +6 (pleccative * negative = positive)
|
|
40
|
+
print(a * a) # -9 (pleccative * pleccative = negative)
|
|
41
|
+
print(a + 5) # +2 (different signs cancel)
|
|
42
|
+
print(-b) # |2 (120-degree rotation: + -> - -> | -> +)
|
|
43
|
+
print(~a) # -3 (240-degree rotation: + -> | -> - -> +)
|
|
44
|
+
print(c / b) # -2.5 (exact inverse of multiplication)
|
|
45
|
+
print(b ** 3) # +8 (signs cycle every 3 powers)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Multiplication sign table
|
|
49
|
+
|
|
50
|
+
| × | `+` | `-` | `\|` |
|
|
51
|
+
| :---: | :---: | :---: | :---: |
|
|
52
|
+
| **`+`** | `+` | `-` | `\|` |
|
|
53
|
+
| **`-`** | `-` | `\|` | `+` |
|
|
54
|
+
| **`\|`** | `\|` | `+` | `-` |
|
|
55
|
+
|
|
56
|
+
## Documentation
|
|
57
|
+
|
|
58
|
+
Full usage guides and examples are available at [pleccative.davep.dev](https://pleccative.davep.dev/), including the [Getting Started](https://pleccative.davep.dev/getting_started/) guide.
|
|
59
|
+
|
|
60
|
+
## Licence
|
|
61
|
+
|
|
62
|
+
Licensed under the [MIT License](LICENSE).
|
|
63
|
+
|
|
64
|
+
[//]: # (README.md ends here)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pleccative"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "A Python implementation of pleccative numbers"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
dependencies = []
|
|
8
|
+
license = "MIT"
|
|
9
|
+
|
|
10
|
+
[[project.authors]]
|
|
11
|
+
name = "Dave Pearson"
|
|
12
|
+
email = "davep@davep.org"
|
|
13
|
+
|
|
14
|
+
[project.urls]
|
|
15
|
+
Homepage = "https://pleccative.davep.dev/"
|
|
16
|
+
Repository = "https://github.com/davep/pleccative"
|
|
17
|
+
Documentation = "https://pleccative.davep.dev/"
|
|
18
|
+
Source = "https://github.com/davep/pleccative"
|
|
19
|
+
Issues = "https://github.com/davep/pleccative/issues"
|
|
20
|
+
Discussions = "https://github.com/davep/pleccative/discussions"
|
|
21
|
+
|
|
22
|
+
[build-system]
|
|
23
|
+
requires = ["uv_build>=0.13.13,<0.14.0"]
|
|
24
|
+
build-backend = "uv_build"
|
|
25
|
+
|
|
26
|
+
[[tool.uv.index]]
|
|
27
|
+
name = "testpypi"
|
|
28
|
+
url = "https://test.pypi.org/simple/"
|
|
29
|
+
publish-url = "https://test.pypi.org/legacy/"
|
|
30
|
+
explicit = true
|
|
31
|
+
|
|
32
|
+
[tool.pyright]
|
|
33
|
+
venvPath = "."
|
|
34
|
+
venv = ".venv"
|
|
35
|
+
exclude = [".venv"]
|
|
36
|
+
|
|
37
|
+
[tool.ruff.lint]
|
|
38
|
+
select = [
|
|
39
|
+
"E",
|
|
40
|
+
"F",
|
|
41
|
+
"UP",
|
|
42
|
+
"B",
|
|
43
|
+
"SIM",
|
|
44
|
+
"I",
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
[tool.ruff.lint.pycodestyle]
|
|
48
|
+
max-line-length = 120
|
|
49
|
+
|
|
50
|
+
[tool.coverage.run]
|
|
51
|
+
omit = ["tests/*"]
|
|
52
|
+
|
|
53
|
+
[tool.coverage.report]
|
|
54
|
+
exclude_lines = [
|
|
55
|
+
"pragma: no cover",
|
|
56
|
+
"def __repr__",
|
|
57
|
+
"raise AssertionError",
|
|
58
|
+
"raise NotImplementedError",
|
|
59
|
+
"if __name__ == .__main__.:",
|
|
60
|
+
"if TYPE_CHECKING:",
|
|
61
|
+
'class .*\bProtocol\):',
|
|
62
|
+
'@(abc\.)?abstractmethod',
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
[dependency-groups]
|
|
66
|
+
dev = [
|
|
67
|
+
"codespell>=2.4.2",
|
|
68
|
+
"mypy>=2.1.0",
|
|
69
|
+
"pre-commit>=4.6.0",
|
|
70
|
+
"ruff>=0.15.17",
|
|
71
|
+
]
|
|
72
|
+
docs = ["zensical>=0.1.0"]
|
|
73
|
+
test = [
|
|
74
|
+
"pytest>=9.1.0",
|
|
75
|
+
"pytest-cov>=7.1.0",
|
|
76
|
+
]
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pleccative"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "A Python implementation of pleccative numbers"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Dave Pearson", email = "davep@davep.org" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = []
|
|
11
|
+
license = "MIT"
|
|
12
|
+
|
|
13
|
+
[project.urls]
|
|
14
|
+
Homepage = "https://pleccative.davep.dev/"
|
|
15
|
+
Repository = "https://github.com/davep/pleccative"
|
|
16
|
+
Documentation = "https://pleccative.davep.dev/"
|
|
17
|
+
Source = "https://github.com/davep/pleccative"
|
|
18
|
+
Issues = "https://github.com/davep/pleccative/issues"
|
|
19
|
+
Discussions = "https://github.com/davep/pleccative/discussions"
|
|
20
|
+
|
|
21
|
+
[build-system]
|
|
22
|
+
requires = ["uv_build>=0.13.13,<0.14.0"]
|
|
23
|
+
build-backend = "uv_build"
|
|
24
|
+
|
|
25
|
+
[[tool.uv.index]]
|
|
26
|
+
name = "testpypi"
|
|
27
|
+
url = "https://test.pypi.org/simple/"
|
|
28
|
+
publish-url = "https://test.pypi.org/legacy/"
|
|
29
|
+
explicit = true
|
|
30
|
+
|
|
31
|
+
[dependency-groups]
|
|
32
|
+
dev = [
|
|
33
|
+
"codespell>=2.4.2",
|
|
34
|
+
"mypy>=2.1.0",
|
|
35
|
+
"pre-commit>=4.6.0",
|
|
36
|
+
"ruff>=0.15.17",
|
|
37
|
+
]
|
|
38
|
+
docs = [
|
|
39
|
+
"zensical>=0.1.0",
|
|
40
|
+
]
|
|
41
|
+
test = [
|
|
42
|
+
"pytest>=9.1.0",
|
|
43
|
+
"pytest-cov>=7.1.0",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
[tool.pyright]
|
|
47
|
+
venvPath="."
|
|
48
|
+
venv=".venv"
|
|
49
|
+
exclude=[".venv"]
|
|
50
|
+
|
|
51
|
+
[tool.ruff.lint]
|
|
52
|
+
select = [
|
|
53
|
+
# pycodestyle
|
|
54
|
+
"E",
|
|
55
|
+
# Pyflakes
|
|
56
|
+
"F",
|
|
57
|
+
# pyupgrade
|
|
58
|
+
"UP",
|
|
59
|
+
# flake8-bugbear
|
|
60
|
+
"B",
|
|
61
|
+
# flake8-simplify
|
|
62
|
+
"SIM",
|
|
63
|
+
# isort
|
|
64
|
+
"I",
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
[tool.ruff.lint.pycodestyle]
|
|
68
|
+
max-line-length = 120
|
|
69
|
+
|
|
70
|
+
[tool.coverage.run]
|
|
71
|
+
omit = [
|
|
72
|
+
"tests/*"
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
[tool.coverage.report]
|
|
76
|
+
exclude_lines = [
|
|
77
|
+
"pragma: no cover",
|
|
78
|
+
"def __repr__",
|
|
79
|
+
"raise AssertionError",
|
|
80
|
+
"raise NotImplementedError",
|
|
81
|
+
"if __name__ == .__main__.:",
|
|
82
|
+
"if TYPE_CHECKING:",
|
|
83
|
+
"class .*\\bProtocol\\):",
|
|
84
|
+
"@(abc\\.)?abstractmethod",
|
|
85
|
+
]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""A Python implementation of pleccative numbers."""
|
|
2
|
+
|
|
3
|
+
##############################################################################
|
|
4
|
+
# Python imports.
|
|
5
|
+
from importlib.metadata import version
|
|
6
|
+
|
|
7
|
+
######################################################################
|
|
8
|
+
# Main library information.
|
|
9
|
+
__author__ = "Dave Pearson"
|
|
10
|
+
__copyright__ = "Copyright 2026, Dave Pearson"
|
|
11
|
+
__credits__ = ["Dave Pearson"]
|
|
12
|
+
__maintainer__ = "Dave Pearson"
|
|
13
|
+
__email__ = "davep@davep.org"
|
|
14
|
+
__version__: str = version("pleccative")
|
|
15
|
+
__licence__ = "MIT"
|
|
16
|
+
|
|
17
|
+
##############################################################################
|
|
18
|
+
# Local imports.
|
|
19
|
+
from .pleccative import Pleccative
|
|
20
|
+
|
|
21
|
+
##############################################################################
|
|
22
|
+
# Module exports.
|
|
23
|
+
__all__ = ["Pleccative"]
|
|
24
|
+
|
|
25
|
+
### __init__.py ends here
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
"""Idiomatic numeric implementation of pleccative numbers modelled after complex."""
|
|
2
|
+
|
|
3
|
+
##############################################################################
|
|
4
|
+
# Future imports.
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
##############################################################################
|
|
8
|
+
# Python imports.
|
|
9
|
+
from enum import Enum, IntEnum
|
|
10
|
+
from typing import Final, Self, overload
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
##############################################################################
|
|
14
|
+
class _Unset(Enum):
|
|
15
|
+
"""Sentinel type for omitted optional arguments."""
|
|
16
|
+
|
|
17
|
+
TOKEN = 0
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
_UNSET: Final = _Unset.TOKEN
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
##############################################################################
|
|
24
|
+
class Sign(IntEnum):
|
|
25
|
+
"""Represents the three signs in a pleccative number system."""
|
|
26
|
+
|
|
27
|
+
POSITIVE = 0
|
|
28
|
+
"""The positive sign."""
|
|
29
|
+
NEGATIVE = 1
|
|
30
|
+
"""The negative sign."""
|
|
31
|
+
PLECCATIVE = 2
|
|
32
|
+
"""The pleccative sign."""
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def symbol(self) -> str:
|
|
36
|
+
"""The display symbol for the sign."""
|
|
37
|
+
return ("+", "-", "|")[self]
|
|
38
|
+
|
|
39
|
+
def rotate(self, steps: int) -> Sign:
|
|
40
|
+
"""Rotate this sign by a given number of 120-degree steps modulo 3.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
steps: The number of 120-degree steps to rotate the sign.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
The new `Sign` after rotation.
|
|
47
|
+
"""
|
|
48
|
+
return Sign((int(self) + steps) % 3)
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
def from_value(cls, value: Sign | str) -> Sign:
|
|
52
|
+
"""Resolve a `Sign` enum member or symbol string.
|
|
53
|
+
|
|
54
|
+
Args:
|
|
55
|
+
value: A `Sign` enum member or a string representing the sign.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
The corresponding `Sign` enum member.
|
|
59
|
+
"""
|
|
60
|
+
if isinstance(value, Sign):
|
|
61
|
+
return value
|
|
62
|
+
mapping: dict[str, Sign] = {
|
|
63
|
+
"+": cls.POSITIVE,
|
|
64
|
+
"-": cls.NEGATIVE,
|
|
65
|
+
"|": cls.PLECCATIVE,
|
|
66
|
+
}
|
|
67
|
+
try:
|
|
68
|
+
return mapping[value]
|
|
69
|
+
except KeyError as error:
|
|
70
|
+
raise ValueError(f"Unknown pleccative sign: {value!r}") from error
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
##############################################################################
|
|
74
|
+
class Pleccative:
|
|
75
|
+
"""Represents a pleccative number with an interface modelled on Python's complex."""
|
|
76
|
+
|
|
77
|
+
__slots__ = ("_magnitude", "_sign")
|
|
78
|
+
|
|
79
|
+
@overload
|
|
80
|
+
def __init__(self, value: str, /) -> None: ...
|
|
81
|
+
|
|
82
|
+
@overload
|
|
83
|
+
def __init__(
|
|
84
|
+
self, magnitude: float = 0.0, sign: Sign | str = Sign.POSITIVE
|
|
85
|
+
) -> None: ...
|
|
86
|
+
|
|
87
|
+
def __init__(
|
|
88
|
+
self,
|
|
89
|
+
magnitude: float | str = 0.0,
|
|
90
|
+
sign: Sign | str | _Unset = _UNSET,
|
|
91
|
+
) -> None:
|
|
92
|
+
"""Initialise a pleccative number from a value, or a magnitude and sign.
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
magnitude: Either a string representation (such as `"+3"`, `"-2.5"`,
|
|
96
|
+
`"|4"`, `"5"`, or `"0"`), a real number (negative values become
|
|
97
|
+
`Sign.NEGATIVE` when `sign` is omitted), or a non-negative
|
|
98
|
+
magnitude paired with an explicit `sign`.
|
|
99
|
+
sign: The rotational sign, either a `Sign` enum member or a string.
|
|
100
|
+
Must not be supplied when the first argument is a string, and
|
|
101
|
+
requires a non-negative `magnitude`.
|
|
102
|
+
|
|
103
|
+
Raises:
|
|
104
|
+
TypeError: If a second argument is supplied when the first argument
|
|
105
|
+
is a string.
|
|
106
|
+
ValueError: If a negative magnitude is paired with an explicit sign,
|
|
107
|
+
the sign is invalid, or the string cannot be parsed as a
|
|
108
|
+
pleccative number.
|
|
109
|
+
"""
|
|
110
|
+
if isinstance(magnitude, str):
|
|
111
|
+
if sign is not _UNSET:
|
|
112
|
+
raise TypeError(
|
|
113
|
+
"Pleccative() can't take second arg if first is a string"
|
|
114
|
+
)
|
|
115
|
+
parsed_magnitude, resolved_sign = self._parse(magnitude)
|
|
116
|
+
self._magnitude: float = parsed_magnitude
|
|
117
|
+
"""The non-negative distance from zero."""
|
|
118
|
+
self._sign: Sign = Sign.POSITIVE if self._magnitude == 0 else resolved_sign
|
|
119
|
+
"""The rotational sign of the number."""
|
|
120
|
+
return
|
|
121
|
+
|
|
122
|
+
if sign is _UNSET:
|
|
123
|
+
self._magnitude = abs(float(magnitude))
|
|
124
|
+
self._sign = Sign.NEGATIVE if magnitude < 0 else Sign.POSITIVE
|
|
125
|
+
return
|
|
126
|
+
|
|
127
|
+
if magnitude < 0:
|
|
128
|
+
raise ValueError(
|
|
129
|
+
"Magnitude must be non-negative when a sign is explicitly provided."
|
|
130
|
+
)
|
|
131
|
+
resolved_sign = Sign.from_value(sign)
|
|
132
|
+
self._magnitude = abs(float(magnitude))
|
|
133
|
+
self._sign = Sign.POSITIVE if self._magnitude == 0 else resolved_sign
|
|
134
|
+
|
|
135
|
+
@staticmethod
|
|
136
|
+
def _parse(value: str) -> tuple[float, Sign]:
|
|
137
|
+
"""Parse a string literal into a non-negative magnitude and sign.
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
value: The string to parse.
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
A `(magnitude, sign)` tuple.
|
|
144
|
+
|
|
145
|
+
Raises:
|
|
146
|
+
ValueError: If the string is not a valid pleccative literal.
|
|
147
|
+
"""
|
|
148
|
+
text = value.strip()
|
|
149
|
+
if not text:
|
|
150
|
+
raise ValueError(f"Invalid pleccative literal: {value!r}")
|
|
151
|
+
|
|
152
|
+
if text[0] in ("+", "-", "|"):
|
|
153
|
+
sign = Sign.from_value(text[0])
|
|
154
|
+
number_text = text[1:]
|
|
155
|
+
else:
|
|
156
|
+
sign = Sign.POSITIVE
|
|
157
|
+
number_text = text
|
|
158
|
+
|
|
159
|
+
if (
|
|
160
|
+
not number_text
|
|
161
|
+
or number_text[0].isspace()
|
|
162
|
+
or number_text[0] in ("+", "-", "|")
|
|
163
|
+
):
|
|
164
|
+
raise ValueError(f"Invalid pleccative literal: {value!r}")
|
|
165
|
+
|
|
166
|
+
try:
|
|
167
|
+
magnitude = float(number_text)
|
|
168
|
+
except ValueError as error:
|
|
169
|
+
raise ValueError(f"Invalid pleccative literal: {value!r}") from error
|
|
170
|
+
|
|
171
|
+
return magnitude, sign
|
|
172
|
+
|
|
173
|
+
@property
|
|
174
|
+
def magnitude(self) -> float:
|
|
175
|
+
"""The non-negative distance from zero."""
|
|
176
|
+
return self._magnitude
|
|
177
|
+
|
|
178
|
+
@property
|
|
179
|
+
def sign(self) -> Sign:
|
|
180
|
+
"""The rotational sign of the number."""
|
|
181
|
+
return self._sign
|
|
182
|
+
|
|
183
|
+
@classmethod
|
|
184
|
+
def _coerce(cls, value: Pleccative | int | float) -> Self | None:
|
|
185
|
+
"""Convert real numbers into Pleccative instances.
|
|
186
|
+
|
|
187
|
+
Args:
|
|
188
|
+
value: A `Pleccative` instance or a real number.
|
|
189
|
+
|
|
190
|
+
Returns:
|
|
191
|
+
A `Pleccative` instance corresponding to the input value or
|
|
192
|
+
`None` if the input type is unsupported.
|
|
193
|
+
"""
|
|
194
|
+
if isinstance(value, cls):
|
|
195
|
+
return value
|
|
196
|
+
if isinstance(value, (int, float)) and not isinstance(value, bool):
|
|
197
|
+
return cls(value)
|
|
198
|
+
return None
|
|
199
|
+
|
|
200
|
+
def __abs__(self) -> float:
|
|
201
|
+
"""Return the magnitude of the number."""
|
|
202
|
+
return self._magnitude
|
|
203
|
+
|
|
204
|
+
def __bool__(self) -> bool:
|
|
205
|
+
"""Return True when the magnitude is non-zero."""
|
|
206
|
+
return self._magnitude != 0.0
|
|
207
|
+
|
|
208
|
+
def __eq__(self, other: object) -> bool:
|
|
209
|
+
"""Compare equality with another Pleccative or real number."""
|
|
210
|
+
if isinstance(other, Pleccative):
|
|
211
|
+
return self._magnitude == other._magnitude and self._sign == other._sign
|
|
212
|
+
if (
|
|
213
|
+
isinstance(other, (int, float))
|
|
214
|
+
and not isinstance(other, bool)
|
|
215
|
+
and ((coerced := self._coerce(other)) is not None)
|
|
216
|
+
):
|
|
217
|
+
return self._magnitude == coerced._magnitude and self._sign == coerced._sign
|
|
218
|
+
return NotImplemented
|
|
219
|
+
|
|
220
|
+
def __hash__(self) -> int:
|
|
221
|
+
"""Hash consistently with real numbers when sign is POSITIVE, NEGATIVE, or zero."""
|
|
222
|
+
if self._magnitude == 0.0:
|
|
223
|
+
return hash(0.0)
|
|
224
|
+
if self._sign == Sign.POSITIVE:
|
|
225
|
+
return hash(self._magnitude)
|
|
226
|
+
if self._sign == Sign.NEGATIVE:
|
|
227
|
+
return hash(-self._magnitude)
|
|
228
|
+
return hash((self._magnitude, self._sign))
|
|
229
|
+
|
|
230
|
+
def __pos__(self) -> Self:
|
|
231
|
+
"""Return a copy of this pleccative number."""
|
|
232
|
+
return type(self)(self._magnitude, self._sign)
|
|
233
|
+
|
|
234
|
+
def __neg__(self) -> Self:
|
|
235
|
+
"""Multiply by -1 (rotating sign by 120 degrees)."""
|
|
236
|
+
if self._magnitude == 0.0:
|
|
237
|
+
return type(self)(0.0, Sign.POSITIVE)
|
|
238
|
+
return type(self)(self._magnitude, self._sign.rotate(Sign.NEGATIVE))
|
|
239
|
+
|
|
240
|
+
def __invert__(self) -> Self:
|
|
241
|
+
"""Multiply by |1 (rotating sign by 240 degrees)."""
|
|
242
|
+
if self._magnitude == 0.0:
|
|
243
|
+
return type(self)(0.0, Sign.POSITIVE)
|
|
244
|
+
return type(self)(self._magnitude, self._sign.rotate(Sign.PLECCATIVE))
|
|
245
|
+
|
|
246
|
+
def __add__(self, other: Pleccative | int | float) -> Pleccative:
|
|
247
|
+
"""Combine two pleccative numbers via pairwise cancellation."""
|
|
248
|
+
if (right := self._coerce(other)) is None:
|
|
249
|
+
return NotImplemented
|
|
250
|
+
if self._magnitude == 0.0:
|
|
251
|
+
return +right
|
|
252
|
+
if right._magnitude == 0.0:
|
|
253
|
+
return +self
|
|
254
|
+
if self._sign == right._sign:
|
|
255
|
+
return Pleccative(self._magnitude + right._magnitude, self._sign)
|
|
256
|
+
if self._magnitude == right._magnitude:
|
|
257
|
+
return Pleccative(0.0, Sign.POSITIVE)
|
|
258
|
+
if self._magnitude > right._magnitude:
|
|
259
|
+
return Pleccative(self._magnitude - right._magnitude, self._sign)
|
|
260
|
+
return Pleccative(right._magnitude - self._magnitude, right._sign)
|
|
261
|
+
|
|
262
|
+
def __radd__(self, other: int | float) -> Pleccative:
|
|
263
|
+
"""Support left-hand addition with real numbers."""
|
|
264
|
+
if (right := self._coerce(other)) is None:
|
|
265
|
+
return NotImplemented
|
|
266
|
+
return right + self
|
|
267
|
+
|
|
268
|
+
def __sub__(self, other: Pleccative | int | float) -> Pleccative:
|
|
269
|
+
"""Subtract by adding the rotated right operand (a - b == a + (-b))."""
|
|
270
|
+
if (right := self._coerce(other)) is None:
|
|
271
|
+
return NotImplemented
|
|
272
|
+
return self + (-right)
|
|
273
|
+
|
|
274
|
+
def __rsub__(self, other: int | float) -> Pleccative:
|
|
275
|
+
"""Support left-hand subtraction with real numbers."""
|
|
276
|
+
if (left := self._coerce(other)) is None:
|
|
277
|
+
return NotImplemented
|
|
278
|
+
return left - self
|
|
279
|
+
|
|
280
|
+
def __or__(self, other: Pleccative | int | float) -> Pleccative:
|
|
281
|
+
"""Combine using binary | (a | b == a + (~b))."""
|
|
282
|
+
if (right := self._coerce(other)) is None:
|
|
283
|
+
return NotImplemented
|
|
284
|
+
return self + (~right)
|
|
285
|
+
|
|
286
|
+
def __ror__(self, other: int | float) -> Pleccative:
|
|
287
|
+
"""Support left-hand | with real numbers."""
|
|
288
|
+
if (left := self._coerce(other)) is None:
|
|
289
|
+
return NotImplemented
|
|
290
|
+
return left | self
|
|
291
|
+
|
|
292
|
+
def __mul__(self, other: Pleccative | int | float) -> Pleccative:
|
|
293
|
+
"""Multiply magnitudes and add rotational signs modulo 3."""
|
|
294
|
+
if (right := self._coerce(other)) is None:
|
|
295
|
+
return NotImplemented
|
|
296
|
+
if self._magnitude == 0.0 or right._magnitude == 0.0:
|
|
297
|
+
return Pleccative(0.0, Sign.POSITIVE)
|
|
298
|
+
return Pleccative(
|
|
299
|
+
self._magnitude * right._magnitude,
|
|
300
|
+
self._sign.rotate(right._sign),
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
def __rmul__(self, other: int | float) -> Pleccative:
|
|
304
|
+
"""Support left-hand multiplication with real numbers."""
|
|
305
|
+
if (right := self._coerce(other)) is None:
|
|
306
|
+
return NotImplemented
|
|
307
|
+
return right * self
|
|
308
|
+
|
|
309
|
+
def __truediv__(self, other: Pleccative | int | float) -> Pleccative:
|
|
310
|
+
"""Divide magnitudes and subtract rotational signs modulo 3."""
|
|
311
|
+
if (right := self._coerce(other)) is None:
|
|
312
|
+
return NotImplemented
|
|
313
|
+
if right._magnitude == 0.0:
|
|
314
|
+
raise ZeroDivisionError("pleccative division by zero")
|
|
315
|
+
if self._magnitude == 0.0:
|
|
316
|
+
return Pleccative(0.0, Sign.POSITIVE)
|
|
317
|
+
return Pleccative(
|
|
318
|
+
self._magnitude / right._magnitude,
|
|
319
|
+
self._sign.rotate(-int(right._sign)),
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
def __rtruediv__(self, other: int | float) -> Pleccative:
|
|
323
|
+
"""Support left-hand division with real numbers."""
|
|
324
|
+
if (left := self._coerce(other)) is None:
|
|
325
|
+
return NotImplemented
|
|
326
|
+
return left / self
|
|
327
|
+
|
|
328
|
+
def __pow__(self, exponent: int) -> Pleccative:
|
|
329
|
+
"""Raise a pleccative number to an integer power."""
|
|
330
|
+
if not isinstance(exponent, int) or isinstance(exponent, bool):
|
|
331
|
+
return NotImplemented
|
|
332
|
+
if exponent == 0:
|
|
333
|
+
return Pleccative(1.0, Sign.POSITIVE)
|
|
334
|
+
if self._magnitude == 0.0:
|
|
335
|
+
if exponent < 0:
|
|
336
|
+
raise ZeroDivisionError("0.0 cannot be raised to a negative power")
|
|
337
|
+
return Pleccative(0.0, Sign.POSITIVE)
|
|
338
|
+
return Pleccative(
|
|
339
|
+
self._magnitude**exponent,
|
|
340
|
+
Sign((int(self._sign) * exponent) % 3),
|
|
341
|
+
)
|
|
342
|
+
|
|
343
|
+
def __repr__(self) -> str:
|
|
344
|
+
"""Return a representation matching the meme's explicit three-sign notation."""
|
|
345
|
+
if self._magnitude == 0.0:
|
|
346
|
+
return "0"
|
|
347
|
+
formatted = (
|
|
348
|
+
int(self._magnitude) if self._magnitude.is_integer() else self._magnitude
|
|
349
|
+
)
|
|
350
|
+
return f"{self._sign.symbol}{formatted}"
|
|
351
|
+
|
|
352
|
+
def __str__(self) -> str:
|
|
353
|
+
"""Format the number using +, -, or | sign prefixes."""
|
|
354
|
+
if self._magnitude == 0.0:
|
|
355
|
+
return "0"
|
|
356
|
+
formatted = (
|
|
357
|
+
int(self._magnitude) if self._magnitude.is_integer() else self._magnitude
|
|
358
|
+
)
|
|
359
|
+
return f"{self._sign.symbol}{formatted}"
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
### pleccative.py ends here
|
|
File without changes
|