mirror of
https://github.com/louisrubet/rpn
synced 2024-12-30 10:23:32 +01:00
#5: used correct mpfr_t obj instead of internal MPFR struct
This commit is contained in:
parent
537984acd3
commit
db320874b6
10 changed files with 115 additions and 127 deletions
|
@ -340,7 +340,7 @@ static bool get_float(const string& entry, program& prog, string& remaining_entr
|
|||
number* num = (number*)prog.allocate_back((unsigned int)sizeof(number), cmd_number, MPFR_128BITS_STORING_LENGTH, &significand);
|
||||
num->init(significand);
|
||||
|
||||
int mpfr_ret = mpfr_strtofr(&num->_value.mpfr, entry.c_str(), &endptr, 0, MPFR_DEF_RND);
|
||||
int mpfr_ret = mpfr_strtofr(num->_value.mpfr, entry.c_str(), &endptr, 0, MPFR_DEF_RND);
|
||||
if (endptr != entry.c_str())
|
||||
{
|
||||
ret = true;
|
||||
|
|
|
@ -26,7 +26,7 @@ void rtob()
|
|||
number* left = (number*)_stack->pop_back();
|
||||
|
||||
binary bin;
|
||||
bin.set(mpfr_get_sj(&left->_value.mpfr, s_mpfr_rnd));
|
||||
bin.set(mpfr_get_sj(left->_value.mpfr, s_mpfr_rnd));
|
||||
_stack->push_back(&bin, bin.size(), cmd_binary);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ int rpn_if(branch& myobj)
|
|||
MIN_ARGUMENTS_RET(1, -1);
|
||||
ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, -1);
|
||||
|
||||
if (mpfr_cmp_si(&((number*)_stack->get_obj(0))->_value.mpfr, 0UL) != 0)
|
||||
if (mpfr_cmp_si(((number*)_stack->get_obj(0))->_value.mpfr, 0UL) != 0)
|
||||
myobj.arg1 = 1;
|
||||
else
|
||||
myobj.arg1 = 0;
|
||||
|
@ -55,27 +55,15 @@ int rpn_start(branch& myobj)
|
|||
|
||||
// farg1 = first value of start command
|
||||
// farg2 = last value of start command
|
||||
cout<<__FUNCTION__<<": copy_and_push_back(farg2)"<<endl;
|
||||
stack::copy_and_push_back(*_stack, 0, _branch_stack);
|
||||
cout<<"[(number*)_stack->get_obj(0)="; ((number*)_stack->get_obj(0))->show(cout);cout<<"]\n";
|
||||
cout<<"(number*)_stack->get_obj(0)="<<(void*)_stack->get_obj(0)<<"=";
|
||||
dump8((unsigned char*)_stack->get_obj(0), 0, 40);
|
||||
myobj.farg2 = (number*)_branch_stack.get_obj(0);
|
||||
myobj.farg2->init(_branch_stack.get_blob(0));
|
||||
_stack->pop_back();
|
||||
cout<<"myobj.farg2="<<(void*)myobj.farg2<<"=";
|
||||
dump8((unsigned char*)myobj.farg2, 0, 40);
|
||||
cout<<"[-> arg2="; myobj.farg2->show(cout);cout<<"]\n";
|
||||
|
||||
cout<<__FUNCTION__<<": copy_and_push_back(farg1)"<<endl;
|
||||
stack::copy_and_push_back(*_stack, 0, _branch_stack);
|
||||
cout<<"[(number*)_stack->get_obj(0)="; ((number*)_stack->get_obj(0))->show(cout);cout<<"]\n";
|
||||
cout<<"[_branch_stack.size()="<<_branch_stack.size()<<"]\n";
|
||||
myobj.farg1 = (number*)_branch_stack.get_obj(0);
|
||||
myobj.farg1->init(_branch_stack.get_blob(0));
|
||||
_stack->pop_back();
|
||||
cout<<"[myobj.farg2->_value.mpfr._mpfr_d="; myobj.farg2->_value.mpfr._mpfr_d;cout<<"]\n";
|
||||
cout<<"[-> arg2="; myobj.farg2->show(cout);cout<<"]\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -118,7 +106,7 @@ int rpn_next(branch& myobj)
|
|||
}
|
||||
|
||||
// increment then test
|
||||
mpfr_add_si(&myobj.farg1->_value.mpfr, &myobj.farg1->_value.mpfr, 1UL, MPFR_DEF_RND);
|
||||
mpfr_add_si(myobj.farg1->_value.mpfr, myobj.farg1->_value.mpfr, 1UL, MPFR_DEF_RND);
|
||||
|
||||
// for command: increment symbol too
|
||||
if (start_or_for->arg1 != -1)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
void rpn_e(void)
|
||||
{
|
||||
number num;
|
||||
CHECK_MPFR(mpfr_const_euler(&num._value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_const_euler(num._value.mpfr, s_mpfr_rnd));
|
||||
_stack->push_back(&num, num.size(), cmd_number);
|
||||
((number*)_stack->back())->ensure_significand();
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ void rpn_log()
|
|||
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));
|
||||
CHECK_MPFR(mpfr_log10(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_alog()
|
||||
|
@ -21,7 +21,7 @@ void rpn_alog()
|
|||
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));
|
||||
CHECK_MPFR(mpfr_exp10(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_ln()
|
||||
|
@ -30,7 +30,7 @@ void rpn_ln()
|
|||
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));
|
||||
CHECK_MPFR(mpfr_log(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_exp()
|
||||
|
@ -39,7 +39,7 @@ void rpn_exp()
|
|||
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));
|
||||
CHECK_MPFR(mpfr_exp(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_sinh()
|
||||
|
@ -48,7 +48,7 @@ void rpn_sinh()
|
|||
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));
|
||||
CHECK_MPFR(mpfr_sinh(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_asinh()
|
||||
|
@ -57,7 +57,7 @@ void rpn_asinh()
|
|||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||
|
||||
floating_t* left = &((number*)_stack->get_obj(0))->_value;
|
||||
CHECK_MPFR(mpfr_asinh(&left->mpfr, &left->mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_asinh(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_cosh()
|
||||
|
@ -66,7 +66,7 @@ void rpn_cosh()
|
|||
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));
|
||||
CHECK_MPFR(mpfr_cosh(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_acosh()
|
||||
|
@ -75,7 +75,7 @@ void rpn_acosh()
|
|||
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));
|
||||
CHECK_MPFR(mpfr_cosh(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_tanh()
|
||||
|
@ -84,7 +84,7 @@ void rpn_tanh()
|
|||
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));
|
||||
CHECK_MPFR(mpfr_tanh(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_atanh()
|
||||
|
@ -93,5 +93,5 @@ void rpn_atanh()
|
|||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||
|
||||
floating_t* left = &((number*)_stack->get_obj(0))->_value;
|
||||
CHECK_MPFR(mpfr_atanh(&left->mpfr, &left->mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_atanh(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ void plus()
|
|||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
CHECK_MPFR(mpfr_add(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_add(left->_value.mpfr, left->_value.mpfr, right->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
// binary
|
||||
else if (IS_ARG_TYPE(0, cmd_binary))
|
||||
|
@ -35,7 +35,7 @@ void minus()
|
|||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
CHECK_MPFR(mpfr_sub(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_sub(left->_value.mpfr, left->_value.mpfr, right->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
// binary
|
||||
else if (IS_ARG_TYPE(0, cmd_binary))
|
||||
|
@ -59,7 +59,7 @@ void mul()
|
|||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
CHECK_MPFR(mpfr_mul(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_mul(left->_value.mpfr, left->_value.mpfr, right->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
// binary
|
||||
else if (IS_ARG_TYPE(0, cmd_binary))
|
||||
|
@ -85,7 +85,7 @@ void div()
|
|||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
CHECK_MPFR(mpfr_div(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_div(left->_value.mpfr, left->_value.mpfr, right->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
// binary
|
||||
else if (IS_ARG_TYPE(0, cmd_binary))
|
||||
|
@ -114,7 +114,7 @@ void neg()
|
|||
if (IS_ARG_TYPE(0, cmd_number))
|
||||
{
|
||||
number* left = (number*)_stack->back();
|
||||
CHECK_MPFR(mpfr_neg(&left->_value.mpfr, &left->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_neg(left->_value.mpfr, left->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
// binary
|
||||
else if (IS_ARG_TYPE(0, cmd_binary))
|
||||
|
@ -129,7 +129,7 @@ void inv()
|
|||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||
|
||||
number* left = (number*)_stack->back();
|
||||
CHECK_MPFR(mpfr_si_div(&left->_value.mpfr, 1L, &left->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_si_div(left->_value.mpfr, 1L, left->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void purcent()
|
||||
|
@ -141,8 +141,8 @@ void purcent()
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
CHECK_MPFR(mpfr_mul(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_div_si(&left->_value.mpfr, &left->_value.mpfr, 100L, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_mul(left->_value.mpfr, left->_value.mpfr, right->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_div_si(left->_value.mpfr, left->_value.mpfr, 100L, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void purcentCH()
|
||||
|
@ -154,8 +154,8 @@ void purcentCH()
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
CHECK_MPFR(mpfr_mul_si(&right->_value.mpfr, &right->_value.mpfr, 100L, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_div(&left->_value.mpfr, &right->_value.mpfr, &left->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_mul_si(right->_value.mpfr, right->_value.mpfr, 100L, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_div(left->_value.mpfr, right->_value.mpfr, left->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void power()
|
||||
|
@ -166,7 +166,7 @@ void power()
|
|||
|
||||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
CHECK_MPFR(mpfr_pow(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_pow(left->_value.mpfr, left->_value.mpfr, right->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void squareroot()
|
||||
|
@ -175,7 +175,7 @@ void squareroot()
|
|||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||
|
||||
number* left = (number*)_stack->back();
|
||||
CHECK_MPFR(mpfr_sqrt(&left->_value.mpfr, &left->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_sqrt(left->_value.mpfr, left->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void square()
|
||||
|
@ -184,7 +184,7 @@ void square()
|
|||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||
|
||||
number* left = (number*)_stack->back();
|
||||
CHECK_MPFR(mpfr_sqr(&left->_value.mpfr, &left->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_sqr(left->_value.mpfr, left->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void modulo()
|
||||
|
@ -196,5 +196,5 @@ void modulo()
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
CHECK_MPFR(mpfr_fmod(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_fmod(left->_value.mpfr, left->_value.mpfr, right->_value.mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
|
|
@ -1,17 +1,15 @@
|
|||
stack _stack_manip;
|
||||
|
||||
//
|
||||
void swap(void)
|
||||
{
|
||||
MIN_ARGUMENTS(2);
|
||||
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-1, _stack_manip);
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-2, _stack_manip);
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-1, _branch_stack);
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-2, _branch_stack);
|
||||
(void)_stack->pop_back();
|
||||
(void)_stack->pop_back();
|
||||
stack::copy_and_push_back(_stack_manip, 0, *_stack);
|
||||
stack::copy_and_push_back(_stack_manip, 1, *_stack);
|
||||
_stack_manip.erase();
|
||||
stack::copy_and_push_back(_branch_stack, 0, *_stack);
|
||||
stack::copy_and_push_back(_branch_stack, 1, *_stack);
|
||||
(void)_branch_stack.pop_back();
|
||||
(void)_branch_stack.pop_back();
|
||||
}
|
||||
|
||||
void drop(void)
|
||||
|
@ -67,17 +65,18 @@ void rot(void)
|
|||
{
|
||||
MIN_ARGUMENTS(3);
|
||||
|
||||
//TODO could be optimized
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-3, _stack_manip);
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-2, _stack_manip);
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-1, _stack_manip);
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-3, _branch_stack);
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-2, _branch_stack);
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-1, _branch_stack);
|
||||
(void)_stack->pop_back();
|
||||
(void)_stack->pop_back();
|
||||
(void)_stack->pop_back();
|
||||
stack::copy_and_push_back(_stack_manip, 2, *_stack);
|
||||
stack::copy_and_push_back(_stack_manip, 1, *_stack);
|
||||
stack::copy_and_push_back(_stack_manip, 0, *_stack);
|
||||
_stack_manip.erase();
|
||||
stack::copy_and_push_back(_branch_stack, 2, *_stack);
|
||||
stack::copy_and_push_back(_branch_stack, 1, *_stack);
|
||||
stack::copy_and_push_back(_branch_stack, 0, *_stack);
|
||||
(void)_branch_stack.pop_back();
|
||||
(void)_branch_stack.pop_back();
|
||||
(void)_branch_stack.pop_back();
|
||||
}
|
||||
|
||||
void depth(void)
|
||||
|
|
|
@ -7,10 +7,10 @@ void sup(void)
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) > 0)
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) > 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
void sup_eq(void)
|
||||
|
@ -22,10 +22,10 @@ void sup_eq(void)
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) >= 0)
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) >= 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
void inf(void)
|
||||
|
@ -37,10 +37,10 @@ void inf(void)
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) < 0)
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) < 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
void inf_eq(void)
|
||||
|
@ -52,10 +52,10 @@ void inf_eq(void)
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) <= 0)
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) <= 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
void diff(void)
|
||||
|
@ -67,10 +67,10 @@ void diff(void)
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) != 0)
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) != 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
void eq(void)
|
||||
|
@ -82,10 +82,10 @@ void eq(void)
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) == 0)
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if (mpfr_cmp(left->_value.mpfr, right->_value.mpfr) == 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
void test_and(void)
|
||||
|
@ -97,11 +97,11 @@ void test_and(void)
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
if ((mpfr_cmp_si(&left->_value.mpfr, 0) == 0)
|
||||
&& (mpfr_cmp_si(&right->_value.mpfr, 0) == 0))
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if ((mpfr_cmp_si(left->_value.mpfr, 0) == 0)
|
||||
&& (mpfr_cmp_si(right->_value.mpfr, 0) == 0))
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
void test_or(void)
|
||||
|
@ -113,11 +113,11 @@ void test_or(void)
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
if ((mpfr_cmp_si(&left->_value.mpfr, 0) == 0)
|
||||
|| (mpfr_cmp_si(&right->_value.mpfr, 0) == 0))
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if ((mpfr_cmp_si(left->_value.mpfr, 0) == 0)
|
||||
|| (mpfr_cmp_si(right->_value.mpfr, 0) == 0))
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
void test_xor(void)
|
||||
|
@ -129,19 +129,19 @@ void test_xor(void)
|
|||
number* right = (number*)_stack->pop_back();
|
||||
number* left = (number*)_stack->back();
|
||||
|
||||
if (mpfr_cmp_si(&left->_value.mpfr, 0) == 0)
|
||||
if (mpfr_cmp_si(left->_value.mpfr, 0) == 0)
|
||||
{
|
||||
if (mpfr_cmp_si(&right->_value.mpfr, 0) != 0)
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if (mpfr_cmp_si(right->_value.mpfr, 0) != 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mpfr_cmp_si(&right->_value.mpfr, 0) == 0)
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if (mpfr_cmp_si(right->_value.mpfr, 0) == 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -151,10 +151,10 @@ void test_not(void)
|
|||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||
|
||||
number* left = (number*)_stack->back();
|
||||
if (mpfr_cmp_si(&left->_value.mpfr, 0) == 0)
|
||||
mpfr_set_si(&left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
if (mpfr_cmp_si(left->_value.mpfr, 0) == 0)
|
||||
mpfr_set_si(left->_value.mpfr, 1, s_mpfr_rnd);
|
||||
else
|
||||
mpfr_set_si(&left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
mpfr_set_si(left->_value.mpfr, 0, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
void same(void)
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
void pi(void)
|
||||
{
|
||||
number num;
|
||||
CHECK_MPFR(mpfr_const_pi(&num._value.mpfr, s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_const_pi(num._value.mpfr, s_mpfr_rnd));
|
||||
_stack->push_back(&num, num.size(), cmd_number);
|
||||
((number*)_stack->back())->ensure_significand();
|
||||
}
|
||||
|
@ -14,9 +14,9 @@ void d2r(void)
|
|||
|
||||
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));
|
||||
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)
|
||||
|
@ -26,9 +26,9 @@ void r2d(void)
|
|||
|
||||
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));
|
||||
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)
|
||||
|
@ -37,7 +37,7 @@ void rpn_sin(void)
|
|||
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));
|
||||
CHECK_MPFR(mpfr_sin(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_asin(void)
|
||||
|
@ -46,7 +46,7 @@ void rpn_asin(void)
|
|||
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));
|
||||
CHECK_MPFR(mpfr_asin(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_cos(void)
|
||||
|
@ -55,7 +55,7 @@ void rpn_cos(void)
|
|||
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));
|
||||
CHECK_MPFR(mpfr_cos(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_acos(void)
|
||||
|
@ -64,7 +64,7 @@ void rpn_acos(void)
|
|||
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));
|
||||
CHECK_MPFR(mpfr_acos(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_tan(void)
|
||||
|
@ -73,7 +73,7 @@ void rpn_tan(void)
|
|||
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));
|
||||
CHECK_MPFR(mpfr_tan(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
||||
void rpn_atan(void)
|
||||
|
@ -82,5 +82,5 @@ void rpn_atan(void)
|
|||
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));
|
||||
CHECK_MPFR(mpfr_atan(left->mpfr, left->mpfr, s_mpfr_rnd));
|
||||
}
|
||||
|
|
37
src/rpn.cpp
37
src/rpn.cpp
|
@ -106,52 +106,52 @@ typedef long long integer_t;
|
|||
// MPFR object
|
||||
struct floating_t
|
||||
{
|
||||
__mpfr_struct mpfr;
|
||||
mpfr_t mpfr;
|
||||
|
||||
void init(void* significand)
|
||||
{
|
||||
mpfr_custom_init(significand, MPFR_128BITS_STORING_LENGTH);
|
||||
mpfr_custom_init_set(&mpfr, MPFR_ZERO_KIND, 0, s_mpfr_prec, significand);
|
||||
mpfr_custom_init_set(mpfr, MPFR_ZERO_KIND, 0, s_mpfr_prec, significand);
|
||||
}
|
||||
|
||||
void set_significand(void* significand)
|
||||
{
|
||||
mpfr._mpfr_d = (mp_limb_t*)significand;
|
||||
mpfr->_mpfr_d = (mp_limb_t*)significand;
|
||||
}
|
||||
|
||||
floating_t& operator=(const long int val)
|
||||
{
|
||||
mpfr_set_si(&mpfr, val, s_mpfr_rnd);
|
||||
mpfr_set_si(mpfr, val, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
floating_t& operator=(const unsigned long val)
|
||||
{
|
||||
mpfr_set_ui(&mpfr, val, s_mpfr_rnd);
|
||||
mpfr_set_ui(mpfr, val, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
floating_t& operator=(const integer_t val)
|
||||
{
|
||||
mpfr_set_sj(&mpfr, val, s_mpfr_rnd);
|
||||
mpfr_set_sj(mpfr, val, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
operator int()
|
||||
{
|
||||
return (int)mpfr_get_si(&mpfr, s_mpfr_rnd);
|
||||
return (int)mpfr_get_si(mpfr, s_mpfr_rnd);
|
||||
}
|
||||
|
||||
void ensure_significand()
|
||||
{
|
||||
//mpfr._mpfr_d = (mp_limb_t*)significand;
|
||||
//mpfr->_mpfr_d = (mp_limb_t*)significand;
|
||||
}
|
||||
|
||||
bool operator>(const floating_t right)
|
||||
{
|
||||
return mpfr_cmp(&mpfr, &right.mpfr) > 0;
|
||||
return mpfr_cmp(mpfr, right.mpfr) > 0;
|
||||
}
|
||||
|
||||
bool operator<(const floating_t right)
|
||||
{
|
||||
return mpfr_cmp(&mpfr, &right.mpfr) < 0;
|
||||
return mpfr_cmp(mpfr, right.mpfr) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -190,14 +190,14 @@ 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, MPFR_128BITS_STORING_LENGTH);
|
||||
}
|
||||
|
||||
//
|
||||
void set(const floating_t& value)
|
||||
{
|
||||
_type = cmd_number;
|
||||
_value.mpfr._mpfr_d = value.mpfr._mpfr_d;
|
||||
_value.mpfr->_mpfr_d = value.mpfr->_mpfr_d;
|
||||
}
|
||||
|
||||
void set(long value)
|
||||
|
@ -221,7 +221,7 @@ struct number : public object
|
|||
|
||||
void ensure_significand()
|
||||
{
|
||||
//_value.mpfr._mpfr_d = (mp_limb_t*)_value.significand;
|
||||
//_value.mpfr->_mpfr_d = (mp_limb_t*)_value.significand;
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -382,8 +382,8 @@ struct branch : public object
|
|||
arg1 = -1;
|
||||
arg2 = -1;
|
||||
arg3 = -1;
|
||||
farg1 = 0L;
|
||||
farg2 = 0L;
|
||||
farg1 = NULL;
|
||||
farg2 = NULL;
|
||||
arg_bool = 0;
|
||||
if (value != NULL)
|
||||
{
|
||||
|
@ -401,7 +401,7 @@ struct branch : public object
|
|||
branch_fn_t _fn;
|
||||
// args used by cmd_branch cmds
|
||||
int arg1, arg2, arg3;
|
||||
floating_t farg1, farg2;
|
||||
number *farg1, *farg2;
|
||||
bool arg_bool;
|
||||
unsigned int _len;
|
||||
char _value[0];
|
||||
|
@ -415,7 +415,7 @@ void object::show(ostream& stream)
|
|||
{
|
||||
case cmd_number:
|
||||
//(void)mpfr_printf(s_mpfr_printf_format.c_str(), &((number*)this)->_value.mpfr);
|
||||
(void)mpfr_sprintf(buffer, s_mpfr_printf_format.c_str(), &((number*)this)->_value.mpfr);
|
||||
(void)mpfr_sprintf(buffer, s_mpfr_printf_format.c_str(), ((number*)this)->_value.mpfr);
|
||||
stream<<buffer;
|
||||
break;
|
||||
case cmd_binary:
|
||||
|
@ -576,6 +576,7 @@ public:
|
|||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -833,6 +834,7 @@ private:
|
|||
string _err_context;
|
||||
|
||||
stack* _stack;
|
||||
stack _branch_stack;
|
||||
|
||||
heap* _global_heap;
|
||||
heap _local_heap;
|
||||
|
@ -884,7 +886,6 @@ private:
|
|||
#define ARG_MUST_BE_OF_TYPE(num, type) do { if (_stack->get_type(num) != (type)) { ERR_CONTEXT(ret_bad_operand_type); return; } } while(0)
|
||||
#define ARG_MUST_BE_OF_TYPE_RET(num, type, ret) do { if (_stack->get_type(num) != (type)) { ERR_CONTEXT(ret_bad_operand_type); return (ret); } } while(0)
|
||||
#define IS_ARG_TYPE(num, type) (_stack->get_type(num) == (type))
|
||||
//TODO
|
||||
#define CHECK_MPFR(op) do { (void)(op); } while(0)
|
||||
|
||||
// keywords implementation
|
||||
|
|
|
@ -61,7 +61,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
// index_from is counted from back, last entry is 0
|
||||
//
|
||||
static void copy_and_push_back(stack& from, unsigned int index_from, stack& to)
|
||||
{
|
||||
// copy a whole stack entry (with blob) and push it back to another stack
|
||||
|
@ -83,6 +83,7 @@ public:
|
|||
_base = (char*)realloc(_base, _total_size);
|
||||
_current = _base + offset;
|
||||
}
|
||||
|
||||
if (_current_blob + blob_size > _blob + _total_blob_size)
|
||||
{
|
||||
unsigned long offset = _current_blob - _blob;
|
||||
|
@ -105,7 +106,6 @@ public:
|
|||
_vtype.push_back(type);
|
||||
_count++;
|
||||
|
||||
//cout<<__FUNCTION__<<": -> stack size is "<<_count<<endl;//lru
|
||||
return allocated;
|
||||
}
|
||||
|
||||
|
@ -118,7 +118,7 @@ public:
|
|||
_current = _vpointer[_count - 1];
|
||||
_vlen.pop_back();
|
||||
_vpointer.pop_back();
|
||||
|
||||
|
||||
_current_blob = _vpointer_blob[_count - 1];
|
||||
_vlen_blob.pop_back();
|
||||
_vpointer_blob.pop_back();
|
||||
|
|
Loading…
Reference in a new issue