#34: implements logs, real, test, trig commands with MPFR

This commit is contained in:
Louis Rubet 2017-04-20 22:46:48 +02:00
parent c2b3b2a35a
commit ee508bb605
8 changed files with 114 additions and 142 deletions

View file

@ -27,7 +27,6 @@ static ret_value get_fn(const char* fn_name, program_fn_t& fn, cmd_type_t& type)
static bool _cut(const char* entry, vector<string>& entries)
{
string tmp;
//TODO borner
int len = strlen(entry);
for (int i=0; i < len; i++)
@ -482,22 +481,6 @@ static bool _obj_from_string(const string& entry, object*& obj, unsigned int& ob
return ret;
}
static void _delete_obj_from_string(object* obj)
{
if (obj != NULL)
{
switch (obj->_type)
{
case cmd_number:
case cmd_binary:
break;
default:
free(obj);
break;
}
}
}
static char* entry_completion_generator(const char* text, int state)
{
static int list_index, len, too_far;
@ -561,15 +544,7 @@ static ret_value parse(const char* entry, program& prog)
// ex: entry="1 2+" -> vector<string> = {"1", "2+"} -> first "1", second "2" and remaining_entry="+"
// this remaining entry is treated as an entry
if(_obj_from_string(main_entry, obj, obj_size, type, remaining_entry))
{
prog.push_back(obj, obj_size, type);
//TODO wtf !?
//_delete_obj_from_string(obj);
}
else
{
// no syntax error for now, an unknown obj form is considered as a symbol
}
main_entry = remaining_entry;
}
}

View file

