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.

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论