Developer Creates Multi-Language Moon Phase Calculator with Unique Self-Contained Function Requirement

BigGo Editorial Team
Developer Creates Multi-Language Moon Phase Calculator with Unique Self-Contained Function Requirement

A developer has released an open-source collection of moon phase calculation functions across multiple programming languages, dubbed the Werewolf Early Warning System. The project implements the same astronomical algorithm in eight different languages, from systems-level languages like C++ and Rust to scripting languages like Python and JavaScript.

Supported Languages:

  • Systems Level: C/C++, Rust, Zig
  • Scripting: Lua, JavaScript, Python
  • DSLs: awk, bc
  • Recent Addition: Raku

The Copy-Paste Rule Creates Interesting Programming Challenges

What makes this project particularly intriguing is its strict copy-paste rule requirement. Each function must be completely self-contained, allowing developers to copy and paste the code directly into their projects without dependencies or environmental changes. This constraint has sparked discussion among developers about how such restrictions affect coding style and performance.

The requirement forces programmers to think differently about their usual approaches. Instead of relying on external libraries or global variables, each implementation must be pure and standalone. This creates an interesting tension between clean code practices and practical constraints.

One developer noted that the Rust implementation likely runs faster than the C version, not because Rust is inherently faster, but because the self-contained requirement led to different optimization choices. The Rust code uses a single floor operation with a closure, while the C version required two fmod operations to maintain readability within the constraints.

Beyond Simple Time Division

The project addresses a common misconception about moon phase calculations. Many developers initially assume you can simply divide the time since Unix epoch by the moon's cycle period. However, lunar phases involve more complex astronomical calculations, including solving Kepler's equation and accounting for orbital mechanics.

The algorithm originates from John Walker's 1980s moontool program, which itself was based on Practical Astronomy With Your Calculator. Each implementation returns the moon's age in radians, which can then be converted to illuminated fraction, phase names, and emoji representations.

Key Algorithm Features:

  • Returns moon "age" in radians
  • Formula: (1-cos(x))/2 gives illuminated fraction
  • Moon age in days: multiply normalized age by ~29.5
  • Based on John Walker's 1980s moontool algorithm
  • Original source: "Practical Astronomy With Your Calculator"

Community Contributions and Technical Refinements

The project has attracted contributions from the programming community, including a recent addition of a Raku implementation. The Raku version showcases some elegant language features, including built-in Euclidean modulo support and native pi constants.

Technical discussions have emerged around optimization opportunities, with some developers suggesting that normalizing angles to a [0, 2) range instead of [0, 360) could improve both performance and accuracy, especially when using specialized math functions like sinpi and cospi.

The project demonstrates how programming constraints can lead to creative solutions and cross-language learning. By implementing identical algorithms across different languages while maintaining strict purity requirements, it offers insights into how language design affects code structure and performance trade-offs.

Reference: moonphase - Werewolf Early Warning System