frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

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

https://github.com/oliverkwebb/moonphase
67•oliverkwebb•1d ago

Comments

ericpruitt•1d ago
I adapted the same moonphase logic for the status line program I use with dwm. Did you run into any discrepancies with using January 0 vs December 31st? Some of the translations of Walker's as noted in my comment at https://github.com/ericpruitt/emus/blob/ea059239845ee6d57614... seem to produce differing results.
oliverkwebb•1d ago
This discrepancy with time is explained in the book the algorithm is based on (Practical Astronomy with Your Calculator, which also does a good job explaining a ton of other ideas and models in astronomical calculation).

January 0th makes sense because a year starts on January 1st. You count days of the year from 1 instead of from 0. So if we are going by the day of the year, day 1 of the year should be Jan1, the consequence of this being that Jan0 exists and is Dec31 of the prev year.

I originally got into this because of my status bar on i3, hacking together C code from moontool into https://github.com/oliverkwebb/moontool about a year back.

rybosome•1d ago
Fun! From the repo[0]:

> Your function must be self-contained, such that someone could copy-paste it into their code and not get an error. It also must not effect the environment outside it if at all possible (no #define or mutating globals). It must be as "pure" and self-contained as the language will allow.

This is an interesting restriction, actually, with respect to style. The “natural” way I would program such an algorithm certainly doesn’t adhere to this requirement.

Producing functions that are still correct, idiomatic and legible while producing minimal changes to environment or state requires a non-beginners understanding of the runtime nature of the language.

[0]: https://github.com/oliverkwebb/moonphase?tab=readme-ov-file#...

danparsonson•1d ago
Could you elaborate on this? Maybe an example of what you mean?
oliverkwebb•1d ago
This is also interesting for speed, The algorithms ran by all the impls are identical, but sacrificing speed for cleanliness and adherence to spirit is done regularly.

Without having actually measured, the rust impl probably runs faster than the C impl, but that's not because rust is "faster" than C. That's because I used a closure with one call to floor() for modulus in rust, whilst in C I did 2 fmod()'s to get it because the alternative would've been to make the code unreadable or add a second function/macro (although thinking about it now, undef does exist...)

blululu•1d ago
This is really nice. My initial thought was "why just divide the time interval since the Unix Epoch by the synodic period of the moon". Turns out the moon's phase is a bit more complicated than that. https://en.wikipedia.org/wiki/Lunar_phase#Calculating_phase
madcaptenor•1d ago
That said, this is basically what the Hebrew calendar does (although of course they use a much earlier epoch)
o11c•1d ago
`pom(6)` from `bsdgames` might already be installed on your system to do this. Source comments indicate it was originally based on prior versions of the same book but updated to the Third Edition in 1998.

For "implement simple task in many languages" you should probably think of Rosetta Code, now at https://rosettacode.miraheze.org/wiki/Rosetta_Code

lifthrasiir•1d ago
This code consistently converts angles to the [0, 360) range, which is IMO overkill. A better approach is to normalize everything to [0, 2) range so that you can simply multiply or divide by pi to get the radians. If your math library has `sinpi` or `cospi` it will be even more accurate, as it no longer has to divide by 2pi to do the actual calculation.
librasteve•1d ago
made a PR for raku, hope it gets accepted

some cool things stood out:

  sub fixangle($a) { $a mod 360 }    # raku has built-in support for Euclidean modulo
  pi                                 # raku has pi
  
  # Solve equation of Kepler
  my $e = $M; 
  my $delta;

  repeat {                           # raku's repeat loop allows initialisation of delta to be folded in
    $delta = $e - $eccent * sin($e) - $M; 
    $e -= $delta / (1 - $eccent * cos($e));
  } while abs($delta) > 1e-6;
eddyg•1d ago
Thanks! (I'm a big fan of Raku; it doesn't get nearly the respect it deserves!)
oliverkwebb•1d ago
Thank you! I accepted it
librasteve•1d ago
yay - it has made it to raku module status https://raku.land/zef:librasteve/Moonphase
anthk•1d ago
AWK can be far more than a DSL.
kbr2000•1d ago
LCAL[0]: The Moon Phase Calendar Program

A PostScript program to visualize a calendar of moon phases (skip down to "LCAL PostScript Calendar Examples" for just that). Did some nice PS prints recently for the next 10 years, adapted to fit in a frame I had laying around.

[0] https://pcal.sourceforge.net/

Deep learning gets the glory, deep fact checking gets ignored

https://rachel.fast.ai/posts/2025-06-04-enzyme-ml-fails/index.html
41•chmaynard•40m ago•3 comments

Destination: Jupiter

https://clarkesworldmagazine.com/liptak_06_25/
49•AndrewLiptak•2h ago•12 comments

Quarkdown: A modern Markdown-based typesetting system

https://github.com/iamgio/quarkdown
550•asicsp•14h ago•236 comments

Show HN: AirAP AirPlay server - AirPlay to an iOS Device

https://github.com/neon443/AirAP
101•neon443•1h ago•8 comments

The Small World of English

https://www.inotherwords.app/linguabase/
102•michaeld123•6h ago•53 comments

Show HN: An Alfred workflow to open GCP services and browse resources within

https://github.com/dineshgowda24/alfred-gcp-workflow
27•dineshgowda24•2h ago•5 comments

(On | No) Syntactic Support for Error Handling

https://go.dev/blog/error-syntax
259•henrikhorluck•5h ago•311 comments

Activeloop (YC S18) Is Hiring Senior Back End and AI Search Engineers(Onsite, MV)

https://careers.activeloop.ai/
1•davidbuniat•1h ago

Ask HN: Options for One-Handed Typing

42•Townley•2h ago•49 comments

Show HN: I wrote a Java decompiler in pure C language

https://github.com/neocanable/garlic
132•neocanable•9h ago•63 comments

Show HN: Localize React apps without rewriting code

https://github.com/lingodotdev/lingo.dev
43•maxpr•4h ago•38 comments

The Shape of the Essay Field

https://paulgraham.com/field.html
46•luisb•12h ago•30 comments

Show HN: Controlling 3D models with voice and hand gestures

https://github.com/collidingScopes/3d-model-playground
72•getToTheChopin•7h ago•14 comments

CVE-2024-47081: Netrc credential leak in PSF requests library

https://seclists.org/fulldisclosure/2025/Jun/2
30•jupenur•3h ago•5 comments

Swift at Apple: Migrating the Password Monitoring Service from Java

https://www.swift.org/blog/swift-at-apple-migrating-the-password-monitoring-service-from-java/
161•fidotron•5h ago•110 comments

Can adults grow new brain cells?

https://www.livescience.com/health/neuroscience/can-adults-grow-new-brain-cells
25•bookofjoe•1h ago•8 comments

Yoshua Bengio Launches LawZero: A New Nonprofit Advancing Safe-by-Design AI

https://lawzero.org/en/news/yoshua-bengio-launches-lawzero-new-nonprofit-advancing-safe-design-ai
28•WillieCubed•1h ago•15 comments

Vision Language Models Are Biased

https://vlmsarebiased.github.io/
101•taesiri•9h ago•78 comments

Gemini in Chrome

https://gemini.google/overview/gemini-in-chrome/?hl=en
33•aru•1h ago•35 comments

Technical Guide to System Calls: Implementation and Signal Handling in Modern OS

https://mohitmishra786.github.io/chessman/2025/03/31/Technical-Guide-to-System-Calls-Implementation-and-Signal-Handling-in-Modern-Operating-Systems.html
12•signa11•5h ago•3 comments

There should be no Computer Art (1971)

https://dam.org/museum/essays_ui/essays/there-should-be-no-computer-art/
62•glimshe•12h ago•92 comments

Show HN: PinSend – Share text between devices using a PIN(P2P, no login)

https://pinsend.app
47•avovsya•7h ago•27 comments

Builder.ai Collapses: $1.5B 'AI' Startup Exposed as 'Indians'

https://www.ibtimes.co.uk/builderai-collapses-15bn-ai-startup-exposed-actually-indians-pretending-bots-1734784
236•healsdata•8h ago•155 comments

Oh Fuck! How Do People Feel about Robots That Leverage Profanity

https://arxiv.org/abs/2505.05831
12•rolph•5h ago•10 comments

Covert Web-to-App Tracking via Localhost on Android

https://localmess.github.io/
239•sebastian_z•9h ago•191 comments

Plutonium Mountain: The 17-year mission to guard remains of Soviet nuclear tests

https://www.belfercenter.org/publication/plutonium-mountain-inside-17-year-mission-secure-legacy-soviet-nuclear-testing
74•jmillikin•12h ago•41 comments

Fun with Futex

https://blog.fredrb.com/2025/06/02/futex-fun/
70•ingve•15h ago•20 comments

The Metamorphosis of Prime Intellect (1994)

https://localroger.com/prime-intellect/mopiall.html
140•lawrenceyan•18h ago•67 comments

AWS forms EU-based cloud unit as customers fret

https://www.theregister.com/2025/06/03/aws_european_sovereign_cloud/
40•rntn•3h ago•35 comments

Implementing a Forth

https://ratfactor.com/forth/implementing
102•todsacerdoti•3d ago•38 comments