The Boston Diaries

The ongoing saga of Sean Conner, who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Saturday, August 01, 2026

The precedence of the factorial operator

Back when I added a factorial operator to my assembler, the code only applied it to numeric literals because the code that implemented it appeared in the same function as parsing a numeric literal. I only found the bug when I tried to apply a factorial to a subexpression in parenthesis, for example, -(2+1)!.

The fix was easy, just move the code out of the numberic literal parsing routine rvalue() to rfactor() where the unary + and - occur, as well as handing subexpressions. But the fix lead to another issue—how to handle a sequence like -3!. Should it error out since negative values aren't defined for factorial? Or should it apply the factorial to “3” and then the unary minus sign?

I asked the question over on Lobters and a very loose consensus seemed to be that factorial has a higher precedence than unary minus. I also threw -3! at Wolfram Alpha (that I forgot existed until I started doing some web searches on the question) and it also handled factorial before unary minus.

So that's what I went with. From what little I found on this topic, it seems that -3! should indeed return -6 as an answer. It also seems to be the mathematical convention that -32 should return -9 and not 9 (which is that my assembler returns, as well as Microsoft Excel). Fixing that issue would require a rewrite of the expression parser. The Shunting Yard algorithm I use to handle precedence doesn't handle unary operators all that well.

Sigh.

Obligatory Picture

[Self-portrait with a Christmas Tree] Oh Chrismtas Tree!  My Christmas Tree!  Rise up and hear the bells!

Obligatory Contact Info

Obligatory Feeds

Obligatory Links

Obligatory Miscellaneous

Obligatory AI Disclaimer

No AI was used in the making of this site, unless otherwise noted.

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site: https://boston.conman.org/, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

https://boston.conman.org/2000/08/01

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2026 by Sean Conner. All Rights Reserved.