Skip to content

Getting Started

Installation

pleccative requires Python 3.11 or later and has no runtime dependencies.

Install with uv:

uv add pleccative

Or with pip:

pip install pleccative

Creating pleccative numbers

Import Pleccative from the top-level package:

from pleccative import Pleccative

Pleccative can be constructed in several ways, mirroring Python's built-in complex type.

Default zero

Calling Pleccative() with no arguments returns 0:

Pleccative()  # 0

From a real number

Passing a single int or float lifts it into a Pleccative number—positive values get +, negative values get -, and zero becomes canonical 0:

Pleccative(5)      # +5
Pleccative(-5)     # -5
Pleccative(2.5)    # +2.5
Pleccative(-2.5)   # -2.5
Pleccative(0)      # 0

From a magnitude and explicit sign

Pass a non-negative magnitude together with a sign symbol ("+", "-", or "|") or a Sign enum member from pleccative.pleccative:

from pleccative import Pleccative
from pleccative.pleccative import Sign

Pleccative(3, "+")              # +3
Pleccative(3, "-")              # -3
Pleccative(3, "|")              # |3
Pleccative(3, Sign.PLECCATIVE)  # |3

Explicit signs require a non-negative magnitude

When sign is passed explicitly, magnitude must be non-negative—calls such as Pleccative(-3, "-") or Pleccative(-3, "|") raise ValueError to avoid ambiguity.

From a string

Just like complex("1+2j"), you can pass a single string literal with an optional +, -, or | prefix. Leading and trailing whitespace, floating-point notation, scientific notation, and numeric underscores are all supported:

Pleccative("|5")          # |5
Pleccative("-2.5")        # -2.5
Pleccative("+3")          # +3
Pleccative("4")           # +4
Pleccative("  |1_000.5 ") # |1000.5
Pleccative("|1.5e-2")     # |0.015
Pleccative("0")           # 0

Passing a second argument alongside a string (such as Pleccative("|5", "+")) raises TypeError, matching complex(). Both str() and repr() round-trip cleanly back through Pleccative(...):

x = Pleccative(3.5, "|")
assert Pleccative(str(x)) == x
assert Pleccative(repr(x)) == x

Zero canonicalisation

Any zero-magnitude value canonicalises to positive zero (magnitude == 0.0, sign == Sign.POSITIVE), regardless of how it is constructed:

Pleccative(0, "|") == Pleccative("-0") == Pleccative(-0.0) == Pleccative(0)  # True

Inspecting values

Pleccative instances are immutable and expose read-only magnitude and sign properties, along with standard Python built-ins:

x = Pleccative("|3.5")

x.magnitude  # 3.5 (always a non-negative float)
x.sign       # Sign.PLECCATIVE
abs(x)       # 3.5
bool(x)      # True  (False only for 0)
str(x)       # '|3.5'
repr(x)      # '|3.5'

Unary operators (sign rotation)

Operator Meaning Cycle
+x Copy (0° rotation) + → +, - → -, | → |
-x Multiply by -1 (120° rotation) + → - → | → +
~x Multiply by |1 (240° rotation) + → | → - → +
x = Pleccative("+4")

+x      # +4
-x      # -4
-(-x)   # |4   (also equal to ~x)
-(-(-x))# +4   (three 120-degree rotations return to the start)

~x      # |4
~(~x)   # -4   (also equal to -x)
-(~x)   # +4   (- and ~ are inverses)

Zero is unaffected by rotation (-Pleccative(0) == ~Pleccative(0) == Pleccative(0)).

Arithmetic operations

All binary operators interoperate seamlessly with Python int and float values on either side. (bool values are not coerced and raise TypeError.)

Addition (+)

Same-sign values add their magnitudes; different-sign values cancel pairwise, keeping the sign of whichever operand has the larger magnitude:

Pleccative("|2") + Pleccative("|3")  # |5
Pleccative("+5") + Pleccative("|2")  # +3
Pleccative("|5") + Pleccative(-2)    # |3
Pleccative("+4") + Pleccative("|4")  # 0
Pleccative("|5") + 2                 # |3
5 + Pleccative("|2")                 # +3

Subtraction (-)

Subtraction is defined as a - b == a + (-b):

