Hasty Briefsbeta

Bilingual

Show HN: Moon Phase Algorithms for C, Lua, Awk, JavaScript, etc.

a year ago
  • #programming
  • #moon-phase
  • #astronomy
  • A collection of snippets calculates the moon's phase in various programming languages.
  • Supported languages include C/C++, Rust, Zig, Lua, JavaScript, Python, awk, and bc.
  • Functions take time input (e.g., Unix epoch seconds) and return the moon's age in radians.
  • The illuminated fraction of the moon's surface is derived using (1-cos(x))/2.
  • Moon age can be converted to days by multiplying by ~29.5.
  • Phase index is determined using illuminated fraction and angle for naming and emoji representation.
  • All implementations are self-contained, pure, and based on John Walker's moontool algorithm from the 80s.
  • Test cases are included to demonstrate illuminated fraction and percent calculations.