@ -1,105 +1,97 @@
#define _USE_MATH_DEFINES
//
void rpn_e(void)
{
//TODO
//putf(M_E);
number num;
CHECK_MPFR(mpfr_const_euler(&num._value.mpfr, s_mpfr_rnd));
_stack->push_back(&num, num.size(), cmd_number);
((number*)_stack->back())->ensure_significand();
}
void rpn_log()
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = log10(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_log10(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_alog()
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = pow(((number*)_stack->get_obj(0))->_value, 10);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_exp10(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_ln()
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = log(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_log(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_exp()
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = exp(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_exp(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_sinh()
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = sinh(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_sinh(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_asinh()
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
//floating_t value = ((number*)_stack->get_obj(0))->_value;
//if(value>0)
// value = log(value + sqrt(value * value + 1));
//else
// value = -log(-value + sqrt(value * value + 1));
//((number*)_stack->get_obj(0))->_value = value;
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_asinh(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_cosh()
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = cosh(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_cosh(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_acosh()
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
//floating_t value = ((number*)_stack->get_obj(0))->_value;
//if(value>0)
// value = log(value + sqrt(value * value - 1));
//else
// value = -log(-value + sqrt(value * value - 1));
//((number*)_stack->get_obj(0))->_value = value;
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_cosh(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_tanh()
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = tanh(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_tanh(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_atanh()
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
//floating_t value = ((number*)_stack->get_obj(0))->_value;
//value = (log(1 + value) - log(1 - value)) / 2;
//((number*)_stack->get_obj(0))->_value = value;
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_atanh(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}

View file

@ -22,17 +22,6 @@ void plus()
binary* left = (binary*)_stack->back();
left->_value += right->_value;
}
#if 0
// string
else if (IS_ARG_TYPE(0, cmd_string))
{
ARG_MUST_BE_OF_TYPE(1, cmd_string);
string& second = *((ostring*)_stack->back())->_value;
_stack->pop_back();
*((ostring*)_stack->back())->_value += second;
}
#endif
else
ERR_CONTEXT(ret_bad_operand_type);
}

View file

@ -86,7 +86,7 @@ void program::test()
if (stack_is != stack_should_be)
{
cout<<endl<<endl<<test_title<<endl<<entry<<endl<<"FAIL, ";
cout<<"real stack is '"<<stack_is<<endl;
cout<<"real stack is "<<stack_is<<endl;
failed = true;
indicate_passed = false;
}

View file

@ -1,5 +1,3 @@
//TODO must work with binary, ostring (others ?)
void sup(void)
{
MIN_ARGUMENTS(2);

View file

@ -1,72 +1,86 @@
#define _USE_MATH_DEFINES
//
void pi(void)
{
//TODO
//putf(M_PI);
number num;
CHECK_MPFR(mpfr_const_pi(&num._value.mpfr, s_mpfr_rnd));
_stack->push_back(&num, num.size(), cmd_number);
((number*)_stack->back())->ensure_significand();
}
void d2r(void)
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value *= M_PI / 360;
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t pi;
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_const_pi(&pi.mpfr, s_mpfr_rnd));
CHECK_MPFR(mpfr_mul(&left->mpfr, &left->mpfr, &pi.mpfr, s_mpfr_rnd));
CHECK_MPFR(mpfr_div_si(&left->mpfr, &left->mpfr, 180, s_mpfr_rnd));
}
void r2d(void)
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value *= 360 / M_PI;
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t pi;
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_const_pi(&pi.mpfr, s_mpfr_rnd));
CHECK_MPFR(mpfr_mul_si(&left->mpfr, &left->mpfr, 180, s_mpfr_rnd));
CHECK_MPFR(mpfr_div(&left->mpfr, &left->mpfr, &pi.mpfr, s_mpfr_rnd));
}
void rpn_sin(void)
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = sin(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_sin(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_asin(void)
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = asin(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_asin(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_cos(void)
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = cos(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_cos(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_acos(void)
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = acos(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_acos(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_tan(void)
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = tan(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_tan(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}
void rpn_atan(void)
{
//TODO
//MIN_ARGUMENTS(1);
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
//((number*)_stack->get_obj(0))->_value = atan(((number*)_stack->get_obj(0))->_value);
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
floating_t* left = &((number*)_stack->get_obj(0))->_value;
CHECK_MPFR(mpfr_atan(&left->mpfr, &left->mpfr, s_mpfr_rnd));
}

View file

@ -192,7 +192,7 @@ struct number : public object
_type = cmd_number;
_value = value;
}
unsigned int size() { return (unsigned int)sizeof(floating_t); }
unsigned int size() { return (unsigned int)sizeof(number); }
void ensure_significand()
{
@ -376,7 +376,6 @@ struct branch : public object
branch_fn_t _fn;
// args used by cmd_branch cmds
int arg1, arg2, arg3;
//TODO change to int
floating_t farg1, farg2;
bool arg_bool;
unsigned int _len;
@ -385,11 +384,14 @@ struct branch : public object
void object::show(ostream& stream)
{
//TODO please don't do that
char buffer_for_number[256];
switch(_type)
{
case cmd_number:
((number*)this)->ensure_significand();
(void)mpfr_printf(s_mpfr_printf_format.c_str(), &((number*)this)->_value.mpfr);
(void)mpfr_sprintf(buffer_for_number, s_mpfr_printf_format.c_str(), &((number*)this)->_value.mpfr);
stream<<buffer_for_number;
break;
case cmd_binary:
{
@ -573,7 +575,7 @@ public:
{
// for if-then-else-end
vector<struct if_layout_t> vlayout;
int layout_index=-1;// TODO remplacable par vlayout.size()-1
int layout_index=-1;
// for start-end-step
vector<int> vstartindex;
@ -930,6 +932,8 @@ int main(int argc, char* argv[])
program::show_stack(global_stack, separator);
}
}
mpfr_free_cache();
return ret;
}

View file

@ -1,6 +1,6 @@
// version and soft name
static const char version[] = "1.3.3";
static const char uname[] = "rpn v1.3.3, (c) 2013 <louis@rubet.fr>, GNU General Public License";
static const char version[] = "2.0 RC1";
static const char uname[] = "rpn v2.0 RC1, (c) 2013 <louis@rubet.fr>, GNU General Public License";
static const char g_cursor[] = "> ";
static const string g_show_stack_separator = "> ";
@ -9,7 +9,7 @@ static const string g_show_stack_separator = "> ";
static const char* syntax[] = {
ATTR_BOLD "R" ATTR_OFF "everse "
ATTR_BOLD "P" ATTR_OFF "olish "
ATTR_BOLD "N" ATTR_OFF "otation language, based on hewlett-Packard RPL",
ATTR_BOLD "N" ATTR_OFF "otation language, based on Hewlett-Packard RPL",
"",
"Syntax: rpn [command]",
"with optional command = list of commands",