mirror of
https://github.com/louisrubet/rpn
synced 2025-01-19 10:26:22 +01:00
#34: MPFR lib, general and real calc, some branch
This commit is contained in:
parent
4670aa77c0
commit
7ddde5dea5
7 changed files with 252 additions and 184 deletions
|
@ -562,15 +562,8 @@ static ret_value parse(const char* entry, program& prog)
|
||||||
// this remaining entry is treated as an entry
|
// this remaining entry is treated as an entry
|
||||||
if(_obj_from_string(main_entry, obj, obj_size, type, remaining_entry))
|
if(_obj_from_string(main_entry, obj, obj_size, type, remaining_entry))
|
||||||
{
|
{
|
||||||
cout<<"parse->obj = "<<obj<<endl;
|
|
||||||
prog.push_back(obj, obj_size, type);
|
prog.push_back(obj, obj_size, type);
|
||||||
if (((object*)prog.back())->_type == cmd_number)
|
//TODO wtf !?
|
||||||
{
|
|
||||||
cout<<"((number*)prog.back())="<<(void*)prog.back()<<endl;
|
|
||||||
cout<<" (((number*)prog.back())->_value.mpfr._mpfr_d = "<<((number*)prog.back())->_value.mpfr._mpfr_d<<")"<<endl;
|
|
||||||
((number*)prog.back())->_value.mpfr._mpfr_d = (mp_limb_t*)((number*)prog.back())->_value.significand;
|
|
||||||
cout<<" -> (((number*)prog.back())->_value.mpfr._mpfr_d = "<<((number*)prog.back())->_value.mpfr._mpfr_d<<")"<<endl;
|
|
||||||
}
|
|
||||||
//_delete_obj_from_string(obj);
|
//_delete_obj_from_string(obj);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -56,7 +56,9 @@ int rpn_start(branch& myobj)
|
||||||
// farg1 = first value of start command
|
// farg1 = first value of start command
|
||||||
// farg2 = last value of start command
|
// farg2 = last value of start command
|
||||||
myobj.farg2 = getf();
|
myobj.farg2 = getf();
|
||||||
|
myobj.farg2.ensure_significand();
|
||||||
myobj.farg1 = getf();
|
myobj.farg1 = getf();
|
||||||
|
myobj.farg1.ensure_significand();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +74,9 @@ int rpn_for(branch& myobj)
|
||||||
// farg2 = last value of for command
|
// farg2 = last value of for command
|
||||||
// arg1 = index of symbol to increase
|
// arg1 = index of symbol to increase
|
||||||
myobj.farg2 = getf();
|
myobj.farg2 = getf();
|
||||||
|
myobj.farg2.ensure_significand();
|
||||||
myobj.farg1 = getf();
|
myobj.farg1 = getf();
|
||||||
|
myobj.farg1.ensure_significand();
|
||||||
|
|
||||||
// store symbol with first value
|
// store symbol with first value
|
||||||
number num;
|
number num;
|
||||||
|
@ -91,6 +95,7 @@ int rpn_next(branch& myobj)
|
||||||
{
|
{
|
||||||
myobj.arg_bool = true;
|
myobj.arg_bool = true;
|
||||||
myobj.farg1 = start_or_for->farg1;
|
myobj.farg1 = start_or_for->farg1;
|
||||||
|
myobj.farg1.ensure_significand();
|
||||||
}
|
}
|
||||||
|
|
||||||
// increment then test
|
// increment then test
|
||||||
|
@ -105,7 +110,10 @@ int rpn_next(branch& myobj)
|
||||||
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
|
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
|
||||||
// check symbol variable is a number, then increase
|
// check symbol variable is a number, then increase
|
||||||
if (_local_heap.get(string(var->_value), obj, size, type) && (type == cmd_number))
|
if (_local_heap.get(string(var->_value), obj, size, type) && (type == cmd_number))
|
||||||
|
{
|
||||||
((number*)obj)->_value = myobj.farg1;
|
((number*)obj)->_value = myobj.farg1;
|
||||||
|
((number*)obj)->ensure_significand();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//test value
|
//test value
|
||||||
|
@ -136,6 +144,7 @@ int rpn_step(branch& myobj)
|
||||||
{
|
{
|
||||||
myobj.arg_bool = true;
|
myobj.arg_bool = true;
|
||||||
myobj.farg1 = start_or_for->farg1;
|
myobj.farg1 = start_or_for->farg1;
|
||||||
|
myobj.farg1.ensure_significand();
|
||||||
}
|
}
|
||||||
|
|
||||||
// increment then test
|
// increment then test
|
||||||
|
@ -150,7 +159,10 @@ int rpn_step(branch& myobj)
|
||||||
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
|
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
|
||||||
// check symbol variable is a number, then increase
|
// check symbol variable is a number, then increase
|
||||||
if (_local_heap.get(string(var->_value), obj, size, type) && (type == cmd_number))
|
if (_local_heap.get(string(var->_value), obj, size, type) && (type == cmd_number))
|
||||||
|
{
|
||||||
((number*)obj)->_value = myobj.farg1;
|
((number*)obj)->_value = myobj.farg1;
|
||||||
|
((number*)obj)->ensure_significand();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//test value
|
//test value
|
||||||
|
|
|
@ -27,9 +27,11 @@ program::keyword_t program::_keywords[] =
|
||||||
{ cmd_keyword, "inv", &program::inv, "unarity operator inverse (1/)" },
|
{ cmd_keyword, "inv", &program::inv, "unarity operator inverse (1/)" },
|
||||||
{ cmd_keyword, "%", &program::purcent, "binary operator purcent" },
|
{ cmd_keyword, "%", &program::purcent, "binary operator purcent" },
|
||||||
{ cmd_keyword, "%CH", &program::purcentCH, "binary operator inverse purcent" },
|
{ cmd_keyword, "%CH", &program::purcentCH, "binary operator inverse purcent" },
|
||||||
{ cmd_keyword, "^", &program::power, "binary operator power" },
|
{ cmd_keyword, "^", &program::power, "(or pow) binary operator power" },
|
||||||
|
{ cmd_keyword, "pow", &program::power, "" },
|
||||||
{ cmd_keyword, "sqrt", &program::squareroot, "unarity operator square root" },
|
{ cmd_keyword, "sqrt", &program::squareroot, "unarity operator square root" },
|
||||||
{ cmd_keyword, "sq", &program::square, "unarity operator square" },
|
{ cmd_keyword, "sq", &program::square, "(or sqr) unarity operator square" },
|
||||||
|
{ cmd_keyword, "sqr", &program::square, "" },
|
||||||
{ cmd_keyword, "mod", &program::modulo, "binary operator modulo" },
|
{ cmd_keyword, "mod", &program::modulo, "binary operator modulo" },
|
||||||
|
|
||||||
//BINARY
|
//BINARY
|
||||||
|
|
|
@ -77,11 +77,14 @@ void std()
|
||||||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
number::s_default_precision = int(getf());
|
number::s_default_precision = int(getf());
|
||||||
}
|
}
|
||||||
|
|
||||||
number::s_current_precision = number::s_default_precision;
|
number::s_current_precision = number::s_default_precision;
|
||||||
number::s_mode = number::std;
|
number::s_mode = number::std;
|
||||||
|
|
||||||
cout.precision(number::s_current_precision);
|
// format for mpfr_printf
|
||||||
cout.unsetf(ios_base::floatfield);
|
stringstream ss;
|
||||||
|
ss << number::s_current_precision;
|
||||||
|
s_mpfr_printf_format = s_mpfr_printf_format_beg + ss.str() + s_mpfr_printf_format_std;
|
||||||
}
|
}
|
||||||
|
|
||||||
void fix()
|
void fix()
|
||||||
|
@ -92,7 +95,10 @@ void fix()
|
||||||
number::s_current_precision = int(getf());
|
number::s_current_precision = int(getf());
|
||||||
number::s_mode = number::fix;
|
number::s_mode = number::fix;
|
||||||
|
|
||||||
cout << setprecision(number::s_current_precision) << fixed;
|
// format for mpfr_printf
|
||||||
|
stringstream ss;
|
||||||
|
ss << number::s_current_precision;
|
||||||
|
s_mpfr_printf_format = s_mpfr_printf_format_beg + ss.str() + s_mpfr_printf_format_fix;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sci()
|
void sci()
|
||||||
|
@ -103,7 +109,10 @@ void sci()
|
||||||
number::s_current_precision = (int)getf();
|
number::s_current_precision = (int)getf();
|
||||||
number::s_mode = number::sci;
|
number::s_mode = number::sci;
|
||||||
|
|
||||||
cout << setprecision(number::s_current_precision) << scientific;
|
// format for mpfr_printf
|
||||||
|
stringstream ss;
|
||||||
|
ss << number::s_current_precision;
|
||||||
|
s_mpfr_printf_format = s_mpfr_printf_format_beg + ss.str() + s_mpfr_printf_format_sci;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rpn_version()
|
void rpn_version()
|
||||||
|
|
286
src/rpn-real.h
286
src/rpn-real.h
|
@ -2,23 +2,27 @@ void plus()
|
||||||
{
|
{
|
||||||
MIN_ARGUMENTS(2);
|
MIN_ARGUMENTS(2);
|
||||||
|
|
||||||
//TODO
|
// float
|
||||||
//// float
|
if (IS_ARG_TYPE(0, cmd_number))
|
||||||
//if (IS_ARG_TYPE(0, cmd_number))
|
{
|
||||||
//{
|
ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
||||||
// ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
|
||||||
// //TODO really too slow
|
number* right = (number*)_stack->back();
|
||||||
// putf(getf() + getf());
|
_stack->pop_back();
|
||||||
//}
|
number* left = (number*)_stack->back();
|
||||||
//// binary
|
CHECK_MPFR(mpfr_add(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||||
//else if (IS_ARG_TYPE(0, cmd_binary))
|
}
|
||||||
//{
|
// binary
|
||||||
// ARG_MUST_BE_OF_TYPE(1, cmd_binary);
|
else if (IS_ARG_TYPE(0, cmd_binary))
|
||||||
// //TODO really too slow
|
{
|
||||||
// putb(getb() + getb());
|
ARG_MUST_BE_OF_TYPE(1, cmd_binary);
|
||||||
//}
|
|
||||||
|
binary* right = (binary*)_stack->back();
|
||||||
//TODO
|
_stack->pop_back();
|
||||||
|
binary* left = (binary*)_stack->back();
|
||||||
|
left->_value += right->_value;
|
||||||
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
// string
|
// string
|
||||||
else if (IS_ARG_TYPE(0, cmd_string))
|
else if (IS_ARG_TYPE(0, cmd_string))
|
||||||
|
@ -29,177 +33,191 @@ void plus()
|
||||||
*((ostring*)_stack->back())->_value += second;
|
*((ostring*)_stack->back())->_value += second;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
//else
|
else
|
||||||
// ERR_CONTEXT(ret_bad_operand_type);
|
ERR_CONTEXT(ret_bad_operand_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void minus()
|
void minus()
|
||||||
{
|
{
|
||||||
MIN_ARGUMENTS(2);
|
MIN_ARGUMENTS(2);
|
||||||
|
|
||||||
//TODO
|
// float
|
||||||
//// float
|
if (IS_ARG_TYPE(0, cmd_number))
|
||||||
//if (IS_ARG_TYPE(0, cmd_number))
|
{
|
||||||
//{
|
ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
||||||
// ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
|
||||||
// //TODO really too slow
|
number* right = (number*)_stack->back();
|
||||||
// floating_t first = getf();
|
_stack->pop_back();
|
||||||
// putf(getf() - first);
|
number* left = (number*)_stack->back();
|
||||||
//}
|
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))
|
// binary
|
||||||
//{
|
else if (IS_ARG_TYPE(0, cmd_binary))
|
||||||
// ARG_MUST_BE_OF_TYPE(1, cmd_binary);
|
{
|
||||||
// //TODO really too slow
|
ARG_MUST_BE_OF_TYPE(1, cmd_binary);
|
||||||
// integer_t first = getb();
|
|
||||||
// putb(getb() - first);
|
binary* right = (binary*)_stack->back();
|
||||||
//}
|
_stack->pop_back();
|
||||||
//else
|
binary* left = (binary*)_stack->back();
|
||||||
// ERR_CONTEXT(ret_bad_operand_type);
|
left->_value -= right->_value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ERR_CONTEXT(ret_bad_operand_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mul()
|
void mul()
|
||||||
{
|
{
|
||||||
//TODO
|
// float
|
||||||
//// float
|
if (IS_ARG_TYPE(0, cmd_number))
|
||||||
//if (IS_ARG_TYPE(0, cmd_number))
|
{
|
||||||
//{
|
ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
||||||
// ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
|
||||||
// //TODO really too slow
|
number* right = (number*)_stack->back();
|
||||||
// putf(getf() * getf());
|
_stack->pop_back();
|
||||||
//}
|
number* left = (number*)_stack->back();
|
||||||
//// binary
|
CHECK_MPFR(mpfr_mul(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||||
//else if (IS_ARG_TYPE(0, cmd_binary))
|
}
|
||||||
//{
|
// binary
|
||||||
// ARG_MUST_BE_OF_TYPE(1, cmd_binary);
|
else if (IS_ARG_TYPE(0, cmd_binary))
|
||||||
// //TODO really too slow
|
{
|
||||||
// putb(getb() * getb());
|
ARG_MUST_BE_OF_TYPE(1, cmd_binary);
|
||||||
//}
|
|
||||||
//else
|
binary* right = (binary*)_stack->back();
|
||||||
// ERR_CONTEXT(ret_bad_operand_type);
|
_stack->pop_back();
|
||||||
|
binary* left = (binary*)_stack->back();
|
||||||
|
left->_value *= right->_value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ERR_CONTEXT(ret_bad_operand_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void div()
|
void div()
|
||||||
{
|
{
|
||||||
//TODO
|
MIN_ARGUMENTS(2);
|
||||||
//MIN_ARGUMENTS(2);
|
|
||||||
|
|
||||||
//// float
|
// float
|
||||||
//if (IS_ARG_TYPE(0, cmd_number))
|
if (IS_ARG_TYPE(0, cmd_number))
|
||||||
//{
|
{
|
||||||
// ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
||||||
// //TODO really too slow
|
|
||||||
|
|
||||||
// floating_t first = getf();
|
number* right = (number*)_stack->back();
|
||||||
// // arithmetic faults are managed by c++
|
_stack->pop_back();
|
||||||
// putf(getf() / first);
|
number* left = (number*)_stack->back();
|
||||||
//}
|
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))
|
// binary
|
||||||
//{
|
else if (IS_ARG_TYPE(0, cmd_binary))
|
||||||
// ARG_MUST_BE_OF_TYPE(1, cmd_binary);
|
{
|
||||||
// if (((binary*)_stack->get_obj(0))->_value == 0)
|
ARG_MUST_BE_OF_TYPE(1, cmd_binary);
|
||||||
// {
|
if (((binary*)_stack->get_obj(0))->_value == 0)
|
||||||
// ERR_CONTEXT(ret_div_by_zero);
|
{
|
||||||
// }
|
ERR_CONTEXT(ret_div_by_zero);
|
||||||
// else
|
}
|
||||||
// {
|
else
|
||||||
// //TODO really too slow
|
{
|
||||||
// integer_t first = getb();
|
binary* right = (binary*)_stack->back();
|
||||||
// putb(getb() / first);
|
_stack->pop_back();
|
||||||
// }
|
binary* left = (binary*)_stack->back();
|
||||||
//}
|
left->_value /= right->_value;
|
||||||
//else
|
}
|
||||||
// ERR_CONTEXT(ret_bad_operand_type);
|
}
|
||||||
|
else
|
||||||
|
ERR_CONTEXT(ret_bad_operand_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void neg()
|
void neg()
|
||||||
{
|
{
|
||||||
//TODO
|
MIN_ARGUMENTS(1);
|
||||||
//MIN_ARGUMENTS(1);
|
|
||||||
|
|
||||||
//// float
|
// float
|
||||||
//if (IS_ARG_TYPE(0, cmd_number))
|
if (IS_ARG_TYPE(0, cmd_number))
|
||||||
// putf(-getf());
|
{
|
||||||
//// binary
|
number* left = (number*)_stack->back();
|
||||||
//else if (IS_ARG_TYPE(0, cmd_binary))
|
CHECK_MPFR(mpfr_neg(&left->_value.mpfr, &left->_value.mpfr, s_mpfr_rnd));
|
||||||
// putb(-getb());
|
}
|
||||||
//else
|
// binary
|
||||||
// ERR_CONTEXT(ret_bad_operand_type);
|
else if (IS_ARG_TYPE(0, cmd_binary))
|
||||||
|
((binary*)_stack->back())->_value *= -1;
|
||||||
|
else
|
||||||
|
ERR_CONTEXT(ret_bad_operand_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void inv()
|
void inv()
|
||||||
{
|
{
|
||||||
//TODO
|
MIN_ARGUMENTS(1);
|
||||||
//MIN_ARGUMENTS(1);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
|
||||||
|
|
||||||
//// arithmetic faults are managed by c++
|
number* left = (number*)_stack->back();
|
||||||
//putf(1 / getf());
|
CHECK_MPFR(mpfr_si_div(&left->_value.mpfr, 1L, &left->_value.mpfr, s_mpfr_rnd));
|
||||||
}
|
}
|
||||||
|
|
||||||
void purcent()
|
void purcent()
|
||||||
{
|
{
|
||||||
//TODO
|
MIN_ARGUMENTS(2);
|
||||||
//MIN_ARGUMENTS(2);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
|
||||||
|
|
||||||
//putf((getf() * getf()) / 100);
|
number* right = (number*)_stack->back();
|
||||||
|
_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));
|
||||||
}
|
}
|
||||||
|
|
||||||
void purcentCH()
|
void purcentCH()
|
||||||
{
|
{
|
||||||
//TODO
|
MIN_ARGUMENTS(2);
|
||||||
//MIN_ARGUMENTS(2);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
|
||||||
|
|
||||||
//// arithmetic faults are managed by c++
|
number* right = (number*)_stack->back();
|
||||||
//floating_t first = getf();
|
_stack->pop_back();
|
||||||
//putf((100 * first) / getf());
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
void power()
|
void power()
|
||||||
{
|
{
|
||||||
//TODO
|
MIN_ARGUMENTS(2);
|
||||||
//MIN_ARGUMENTS(2);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
|
||||||
|
|
||||||
//// arithmetic faults are managed by c++
|
number* right = (number*)_stack->back();
|
||||||
//floating_t first = getf();
|
_stack->pop_back();
|
||||||
//putf(powl(getf(), first));
|
number* left = (number*)_stack->back();
|
||||||
|
CHECK_MPFR(mpfr_pow(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||||
}
|
}
|
||||||
|
|
||||||
void squareroot()
|
void squareroot()
|
||||||
{
|
{
|
||||||
//TODO
|
MIN_ARGUMENTS(1);
|
||||||
//MIN_ARGUMENTS(1);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
|
||||||
|
|
||||||
//// arithmetic faults are managed by c++
|
number* left = (number*)_stack->back();
|
||||||
//putf(sqrtl(getf()));
|
CHECK_MPFR(mpfr_sqrt(&left->_value.mpfr, &left->_value.mpfr, s_mpfr_rnd));
|
||||||
}
|
}
|
||||||
|
|
||||||
void square()
|
void square()
|
||||||
{
|
{
|
||||||
//TODO
|
MIN_ARGUMENTS(1);
|
||||||
//MIN_ARGUMENTS(1);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
|
||||||
|
|
||||||
//floating_t first = getf();
|
number* left = (number*)_stack->back();
|
||||||
//putf(first * first);
|
CHECK_MPFR(mpfr_sqr(&left->_value.mpfr, &left->_value.mpfr, s_mpfr_rnd));
|
||||||
}
|
}
|
||||||
|
|
||||||
void modulo()
|
void modulo()
|
||||||
{
|
{
|
||||||
//TODO
|
MIN_ARGUMENTS(2);
|
||||||
//MIN_ARGUMENTS(2);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
||||||
//ARG_MUST_BE_OF_TYPE(1, cmd_number);
|
|
||||||
|
|
||||||
//// arithmetic faults are managed by c++
|
number* right = (number*)_stack->back();
|
||||||
//floating_t first = getf();
|
_stack->pop_back();
|
||||||
//putf(fmodl(getf(), first));
|
number* left = (number*)_stack->back();
|
||||||
|
|
||||||
|
CHECK_MPFR(mpfr_fmod(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
|
||||||
}
|
}
|
||||||
|
|
105
src/rpn.cpp
105
src/rpn.cpp
|
@ -38,13 +38,23 @@ using namespace std;
|
||||||
|
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
|
// default number of printed digitis
|
||||||
|
#define DEFAULT_PRECISION 12
|
||||||
|
|
||||||
// MPFR related constants
|
// MPFR related constants
|
||||||
// 128 bits significand storing length in byters, result of mpfr_custom_get_size (128)
|
// 128 bits significand storing length in byters, result of mpfr_custom_get_size(128)
|
||||||
#define MPFR_DEF_FORMAT "%Rf"
|
|
||||||
#define MPFR_DEF_RND MPFR_RNDN
|
#define MPFR_DEF_RND MPFR_RNDN
|
||||||
#define MPFR_128BITS_PREC 128
|
#define MPFR_128BITS_PREC 128
|
||||||
#define MPFR_128BITS_STORING_LENGTH 16
|
#define MPFR_128BITS_STORING_LENGTH 16
|
||||||
|
|
||||||
|
static string s_mpfr_printf_format_beg = "%.";
|
||||||
|
static string s_mpfr_printf_format_std = "Rg";
|
||||||
|
static string s_mpfr_printf_format_fix = "Rf";
|
||||||
|
static string s_mpfr_printf_format_sci = "Re";
|
||||||
|
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 int g_verbose = 0;
|
static int g_verbose = 0;
|
||||||
|
|
||||||
|
@ -98,19 +108,34 @@ struct floating_t
|
||||||
floating_t()
|
floating_t()
|
||||||
{
|
{
|
||||||
mpfr_custom_init(significand, MPFR_128BITS_STORING_LENGTH);
|
mpfr_custom_init(significand, MPFR_128BITS_STORING_LENGTH);
|
||||||
mpfr_custom_init_set(&mpfr, MPFR_NAN_KIND, 0, MPFR_128BITS_PREC, significand);
|
mpfr_custom_init_set(&mpfr, MPFR_NAN_KIND, 0, s_mpfr_prec, significand);
|
||||||
}
|
}
|
||||||
|
|
||||||
floating_t operator=(const long int val)
|
floating_t operator=(const long int val)
|
||||||
{
|
{
|
||||||
mpfr_custom_init(significand, MPFR_128BITS_STORING_LENGTH);
|
mpfr_custom_init(significand, MPFR_128BITS_STORING_LENGTH);
|
||||||
mpfr_custom_init_set(&mpfr, MPFR_ZERO_KIND, 0, MPFR_128BITS_PREC, significand);
|
mpfr_custom_init_set(&mpfr, MPFR_ZERO_KIND, 0, s_mpfr_prec, significand);
|
||||||
mpfr_set_si(&mpfr, val, MPFR_DEF_RND);
|
mpfr_set_si(&mpfr, val, s_mpfr_rnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
operator int()
|
operator int()
|
||||||
{
|
{
|
||||||
return (int)mpfr_get_si(&mpfr, MPFR_DEF_RND);
|
return (int)mpfr_get_si(&mpfr, s_mpfr_rnd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ensure_significand()
|
||||||
|
{
|
||||||
|
mpfr._mpfr_d = (mp_limb_t*)significand;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator>(const floating_t right)
|
||||||
|
{
|
||||||
|
return mpfr_cmp(&mpfr, &right.mpfr) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator<(const floating_t right)
|
||||||
|
{
|
||||||
|
return mpfr_cmp(&mpfr, &right.mpfr) < 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -155,6 +180,11 @@ struct number : public object
|
||||||
_value = (long)value;
|
_value = (long)value;
|
||||||
}
|
}
|
||||||
unsigned int size() { return (unsigned int)sizeof(floating_t); }
|
unsigned int size() { return (unsigned int)sizeof(floating_t); }
|
||||||
|
|
||||||
|
void ensure_significand()
|
||||||
|
{
|
||||||
|
_value.mpfr._mpfr_d = (mp_limb_t*)_value.significand;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
number operator=(const number& op)
|
number operator=(const number& op)
|
||||||
|
@ -180,7 +210,7 @@ struct number : public object
|
||||||
|
|
||||||
number::mode_enum number::s_default_mode = number::std;
|
number::mode_enum number::s_default_mode = number::std;
|
||||||
number::mode_enum number::s_mode = number::s_default_mode;
|
number::mode_enum number::s_mode = number::s_default_mode;
|
||||||
int number::s_default_precision = 12;
|
int number::s_default_precision = DEFAULT_PRECISION;
|
||||||
int number::s_current_precision = number::s_default_precision;
|
int number::s_current_precision = number::s_default_precision;
|
||||||
|
|
||||||
struct binary : public object
|
struct binary : public object
|
||||||
|
@ -333,6 +363,7 @@ struct branch : public object
|
||||||
branch_fn_t _fn;
|
branch_fn_t _fn;
|
||||||
// args used by cmd_branch cmds
|
// args used by cmd_branch cmds
|
||||||
int arg1, arg2, arg3;
|
int arg1, arg2, arg3;
|
||||||
|
//TODO change to int
|
||||||
floating_t farg1, farg2;
|
floating_t farg1, farg2;
|
||||||
bool arg_bool;
|
bool arg_bool;
|
||||||
unsigned int _len;
|
unsigned int _len;
|
||||||
|
@ -344,36 +375,31 @@ void object::show(ostream& stream)
|
||||||
switch(_type)
|
switch(_type)
|
||||||
{
|
{
|
||||||
case cmd_number:
|
case cmd_number:
|
||||||
//TODO
|
((number*)this)->ensure_significand();
|
||||||
//stream << ((number*)this)->_value;
|
(void)mpfr_printf(s_mpfr_printf_format.c_str(), &((number*)this)->_value.mpfr);
|
||||||
stream<<"number._value.mpfr="<<&((number*)this)->_value.mpfr<<endl;
|
|
||||||
stream<<"number._value.mpfr._mpfr_d="<<(void*)((number*)this)->_value.mpfr._mpfr_d<<endl;
|
|
||||||
stream<<"number._value.significand="<<(void*)((number*)this)->_value.significand<<endl;
|
|
||||||
(void)mpfr_printf(MPFR_DEF_FORMAT, &((number*)this)->_value.mpfr);
|
|
||||||
break;
|
break;
|
||||||
case cmd_binary:
|
case cmd_binary:
|
||||||
{
|
{
|
||||||
//TODO
|
cout << "# ";
|
||||||
//cout << "# ";
|
switch(((binary*)this)->s_mode)
|
||||||
//switch(((binary*)this)->s_mode)
|
{
|
||||||
//{
|
case binary::dec: stream<<std::right<<std::setw(8)<<std::dec<<((binary*)this)->_value<<" d"; break;
|
||||||
// case binary::dec: stream<<std::right<<std::setw(8)<<std::dec<<((binary*)this)->_value<<" d"; break;
|
case binary::hex: stream<<std::right<<std::setw(8)<<std::hex<<((binary*)this)->_value<<" h"; break;
|
||||||
// case binary::hex: stream<<std::right<<std::setw(8)<<std::hex<<((binary*)this)->_value<<" h"; break;
|
case binary::oct: stream<<std::right<<std::setw(8)<<std::oct<<((binary*)this)->_value<<" o"; break;
|
||||||
// case binary::oct: stream<<std::right<<std::setw(8)<<std::oct<<((binary*)this)->_value<<" o"; break;
|
case binary::bin:
|
||||||
// case binary::bin:
|
{
|
||||||
// {
|
string mybin;
|
||||||
// string mybin;
|
for (int i = (int)(log((double)((binary*)this)->_value) / log(2.)); i>=0; i--)
|
||||||
// for (int i = (int)(log((floating_t)((binary*)this)->_value) / log(2.)); i>=0; i--)
|
{
|
||||||
// {
|
if (((binary*)this)->_value & (1 << i))
|
||||||
// if (((binary*)this)->_value & (1 << i))
|
mybin+='1';
|
||||||
// mybin+='1';
|
else
|
||||||
// else
|
mybin+='0';
|
||||||
// mybin+='0';
|
}
|
||||||
// }
|
stream<<std::right<<std::setw(20)<<std::oct<<mybin<<" b";
|
||||||
// stream<<std::right<<std::setw(20)<<std::oct<<mybin<<" b";
|
}
|
||||||
// }
|
break;
|
||||||
// break;
|
}
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case cmd_string:
|
case cmd_string:
|
||||||
|
@ -500,6 +526,14 @@ public:
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
stk.push_back(seq_obj(i), seq_len(i), type);
|
stk.push_back(seq_obj(i), seq_len(i), type);
|
||||||
|
|
||||||
|
// numbers: ensure that significand is correctly recorded by object
|
||||||
|
if (type == cmd_number)
|
||||||
|
{
|
||||||
|
number* k = (number*)stk.back();
|
||||||
|
k->ensure_significand();
|
||||||
|
}
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -796,6 +830,8 @@ 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(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 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))
|
#define IS_ARG_TYPE(num, type) (_stack->get_type(num) == (type))
|
||||||
|
//TODO
|
||||||
|
#define CHECK_MPFR(op) do { (void)(op); } while(0)
|
||||||
|
|
||||||
// keywords implementation
|
// keywords implementation
|
||||||
#include "rpn-general.h"
|
#include "rpn-general.h"
|
||||||
|
@ -820,7 +856,6 @@ private:
|
||||||
static void apply_default(void)
|
static void apply_default(void)
|
||||||
{
|
{
|
||||||
//default precision
|
//default precision
|
||||||
cout << setprecision(number::s_default_precision);
|
|
||||||
number::s_mode = number::s_default_mode;
|
number::s_mode = number::s_default_mode;
|
||||||
|
|
||||||
//default binary mode
|
//default binary mode
|
||||||
|
|
|
@ -44,7 +44,6 @@ public:
|
||||||
_base = (char*)realloc(_base, _total_size);
|
_base = (char*)realloc(_base, _total_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
cout<<"current = "<<(void*)_current<<", index is "<<_count<<endl;
|
|
||||||
if (!dont_copy)
|
if (!dont_copy)
|
||||||
memcpy(_current, obj, size);
|
memcpy(_current, obj, size);
|
||||||
_vlen.push_back(size);
|
_vlen.push_back(size);
|
||||||
|
|
Loading…
Reference in a new issue