mirror of
https://github.com/louisrubet/rpn
synced 2024-11-17 07:47:50 +01:00
#141: complex sin cos tan
This commit is contained in:
parent
3d21b1e5db
commit
c319da4902
2 changed files with 159 additions and 9 deletions
107
src/rpn-trig.hpp
107
src/rpn-trig.hpp
|
@ -38,10 +38,36 @@ void r2d(void)
|
|||
void rpn_sin(void)
|
||||
{
|
||||
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, floating_t::s_mpfr_rnd));
|
||||
if (_stack->get_type(0) == cmd_number)
|
||||
{
|
||||
floating_t* left = &((number*)_stack->get_obj(0))->_value;
|
||||
CHECK_MPFR(mpfr_sin(left->mpfr, left->mpfr, floating_t::s_mpfr_rnd));
|
||||
}
|
||||
else if (_stack->get_type(0) == cmd_complex)
|
||||
{
|
||||
// sin(x+iy)=sin(x)cosh(y)+icos(x)sinh(y)
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-1, _calc_stack);
|
||||
|
||||
floating_t* tmp = &((number*)_calc_stack.allocate_back(number::calc_size(), cmd_number))->_value;
|
||||
floating_t* x = ((complex*)_calc_stack.get_obj(1))->re();
|
||||
floating_t* y = ((complex*)_calc_stack.get_obj(1))->im();
|
||||
|
||||
floating_t* re = ((complex*)_stack->get_obj(0))->re();
|
||||
floating_t* im = ((complex*)_stack->get_obj(0))->im();
|
||||
|
||||
CHECK_MPFR(mpfr_sin(re->mpfr, x->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_cosh(tmp->mpfr, y->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_mul(re->mpfr, re->mpfr, tmp->mpfr, floating_t::s_mpfr_rnd));
|
||||
|
||||
CHECK_MPFR(mpfr_cos(im->mpfr, x->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_sinh(tmp->mpfr, y->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_mul(im->mpfr, im->mpfr, tmp->mpfr, floating_t::s_mpfr_rnd));
|
||||
|
||||
_calc_stack.pop_back(2);
|
||||
}
|
||||
else
|
||||
ERR_CONTEXT(ret_bad_operand_type);
|
||||
}
|
||||
|
||||
void rpn_asin(void)
|
||||
|
@ -56,10 +82,37 @@ void rpn_asin(void)
|
|||
void rpn_cos(void)
|
||||
{
|
||||
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, floating_t::s_mpfr_rnd));
|
||||
if (_stack->get_type(0) == cmd_number)
|
||||
{
|
||||
floating_t* left = &((number*)_stack->get_obj(0))->_value;
|
||||
CHECK_MPFR(mpfr_cos(left->mpfr, left->mpfr, floating_t::s_mpfr_rnd));
|
||||
}
|
||||
else if (_stack->get_type(0) == cmd_complex)
|
||||
{
|
||||
// cos(x+iy) = cos(x)cosh(y) - isin(x)sinh(y)
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-1, _calc_stack);
|
||||
|
||||
floating_t* tmp = &((number*)_calc_stack.allocate_back(number::calc_size(), cmd_number))->_value;
|
||||
floating_t* x = ((complex*)_calc_stack.get_obj(1))->re();
|
||||
floating_t* y = ((complex*)_calc_stack.get_obj(1))->im();
|
||||
|
||||
floating_t* re = ((complex*)_stack->get_obj(0))->re();
|
||||
floating_t* im = ((complex*)_stack->get_obj(0))->im();
|
||||
|
||||
CHECK_MPFR(mpfr_cos(re->mpfr, x->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_cosh(tmp->mpfr, y->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_mul(re->mpfr, re->mpfr, tmp->mpfr, floating_t::s_mpfr_rnd));
|
||||
|
||||
CHECK_MPFR(mpfr_sin(im->mpfr, x->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_sinh(tmp->mpfr, y->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_mul(im->mpfr, im->mpfr, tmp->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_neg(im->mpfr, im->mpfr, floating_t::s_mpfr_rnd));
|
||||
|
||||
_calc_stack.pop_back(2);
|
||||
}
|
||||
else
|
||||
ERR_CONTEXT(ret_bad_operand_type);
|
||||
}
|
||||
|
||||
void rpn_acos(void)
|
||||
|
@ -74,10 +127,46 @@ void rpn_acos(void)
|
|||
void rpn_tan(void)
|
||||
{
|
||||
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, floating_t::s_mpfr_rnd));
|
||||
if (_stack->get_type(0) == cmd_number)
|
||||
{
|
||||
floating_t* left = &((number*)_stack->get_obj(0))->_value;
|
||||
CHECK_MPFR(mpfr_tan(left->mpfr, left->mpfr, floating_t::s_mpfr_rnd));
|
||||
}
|
||||
else if (_stack->get_type(0) == cmd_complex)
|
||||
{
|
||||
// tan(x+iy) = (sin(2x)+isinh(2y)) / cosh(2y)+cos(2x)
|
||||
stack::copy_and_push_back(*_stack, _stack->size()-1, _calc_stack);
|
||||
|
||||
floating_t* tmp = &((number*)_calc_stack.allocate_back(number::calc_size(), cmd_number))->_value;
|
||||
floating_t* x = ((complex*)_calc_stack.get_obj(1))->re();
|
||||
floating_t* y = ((complex*)_calc_stack.get_obj(1))->im();
|
||||
|
||||
floating_t* re = ((complex*)_stack->get_obj(0))->re();
|
||||
floating_t* im = ((complex*)_stack->get_obj(0))->im();
|
||||
|
||||
//x->2x
|
||||
CHECK_MPFR(mpfr_mul_si(x->mpfr, x->mpfr, 2, floating_t::s_mpfr_rnd));
|
||||
//y->2y
|
||||
CHECK_MPFR(mpfr_mul_si(y->mpfr, y->mpfr, 2, floating_t::s_mpfr_rnd));
|
||||
|
||||
//sin(2x)+sinh(2y)
|
||||
CHECK_MPFR(mpfr_sin(re->mpfr, x->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_sinh(im->mpfr, y->mpfr, floating_t::s_mpfr_rnd));
|
||||
|
||||
//cosh(2y)+cos(2x)
|
||||
CHECK_MPFR(mpfr_cosh(tmp->mpfr, y->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_cos(x->mpfr, x->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_add(tmp->mpfr, tmp->mpfr, x->mpfr, floating_t::s_mpfr_rnd));
|
||||
|
||||
//sin(2x)+sinh(2y) / (cosh(2y)+cos(2x))
|
||||
CHECK_MPFR(mpfr_div(re->mpfr, re->mpfr, tmp->mpfr, floating_t::s_mpfr_rnd));
|
||||
CHECK_MPFR(mpfr_div(im->mpfr, im->mpfr, tmp->mpfr, floating_t::s_mpfr_rnd));
|
||||
|
||||
_calc_stack.pop_back(2);
|
||||
}
|
||||
else
|
||||
ERR_CONTEXT(ret_bad_operand_type);
|
||||
}
|
||||
|
||||
void rpn_atan(void)
|
||||
|
|
|
@ -315,3 +315,64 @@ erase
|
|||
(3,4) sqrt
|
||||
-> stack should be (2,1)
|
||||
erase
|
||||
|
||||
# sin (1)
|
||||
(1,2) sin
|
||||
-> stack should be (3.1657785132161681467,1.9596010414216058971)
|
||||
erase
|
||||
|
||||
# sin (2)
|
||||
(1,-2) sin
|
||||
-> stack should be (3.1657785132161681467,-1.9596010414216058971)
|
||||
erase
|
||||
|
||||
# sin (3)
|
||||
(-1,-2) sin
|
||||
-> stack should be (-3.1657785132161681467,-1.9596010414216058971)
|
||||
erase
|
||||
|
||||
# sin (4)
|
||||
(-1,2) sin
|
||||
-> stack should be (-3.1657785132161681467,1.9596010414216058971)
|
||||
erase
|
||||
|
||||
# cos (1)
|
||||
(1,2) cos
|
||||
-> stack should be (2.0327230070196655294,-3.0518977991518000575)
|
||||
erase
|
||||
|
||||
|
||||
# cos (2)
|
||||
(1,-2) cos
|
||||
-> stack should be (2.0327230070196655294,3.0518977991518000575)
|
||||
erase
|
||||
|
||||
# cos (3)
|
||||
(-1,-2) cos
|
||||
-> stack should be (2.0327230070196655294,-3.0518977991518000575)
|
||||
erase
|
||||
|
||||
# cos (4)
|
||||
(-1,2) sin
|
||||
-> stack should be (-3.1657785132161681467,1.9596010414216058971)
|
||||
erase
|
||||
|
||||
# tan (1)
|
||||
(1,2) tan
|
||||
-> stack should be (0.033812826079896690284,1.0147936161466335681)
|
||||
erase
|
||||
|
||||
# tan (2)
|
||||
(1,-2) tan
|
||||
-> stack should be (0.033812826079896690284,-1.0147936161466335681)
|
||||
erase
|
||||
|
||||
# tan (3)
|
||||
(-1,-2) tan
|
||||
-> stack should be (-0.033812826079896690284,-1.0147936161466335681)
|
||||
erase
|
||||
|
||||
# tan (4)
|
||||
(-1,2) tan
|
||||
-> stack should be (-0.033812826079896690284,1.0147936161466335681)
|
||||
erase
|
||||
|
|
Loading…
Reference in a new issue