Implement factorial powered by the Gamma function.

This commit is contained in:
Danila Fedorin 2018-08-18 17:35:33 -07:00
parent cf961ec522
commit 5da9327e12
4 changed files with 20 additions and 1 deletions

2
external/libabacus vendored

@ -1 +1 @@
Subproject commit 8c9acafc932541d9a1c09f1fa0a93ac992254309 Subproject commit 01720914e0899023747c94ef7cf4c5d17782d7dd

View File

@ -55,6 +55,7 @@ FUNCTION(gt);
FUNCTION(gte); FUNCTION(gte);
FUNCTION(negate); FUNCTION(negate);
FUNCTION(factorial);
FUNCTION(ln); FUNCTION(ln);
FUNCTION(exp); FUNCTION(exp);

View File

@ -55,6 +55,7 @@ int main() {
ab.add_function("divide", function_divide, "(num, num)->num"); ab.add_function("divide", function_divide, "(num, num)->num");
ab.add_function("pow", function_pow, "(num, num)->num"); ab.add_function("pow", function_pow, "(num, num)->num");
ab.add_function("negate", function_negate, "(num)->num"); ab.add_function("negate", function_negate, "(num)->num");
ab.add_function("factorial", function_factorial, "(num)->num");
ab.add_function("ln", function_ln, "(num)->num"); ab.add_function("ln", function_ln, "(num)->num");
ab.add_function("exp", function_exp, "(num)->num"); ab.add_function("exp", function_exp, "(num)->num");
@ -78,6 +79,7 @@ int main() {
ab.add_operator_infix("/", "divide", -1, 3); ab.add_operator_infix("/", "divide", -1, 3);
ab.add_operator_infix("^", "pow", 1, 3); ab.add_operator_infix("^", "pow", 1, 3);
ab.add_operator_prefix("-", "negate"); ab.add_operator_prefix("-", "negate");
ab.add_operator_postfix("!", "factorial");
while(!close_requested) { while(!close_requested) {
char* data = readline(" > "); char* data = readline(" > ");

View File

@ -7,6 +7,22 @@ FUNCTION_MPFR2(divide, div)
FUNCTION_MPFR2(pow, pow); FUNCTION_MPFR2(pow, pow);
FUNCTION_MPFR(negate, neg); FUNCTION_MPFR(negate, neg);
FUNCTION(factorial) {
number* left = (number*) libab_unwrap_param(params, 0);
mpfr_t plus_one;
mpfr_t gamma;
mpfr_init2(plus_one, PRECISION);
mpfr_init2(gamma, PRECISION);
mpfr_add_ui(plus_one, left->value, 1, MPFR_RNDN);
mpfr_gamma(gamma, plus_one, MPFR_RNDN);
number* to_return = new number(std::move(gamma));
ref return_value = create_value<number>(ab, to_return);
libab_ref_copy(return_value, into);
return LIBAB_SUCCESS;
}
FUNCTION_COMPARE(lt, <); FUNCTION_COMPARE(lt, <);
FUNCTION_COMPARE(lte, <=); FUNCTION_COMPARE(lte, <=);