From 7825489b35163a0a67d53db26f6dcd0c9b770dc2 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 14 Aug 2018 19:05:14 -0700 Subject: [PATCH] Add negate operation. --- src/main.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 8090a68..725382e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -152,7 +152,7 @@ FUNCTION(to_string_num) { number* num = (number*) libab_unwrap_param(params, 0); mpfr_exp_t exp; char* str = mpfr_get_str(NULL, &exp, 10, requested_precision, num->value, MPFR_RNDN); - std::string output_string = std::string(str).insert(1, 1, '.'); + std::string output_string = std::string(str).insert((mpfr_sgn(num->value) < 0) ? 2 : 1, 1, '.'); if(exp != 1) { output_string += "e"; output_string += std::to_string(exp - 1); @@ -189,6 +189,16 @@ OPERATOR_FUNCTION(minus, -) OPERATOR_FUNCTION(times, *) OPERATOR_FUNCTION(divide, /) +FUNCTION(negate) { + number* value = (number*) libab_unwrap_param(params, 0); + mpfr_t negative; + mpfr_init2(negative, PRECISION); + mpfr_neg(negative, value->value, MPFR_RNDN); + abacus_ref to_return = create_value(ab, new number(std::move(negative))); + libab_ref_copy(to_return, into); + return LIBAB_SUCCESS; +} + FUNCTION(quit) { close_requested = true; libab_get_unit_value(ab, into); @@ -290,6 +300,7 @@ int main() { ab.add_function("minus", function_minus, "(num, num)->num"); ab.add_function("times", function_times, "(num, num)->num"); ab.add_function("divide", function_divide, "(num, num)->num"); + ab.add_function("negate", function_negate, "(num)->num"); while(!close_requested) { std::cout << "> ";