unary (C)
Arithmetic on unary numerals using only string operations. A C port of unary.py.
A natural number n is a string of n marks. Zero is the empty string.
| operation | string operation |
|---|---|
| addition | concatenation |
| subtraction | suffix removal (truncation) |
| multiplication | substitution (each mark → a copy of the other numeral) |
order (<=) |
prefix test |
| divmod | repeated removal |
| power | repeated multiplication |
| gcd | Euclid |
| to binary | repeated pairing-off |
| from binary | Horner: double the string, append a mark if bit is 1 |
Layout
unary/ one byte per mark ('|' characters, NUL-terminated)
unary_packed/ one bit per mark, packed into uint64_t words <- preferred
unary.h public API
unary.c implementation
unary_test.c self-tests
Both directories expose the same function names, so code written against one
compiles against the other (the Unary struct fields differ: s vs w).
Build and test
cd unary_packed
gcc -std=c11 -O2 -Wall -Wextra unary.c unary_test.c -o unary_test
./unary_test
With sanitizers:
gcc -std=c11 -g -Wall -Wextra -Wpedantic -fsanitize=address,undefined \
unary.c unary_test.c -o unary_test_san && ./unary_test_san
Use it in your own code by compiling unary.c alongside your sources and
including unary.h.
API
typedef struct { uint64_t *w; size_t len; } Unary; /* packed version */
typedef enum { U_OK, U_ERR_NOMEM, U_ERR_INVALID, U_ERR_UNDERFLOW,
U_ERR_DIV0, U_ERR_OVERFLOW } UStatus;
const char *u_strerror(UStatus st);
/* construction / reading (the external observer's counting) */
UStatus u_from_str (Unary *out, const char *marks); /* '|' per mark */
UStatus u_from_size(Unary *out, size_t n);
size_t u_to_size (const Unary *a);
UStatus u_to_str (char **out, const Unary *a); /* malloc'd */
void u_free (Unary *a);
/* order */
bool u_eq(a, b); bool u_le(a, b); bool u_lt(a, b); bool u_is_zero(a);
/* arithmetic */
UStatus u_succ (Unary *out, const Unary *a);
UStatus u_add (Unary *out, const Unary *a, const Unary *b);
UStatus u_sub (Unary *out, const Unary *a, const Unary *b); /* U_ERR_UNDERFLOW if b > a */
UStatus u_mul (Unary *out, const Unary *a, const Unary *b);
UStatus u_divmod(Unary *q, Unary *r, const Unary *a, const Unary *b); /* U_ERR_DIV0 if b == 0 */
UStatus u_pow (Unary *out, const Unary *a, const Unary *e); /* 0**0 == 1 */
UStatus u_gcd (Unary *out, const Unary *a, const Unary *b);
/* binary conversion */
UStatus u_to_binary (char **out, const Unary *a); /* malloc'd; caller free() */
UStatus u_from_binary(Unary *out, const char *bits);
Example:
Unary a, b, r;
u_from_size(&a, 6);
u_from_size(&b, 7);
if (u_mul(&r, &a, &b) == U_OK) {
printf("%zu\n", u_to_size(&r)); /* 42 */
u_free(&r);
}
u_free(&a); u_free(&b);
Ownership rules
- Every function that fills an
outnumeral allocates it; release it withu_free(). outmust not alias an input.- On error,
outis left empty (NULLbuffer) and needs no freeing. u_to_binaryandu_to_strreturnmalloc'd strings; release withfree().
Packed representation
Bit i of the numeral lives in w[i / 64] at position i % 64.
Canonical form, relied on by u_eq and u_le:
- every bit below
lenis 1; - every bit at or above
len(in the last word and beyond) is 0.
Every function preserves this. u_check_invariant() verifies it and is used by
the tests.
Assumptions and caveats
Nothing here is a physical claim. Flagged explicitly:
- Conventions of the reader. The operations are conventions a reader applies to strings of marks; a bare collection of marks has no arithmetic of its own.
- Pairing convention. Binary conversion treats two adjacent marks as one pair. This presupposes the reader can recognise "two" as a pair of marks.
lenis bookkeeping. A C buffer must carry its size.lenis used for buffer sizing, bit positions and overflow guards; the arithmetic itself scans and copies marks.u_to_size()is the external observer's count (it wasto_int()in Python).- Contents are determined by
len. Every stored mark is 1, so the buffer holds no information beyond its length. Packing reduces memory 8× relative to one byte per mark; it does not change the O(n) space of a unary numeral. It is also whyto_binary's "pairs become the new number" step is a truncation in the packed version. - Overflow guards are an addition. Size computations return
U_ERR_OVERFLOWwhere Python would simply run out of memory. - Growth. Multiplication and exponentiation produce n·m and n^k marks. At one bit per mark this is cheaper, but it still exhausts memory quickly. A 64-bit-sized value is out of reach for any unary numeral.
Testing
- Reference arithmetic is native
size_tand an independent binary formatter, so the checks do not reuse the code under test. - Each result is also checked for canonical form, independently of the arithmetic reference.
- Operand values straddle 64-bit word boundaries (63/64/65, 127/128/129, 191/192/193).
- The harness validates itself: a deliberately wrong add (
a+b+1) must be flagged, and a numeral with a stray bit pastlenmust be rejected by the invariant check. If either check passes silently the test run aborts. - Coverage: all pairs 0..200 for add; 0..300 against the boundary set for
sub, mul, divmod, gcd, order, binary round trips and error cases;
**over small ranges plus 3^5.
Measured performance
Single runs, -O2, one machine, identical benchmark source for both versions.
Treat as indicative, not definitive.
| operation | byte-per-mark | bit-packed |
|---|---|---|
| mul 6000×6000 (36M marks), ×5 | 0.061 s | 0.005 s |
| add 36M + 36M, ×20 | 0.626 s | 0.027 s |
| divmod 36M / 7 | 0.031 s | 0.019 s |
| to_binary 36M | 0.031 s | 0.044 s |
A 36M-mark numeral occupies 36 MB in the byte version and 4.5 MB when packed.
to_binary is slower when packed because its pair-finding loop reads two bits
per step; a word-parallel version would fix that.
Metadata
Release files for unary 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| unary-0.1.0.tar.gz | 8.0 kB | Without provenance | Details for unary-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| unary-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for unary-0.1.0-py3-none-any.whl |
Total release size: 15.2 kB
Release files / unary-0.1.0.tar.gz
| Download URL | unary-0.1.0.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
73f86c39e58054d63034b96335b25aae363c8fe1a377e12399fdf28ee756113c
|
|
BLAKE2b-256 checksum How to use checksums |
9ee2c8b0a0bc2122c44c738b31bac72a870b705c662f263ab13eded4d88593c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / unary-0.1.0-py3-none-any.whl
| Download URL | unary-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
da61140965db46e590a508f2b70ca508f74e4b6bd7d5e8bc2204a3a712dccd94
|
|
BLAKE2b-256 checksum How to use checksums |
5a3ac632872ceead176b9d4291996cd5cc14339d1491ce479886d1f7e449299c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for unary 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.