Skip to content

Instantly share code, notes, and snippets.

@eddieantonio
Created April 3, 2026 19:36
Show Gist options
  • Select an option

  • Save eddieantonio/fdd7f47c932047eda1fb18ca349c1f34 to your computer and use it in GitHub Desktop.

Select an option

Save eddieantonio/fdd7f47c932047eda1fb18ca349c1f34 to your computer and use it in GitHub Desktop.
moon
#include <stddef.h>
#include <stdint.h>
size_t moon_phase_index(float day) {
static const float BOUNDS[7] = {3.69, 7.38, 11.07, 14.76, 18.45, 22.14, 25.83};
uint_fast8_t a = 0;
if (day >= BOUNDS[a + 3]) a = a + 4;
if (day >= BOUNDS[a + 1]) a = a + 2;
if (day >= BOUNDS[a + 0]) a = a + 1;
return a;
}
#include <stdio.h>
int main() {
for (float t = 0.0; t < 30; t += 1.0) {
printf("%2.1f -> %lu\n", t, moon_phase_index(t));
}
return 0;
}
from typing import NamedTuple
from random import random
class Phase(NamedTuple):
name: str
PHASES = [
Phase("New Moon"),
Phase("Waxing Cresent"),
Phase("First Quarter"),
Phase("Waxing Gibbous"),
Phase("Full Moon"),
Phase("Waning Gibbous"),
Phase("Third Quarter"),
Phase("Waning Crescent"),
]
def moon_phase_index(day: float) -> int:
PHASE_UPPER_BOUNDS = [3.69, 7.38, 11.07, 14.76, 18.45, 22.14, 25.83]
a = 0
a = [a, a + 4][day >= PHASE_UPPER_BOUNDS[a + 3]]
a = [a, a + 2][day >= PHASE_UPPER_BOUNDS[a + 1]]
return [a, a + 1][day >= PHASE_UPPER_BOUNDS[a]]
for t in range(30):
i = moon_phase_index(0.0 + t)
print(t, i, PHASES[i].name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment