From 6a0885a56c660e0041e8879d96e23b27a072f23c Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Mon, 22 May 2017 14:29:55 +0200 Subject: [PATCH] #40: added command prec --- src/rpn-cmd.h | 5 ++++- src/rpn-general.h | 22 ++++++++++++++++++++++ src/rpn.cpp | 9 +++++---- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/rpn-cmd.h b/src/rpn-cmd.h index 116bf9a..970d739 100644 --- a/src/rpn-cmd.h +++ b/src/rpn-cmd.h @@ -12,8 +12,11 @@ program::keyword_t program::_keywords[] = { cmd_keyword, "test", &program::test, "" }, //not seen by user { cmd_keyword, "version", &program::rpn_version, "show rpn version" }, { cmd_keyword, "uname", &program::rpn_uname, "show rpn complete identification string" }, - { cmd_keyword, "type", &program::type, "show first stack entry type" }, + { cmd_keyword, "type", &program::type, "show type of stack first entry" }, { cmd_keyword, "default", &program::rpn_default, "set float representation and precision to default" }, + { cmd_keyword, "prec", &program::precision, "get float precision in bits when first stack is not a number\n\t" + "set float precision in bits when first stack entry is a number. ex: 256 prec\n\t" }, + { cmd_keyword, "round", &program::rpn_default, "set float rounding mode.\n\tex: [\"nearest\", \"toward zero\", \"toward +inf\", \"toward +inf\", \"away from zero\"] round" }, //REAL { cmd_undef, "", NULL, "\nREAL"}, diff --git a/src/rpn-general.h b/src/rpn-general.h index 772c755..f6e9d23 100644 --- a/src/rpn-general.h +++ b/src/rpn-general.h @@ -139,3 +139,25 @@ void rpn_default() { program::apply_default(); } + +void precision() +{ + if (stack_size()>0 && _stack->get_type(0) == cmd_number) + { + //set MPFR float precision + long prec = mpfr_get_si(((number*)_stack->pop_back())->_value.mpfr, s_mpfr_rnd); + if (prec >= 0) + { + s_mpfr_prec = (mpfr_prec_t)prec; + s_mpfr_prec_bytes = mpfr_custom_get_size(prec); + } + else + ERR_CONTEXT(ret_out_of_range); + } + else + { + // get MPFR float precision + number* num = (number*)_stack->allocate_back(number::calc_size(), cmd_number); + num->set((long)s_mpfr_prec); + } +} diff --git a/src/rpn.cpp b/src/rpn.cpp index 036e2d0..1bccc52 100644 --- a/src/rpn.cpp +++ b/src/rpn.cpp @@ -43,7 +43,7 @@ using namespace std; #define DEFAULT_PRECISION 12 // MPFR related constants -// 128 bits significand storing length in byters, result of mpfr_custom_get_size(128) +// 128 bits significand storing length in bytes, result of mpfr_custom_get_size(128) #define MPFR_DEF_RND MPFR_RNDN #define MPFR_128BITS_PREC 128 #define MPFR_128BITS_STORING_LENGTH 16 @@ -56,6 +56,7 @@ static string s_mpfr_printf_format_hex = "%Ra"; static string s_mpfr_printf_format = "%.12Rg"; static mpfr_prec_t s_mpfr_prec = MPFR_128BITS_PREC; static mpfr_rnd_t s_mpfr_rnd = MPFR_DEF_RND; +static unsigned int s_mpfr_prec_bytes = MPFR_128BITS_STORING_LENGTH; // #include "escape.h" @@ -106,7 +107,7 @@ struct floating_t void init(void* significand) { - mpfr_custom_init(significand, MPFR_128BITS_STORING_LENGTH); + mpfr_custom_init(significand, MPFR_128BITS_PREC); mpfr_custom_init_set(mpfr, MPFR_ZERO_KIND, 0, s_mpfr_prec, significand); } @@ -175,7 +176,7 @@ struct number : public object void copy(number& op) { _value = op._value; - memcpy(_value.mpfr->_mpfr_d, op._value.mpfr->_mpfr_d, MPFR_128BITS_STORING_LENGTH); + memcpy(_value.mpfr->_mpfr_d, op._value.mpfr->_mpfr_d, s_mpfr_prec_bytes); } // @@ -199,7 +200,7 @@ struct number : public object static unsigned int calc_size() { - return (unsigned int)(sizeof(number)+MPFR_128BITS_STORING_LENGTH); + return (unsigned int)(sizeof(number)+s_mpfr_prec_bytes); } //