Constexpr factors_of

I’m just now getting around to blogging this snippet from March 2024 (hat tip to Chip Hogg). Apparently some units libraries — things like Au and mp-units — find it useful to represent linear combinations of units as the products of primes; for example, if you represent “meters” as 2 and “seconds” as 5, then “meter-seconds” is 10. Or at least that’s the general idea, I think. Don’t quote me on that part.

This means there’s a market for fast compile-time versions of the functions next_prime_after(p) and prime_factors_of(c). Chip even went so far as to propose that vendors should provide “largest prime factor of c” as a builtin; see P3133 “Fast first-factor finding function” (February 2024). I said, “You don’t need the vendor to do your factoring; you can write that in general-purpose C++ already!”

Read More

NYPL Musical of the Month

Today I learned that the New York Public Library has a blog, on which, many years ago, curator Douglas Reside had a “Musical of the Month” series where he posted old libretti, recordings, and other paraphernalia. Sadly the NYPL’s current website design lacks any coherent indexing or tagging system: no full-text search, no search by author, etc. So, for my own benefit and the benefit of posterity, here are direct links to all of the NYPL “Musical of the Month” blog posts — at least, all of them I could discover via public indices. If you discover any more, please tell me and I’ll add them here!

Read More

Dante’s hypersphere in THREE.js

Mark A. Peterson’s essay “Dante and the 3-sphere” (Am. J. Phys. 47(12), December 1979) suggests that when, in Paradiso 27, Dante looks from the starry sphere upward in the direction of the Empyrean and downward in the direction of Earth, and when he sees that nine concentric circles of angels above mirror the nine concentric planetary spheres below, he is “groping for a language to express an idea conceived intuitively and nonverbally” — namely, the idea of “how a 3-sphere would look from its equator.”

Read More

The crossword from Bored to Death S2E5

In HBO’s Bored to Death episode S2E5 “Forty-Two Down,” protagonist Jonathan Ames (Jason Schwartzman) introduces himself to his target Vikram (Ajay Naidu) by asking his help with that day’s New York Times crossword. We get two brief shots of the crossword itself. The first, a closeup, is just enough to irk the crossworders in the audience, as we see a plethora of unkeyed letters. The second medium shot shows that the black squares in this asymmetric grid spell out the word “HIT.” (Or does the black blob in front of the “H” represent a fourth letter?)

The episode aired 2010-10-24, but the Times shown is mostly the issue of Wednesday 2007-11-14: the facing column contains Mike Hale’s review of the TV show Kenny vs. Spenny, and the “Answer to Previous Puzzle” at lower left is indeed Fred Piscop’s puzzle of 2007-11-13. The crossword, however, is completely doctored — I see no resemblance to the Jim Page puzzle that actually ran on that date. The clues (which do not match the grid) are hilariously zany!

Read More

Fully Schrödinger crosswords

In December, Kory Mathewson tweeted this puzzle-creation challenge:

Create a 5x5 crossword puzzle with two distinct solutions. Each clue must work for both solutions. Do not use the same word in both solutions. No black squares.

Here’s my offering; solutions given at the end of this post.

Click for a PDF of the puzzle

Read More

What I watched and read this year

In 2024, for the first time, I kept a record of the media that I took in this year. Well, not “media” in the news-media sense (however relevant that might have been), but in the sense of books, films, stage shows; and to a lesser extent, journal articles, magazines, TV series, and long-form online content.

Read More

Labeled loop syntax in many languages

Many existing languages support labeled loops, such that you can say break foo; to break out of the loop labeled foo, instead of just breaking from the smallest enclosing loop. The C programming language just gained labeled loops, too, with the adoption of N3355 “Named loops” (Alex Celeste, 2024).

Erich Keane and Aaron Ballman reacted to N3355’s adoption with N3377 “An Improved Syntax for N3355,” which I very much hope will be voted down. It’s an “interesting” proposal, in that the authors are compiler engineers who work on a polyglot compiler (Clang), and yet they don’t seem to consider consistency with other languages to be a positive for C at all.

Read More

Should std::expected be [[nodiscard]]?

A correspondent writes to me that he’s switched from throw/catch exceptions to C++23’s std::expected in all new code. That is, where a traditional C++ program would have:

int f_or_throw();

int g_or_throw() {
  f_or_throw();
    // arguably OK, discards f's result
    // but propagates any exceptional error
  return 1;
}

his programs always have:

using Eint = std::expected<int, std::error_code>;
[[nodiscard]] Eint f_or_error() noexcept;

Eint g_or_error() {
  return f_or_error().transform([](int) {
    return 1;
  });
}

In the former case, we “discard” the result of f_or_throw() by simply discarding the value of the whole call-expression. That’s safe, because errors are always signaled by exceptions, which will be propagated up the stack regardless of what we do with the (non-exceptional, non-error) return value. This ensures that the error is never swallowed except on purpose.

Read More