Pleccative("+5") - Pleccative("+2")  # +3
Pleccative("+5") - Pleccative("-2")  # +3  (since -(-2) == |2, which cancels +5)
Pleccative("+5") - Pleccative("|2")  # +7  (since -(|2) == +2)
Pleccative("-2") - Pleccative("-5")  # |3
Pleccative("|4") - Pleccative("|4")  # 0

Binary pleccation (|)

Binary | is the 240° counterpart to subtraction, defined as a | b == a + (~b):

Pleccative("+5") | Pleccative("+2")  # +3  (since ~(+2) == |2)
Pleccative("+5") | Pleccative("-2")  # +7  (since ~(-2) == +2)
Pleccative("|5") | Pleccative("+2")  # |7  (since ~(+2) == |2)
Pleccative("|4") | Pleccative("|4")  # 0

Operator precedence for |

In Python, bitwise | has lower precedence than +, -, *, and /. Use parentheses when combining | with other arithmetic operators, e.g. (a | b) * c.

Multiplication (*)

Magnitudes multiply and rotational signs add modulo 3:

Pleccative(-2) * Pleccative(-3)      # |6
Pleccative(-2) * Pleccative("|3")    # +6
Pleccative("|2") * Pleccative("|3")  # -6
3 * Pleccative("|4")                 # |12
-3 * Pleccative(-4)                  # |12
Pleccative("|5") * 0                 # 0

Division (/)

Magnitudes divide and rotational signs subtract modulo 3, acting as the exact inverse of multiplication:

Pleccative("+6") / Pleccative("-2")  # |3  (because |3 * -2 == +6)
Pleccative("+6") / Pleccative("|2")  # -3  (because -3 * |2 == +6)
Pleccative("|6") / Pleccative("-2")  # -3  (because -3 * -2 == |6)
Pleccative("|6") / Pleccative("|2")  # +3
6 / Pleccative("-2")                 # |3

Dividing by 0 or Pleccative(0) raises ZeroDivisionError.

Exponentiation (**)

Pleccative supports integer exponents (positive, zero, and negative):

Pleccative(-2) ** 0    # +1
Pleccative(-2) ** 1    # -2
Pleccative(-2) ** 2    # |4
Pleccative(-2) ** 3    # +8
Pleccative(-2) ** -1   # |0.5

Pleccative("|2") ** 2  # -4
Pleccative("|2") ** 3  # +8
Pleccative("|2") ** -1 # -0.5

Pleccative(0) ** 0     # +1
Pleccative(0) ** 3     # 0

Raising Pleccative(0) to a negative power raises ZeroDivisionError, and non-integer exponents (including float and bool) raise TypeError.

Equality, hashing, and ordering

  • Equality (==, !=): Positive, negative, and zero Pleccative values compare equal to matching int and float values; non-zero pleccative (|) values are never equal to any real number. bool values never compare equal to Pleccative.
  • Hashing: Pleccative is hashable and hashes consistently with int and float for positive, negative, and zero values, so instances work reliably in sets and as dict keys.
  • Ordering: Just like complex, pleccative numbers have no total order—<, <=, >, and >= raise TypeError.
Pleccative("+5") == 5       # True
Pleccative("-5") == -5      # True
Pleccative("|5") == 5       # False
Pleccative("|5") == -5      # False

labels = {Pleccative("+5"): "pos", Pleccative("-5"): "neg", Pleccative("|5"): "plec"}
labels[5]                   # 'pos'
labels[-5]                  # 'neg'
labels[Pleccative("|5")]    # 'plec'

Operator quick reference

Operation Description
Pleccative() Canonical zero (0)
Pleccative(x) Lift an int/float or parse a string ("+3", "-2.5", "|4")
Pleccative(m, s) Construct from non-negative magnitude m and sign s (+, -, |, or Sign)
+a, -a, ~a Copy (0°), rotate by 120°, rotate by 240°
a + b Pairwise-cancellation addition
a - b Subtraction (a + (-b))
a | b Pleccation (a + (~b))
a * b Multiplication (magnitudes multiply, signs add mod 3)
a / b Division (magnitudes divide, signs subtract mod 3)
a ** n Integer exponentiation (n: int)
abs(a), bool(a) Non-negative float magnitude; False iff zero
a == b, hash(a) Equality and consistent hashing with Pleccative, int, and float