卡斯号 (Kǎsī hào)
CasNum

原始链接: https://github.com/0x0mer/CasNum

## CasNum:圆规与直尺算术 CasNum 是一个 Python 库,仅使用圆规和直尺构造实现任意精度算术——本质上,重现古希腊人进行计算的方式。数字表示为平面上的点,加法、乘法甚至逻辑函数等基本运算都由五个基本几何构造构建而成:直线创建、圆定义和点相交。 该项目包含一个功能完备但速度较慢的修改版 Game Boy 模拟器,其中*所有* ALU 操作都以几何方式执行。这展示了该库的功能,甚至允许(最终!)像《口袋妖怪 红》这样的游戏运行——尽管由于计算强度,启动可能需要长达 15 分钟。 CasNum 利用缓存来提高性能,但仍然计算成本高昂且内存密集。它专为那些对算术的独特、基础方法以及对计算极限的趣味探索感兴趣的人而设计。该库提供示例和可视化工具,以观察几何构造。它依赖于 `sympy`、`pyglet` 和修改版的 `PyBoy` 等库。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 CasNum (github.com/0x0mer) 7 分,aebtebeten 发表于 17 分钟前 | 隐藏 | 过去 | 收藏 | 1 条评论 帮助 0x0mer 3 分钟前 [–] 感谢发布,意义重大!:) 我很乐意知道你是如何发现它的。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

CasNum (Compass and straightedge Number) is a library that implements arbitrary precision arithmetic using compass and straightedge constructions. Arbitrary precision arithmetic, now with 100% more Euclid. Featuring a functional modified Game Boy emulator where every ALU opcode is implemented entirely through geometric constructions. Demo

  1. Introduction To Compass And Straightedge Constructions
  2. Possible Uses
    1. Integration inside a Gameboy Emulator
  3. Examples and how to run
  4. Philosophy
  5. Performance
  6. Dependencies
  7. F.A.Q
  8. License and Third-Party Credits

Introduction To Compass And Straightedge Constructions

This project began with a simple compass-and-straightedge 'engine', which can be found under the directory cas/. In compass-and-straightedge constructions, one start with just two points: The origin, and a unit. Exactly as God intended. The engine then allows us to do what the ancients did:

  • Construct the line through two points
  • Construct the circle that contains one point and has a center at another point
  • Construct the point at the intersection of two (non-parallel) lines
  • Construct the one or two points in the intersection of a line and a circle (if they intersect)
  • Construct the one point or two points in the intersection of two circles (if they intersect) (Which, by the way turns out to be a nasty 4th degree equation. Check out the formula in circle.py, over 3600 characters, yikes. Good thing we have WolframAlpha).

These five constructions are considered the basic compass and straightedge constructions. Think of these as your ISA.

On top of the compass-and-straightedge engine, we have the CasNum class. In CasNum, a number x is represented as the point (x,0) in the plane. Now, the fun part: implementing all arithmetic and logical operations. We can construct the addition of two points by finding the midpoint between them and doubling it, which are both standard compass-and-straightedge constructions. Then, we can build the product and quotient of numbers using triangle similarity. The logical operations (AND, OR, XOR) are a little uglier, since they are not a "clean algebraic operation" in the relevant sense, but, hey, it works right?

What I thought was pretty neat is that implementing all this from scratch leaves a lot of room for optimization. For example, multiplication by 2 can be implemented much more efficiently than the generic algorithm for multiplication using triangle similarity. Then, implementing modulo by first removing the highest power of two times the modulus from the dividend yielded much better results than the naive implementation.

  • Simple RSA program
  • Integrate into the ALU of a Game Boy emulator, thus obtaining a Game Boy that arithmetically and logically runs solely on compass and straightedge constructions
  • More? idk

The first two examples were actually implemented and can be found under the examples/ directory. So apparently one cannot square the circle using a compass and a straightedge, but at least one can run Pokémon Red. Man, I'm sure the ancient Greeks would have loved to see this.

Integration inside a Game Boy Emulator

Thanks to the great code written by PyBoy, integrating CasNum within it was pretty seamless. The only file I needed to edit was opcodes_gen.py, and the edit was pretty minimal.

As always, please save any important work before running anything I ever write.

To clone the repo, and install requirements:

git clone --recursive [email protected]:0x0mer/CasNum.git
cd CasNum
pip install -r requirements.txt

You can run the rsa and basic examples from the repo's root directory like so:

python3 -m examples.basic
python3 -m examples.rsa

The library comes with a viewer (casnum/cas/viewer.py) that shows the compass and straightedge constructions. It has an automatic zoom that kinda works, but it goes crazy in the rsa example, so you may want to use manual zoom there.

In order to run PyBoy, first you need a ROM. In order to avoid copyright infringement, I included the ROM for 2048, free to distribute under the zlib license. But if, for example, the ROM you have is 'Pokemon.gb', then you can place it in examples/Pyboy and run:

cd examples/PyBoy
pip install -r requirements.txt
PYTHONPATH=../.. python

Then, once in python, run:

from pyboy import PyBoy
from casnum import viewer
viewer.start()
pyboy = PyBoy('2048.gb') # Or whatever ROM you have
while pyboy.tick():
    pass

pyboy.stop()

the viewer.start() just displays the compass-and-straightedge constructions, it is not strictly needed, but it is fun.

Notice however that the first run of Pokemon on the Game Boy emulator takes approximately 15 minutes to boot, so playing it may require somewhat increased patience. You see, Euclid wouldn't have optimized the Game Boy boot screen. He would have spent those 15 minutes in silent appreciation, thinking, "Yeah. That’s about how long that should take."

After running it once, most calculations should already be cached if you run it from the same python interpreter instance, so on the second run you should be able to get a decent 0.5~1 FPS, which is totally almost playable.

Most modern developers are content with a + b. They don't want to work for it. They don't want to see the midpoint being birthed from the intersection of two circles. 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.

Python's lru_cache is used to cache almost any calculation done in the library, as everything is so expensive. Memory usage may blow up, run at your own risk.

  • Time Complexity: Yes
  • Space Complexity: Also yes
  • sympy
  • pyglet (optional but highly recommended. Only needed if you want to display the compass-and-straightedge constructions)
  • pytest-lazy-fixtures (Only needed in order to run the tests)
  • pycryptodome (Only needed if you want to run the rsa example)
  • Euclid Postulate V (optional)
  1. Q: buT cAN iT rUn dOOm?

    A: It can't really "run" anything, its a number.

  2. Q: Is it fast?

    A: Define "fast". If you mean "faster than copying Euclid by hand", then yes, dramatically.

  3. Q: Why did you make this?

    A: I wanted arbitrary precision arithmetic, but I also wanted to feel something.

License and Third-Party Credits

The code in the root of this repository is licensed under the MIT License.

This project incorporates the following third-party materials:

  • PyBoy (Modified): Located in ./examples/PyBoy/. Distributed under the GNU Lesser General Public License (LGPL) v3.0.

    • Notice of Modification: This version of PyBoy has been modified from the original source code to use the CasNum library instead of Python's int.
    • The original, unmodified source code for PyBoy can be found at: https://github.com/Baekalfen/PyBoy.
    • The full LGPL license text is available in ./examples/PyBoy/License.md.
  • 2048.gb: This Game Boy ROM binary is distributed under the zlib License.

    • Disclaimer: This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
联系我们 contact @ memedata.com