1 hour ago · Tech · hide · 0 comments

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…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.