#40: added command prec

This commit is contained in:
Louis Rubet 2017-05-22 14:29:55 +02:00
parent 5d8096088c
commit 6a0885a56c
3 changed files with 31 additions and 5 deletions

View file

@ -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"},

View file

@ -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);
}
}

View file

@ -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);
}
//