soveran / clac

Command-line, stack-based calculator with postfix notation
BSD 2-Clause "Simplified" License
356 stars 30 forks source link

Suggestion: Factorial function #12

Closed smiegrin closed 6 years ago

smiegrin commented 6 years ago

Hello hello! This is a stupendous tool that's being made here, I like it a lot!

I would like to know if a factorial function would be acceptable for this tool. For most functions, I suspect defining words would be sufficient, but in the case of factorials, there's a bit more than just a one-run set of operations. If factorial is defined, I suspect that most other probability problems I would use this tool with could use words that utilize that factorial function.

I imagine the use case being something like the following:

> 9 !
= 362880

or

> 9 fact
= 362880

Again, thank you for this wonderful tool!

soveran commented 6 years ago

Yes! Good idea, and I'm thinking about two ways to implement it. I will use the tgamma function from math.h, and we have a choice of exposing either gamma, !, or both. If we only expose gamma, we can construct ! with the following definition: ! "dup gamma *". And I think if we go that route, we should create the file /usr/local/share/clac/words with definitions like pi, avg, and !. Then the user of course can create a custom words file in $HOME/.config/clac/words and add their own commands, even overriding the previous definitions.

soveran commented 6 years ago

On second thought, maybe implementing ! as a built in is the way to go. Optionally we can also expose gamma. I will play with the different ideas and probably push the code tomorrow.

soveran commented 6 years ago

@smiegrin I added the factorial function as a built in. Can you check if it works for you?

soveran commented 6 years ago

It's available in version 0.3.2.