A fascinating exploration of how CasNum implements arbitrary precision arithmetic using compass and straightedge constructions, complete with a Game Boy emulator where every calculation is performed through geometric principles.
In an era where computational speed is often the primary metric of success, one developer has taken a radically different approach. CasNum, a new library from GitHub user 0x0mer, implements arbitrary precision arithmetic using nothing but compass and straightedge constructions—the same geometric methods employed by ancient Greek mathematicians. The result is both a technical curiosity and a philosophical statement about what computation could be.

The Geometry of Computation
At its core, CasNum begins with the same foundations as classical geometry: two points, the origin and a unit. From these humble beginnings, the library implements five basic compass-and-straightedge constructions that form the basis of its entire computational system:
- Constructing a line through two points
- Drawing a circle with one point as center and another on its circumference
- Finding the intersection point of two non-parallel lines
- Determining intersection points between a line and a circle
- Finding intersection points between two circles
These operations, according to the project's documentation, function as the Instruction Set Architecture (ISA) for this unusual computational approach. The complexity becomes immediately apparent when considering that finding the intersection of two circles requires solving a 4th-degree equation—a formula so complex it spans over 3,600 characters in the code.
Representing Numbers as Geometry
The CasNum class represents numbers as points on a Cartesian plane, with the number x corresponding to the point (x,0). This geometric representation allows for the implementation of arithmetic operations through constructions rather than traditional computational methods.
Addition, for example, is implemented by finding the midpoint between two points and then doubling it—both standard compass-and-straightedge operations. Multiplication and division leverage triangle similarity principles, while logical operations like AND, OR, and XOR are described by the author as "a little uglier" but functional.
The implementation leaves significant room for optimization. The creator notes that multiplication by 2 can be performed much more efficiently than the generic multiplication algorithm, and modulo operations benefit from removing the highest power of two times the modulus before proceeding.
A Game Boy Through Euclidean Eyes
Perhaps the most striking demonstration of CasNum's capabilities is its integration with a Game Boy emulator. By modifying PyBoy, a popular Game Boy emulator, the library replaces the emulator's integer arithmetic with compass-and-straightedge constructions.

This integration allows the emulator to run actual Game Boy cartridges using only geometric computation. The creator has tested this with 2048 and mentions that Pokémon Red can also run, albeit with significant performance limitations. The first boot of Pokémon takes approximately 15 minutes, after which calculations are cached, improving performance to 0.5-1 FPS—described as "totally almost playable."
The visualization component, implemented in casnum/cas/viewer.py, allows users to watch the compass-and-straightedge constructions in action as the emulator executes instructions. The viewer includes automatic zoom functionality that works well for simpler examples but may require manual adjustment for more complex operations like RSA encryption.
Performance Considerations
Given that CasNum performs calculations through geometric constructions rather than optimized CPU operations, performance is a significant consideration. The library relies heavily on Python's lru_cache to store results of expensive calculations, but memory usage can become substantial as the cache grows.
The time and space complexity are both acknowledged to be substantial, with the documentation humorously noting "Time Complexity: Yes. Space Complexity: Also yes." This computational expense is inherent to the approach—every arithmetic operation requires solving geometric problems that would be trivial for traditional processors.
The Philosophy Behind the Compass
Beyond its technical novelty, CasNum embodies a particular philosophy about computation and the nature of mathematical operations. The documentation playfully critiques modern developers' contentment with simple operations like a + b, suggesting that CasNum is for "the developer who believes that if you didn't have to solve a 4th-degree polynomial just to increment a loop counter, you didn't really increment it."
This perspective transforms computation from an invisible background process into a visible, almost tangible process. Each operation becomes a geometric construction that can be observed and understood, rather than a black box executed at electronic speed.
Practical Applications and Limitations
While CasNum is primarily a demonstration project, the creator has implemented two practical examples: a basic RSA encryption program and the Game Boy emulator integration. These examples showcase how the library can handle complex computational tasks despite its unconventional approach.
The project's dependencies are relatively minimal, consisting mainly of sympy for mathematical operations and pyglet for visualization. The RSA example requires pycryptodome, while the Game Boy integration uses a modified version of PyBoy under the LGPL license.
Despite its technical achievements, CasNum has clear limitations. Its performance makes it impractical for most real-world applications, and the complexity of implementing even simple operations would deter all but the most determined developers. However, as an educational tool or a philosophical statement about computation, it offers unique value.
Conclusion
CasNum represents a fascinating intersection of ancient mathematics and modern computing. By implementing arbitrary precision arithmetic through compass-and-straightedge constructions, it challenges our assumptions about what computation looks like and how it should perform.
The project's GitHub repository contains all the necessary code for experimentation, along with examples and detailed documentation. For developers interested in the theoretical foundations of computation, alternative programming paradigms, or simply the beauty of geometric solutions to computational problems, CasNum offers a unique perspective.
In a world increasingly dominated by black-box AI systems and opaque algorithms, CasNum reminds us that computation can be transparent, understandable, and even beautiful. Whether it will inspire new approaches to computation or remain a fascinating curiosity remains to be seen, but its existence expands the boundaries of what we consider possible in programming.
For those interested in exploring this unusual approach to computation further, the CasNum GitHub repository contains the complete implementation, examples, and documentation. The PyBoy emulator modified to use CasNum is also available for comparison with the original implementation.

Comments
Please log in or register to join the discussion