#133: added complex r->c c->r

This commit is contained in:
Louis Rubet 2017-06-16 19:13:57 +02:00
parent e5341f5d8e
commit d8a639773d
4 changed files with 63 additions and 1 deletions

View file

@ -39,7 +39,7 @@ Precision can be really high, up to 0x7FFFFFFFFFFFFFFF bits with GNU MPFR
```
rpn> 256 prec 200 std
rpn> pi 3 * 4 / cos
-0.70710678118654752440084436210484903928483593768847403658833986899536623923105962592591087473479525356117497671223960240783675485777817360566073272153486395308799122357513534343724299243077135552002446
-0.707106781186547524400844362104849039284835937688474036588339868995366239(...)
rpn>
```

View file

@ -62,6 +62,8 @@ program::keyword_t program::s_keywords[] =
{ cmd_keyword, "im", &program::im, "complex imaginary part" },
{ cmd_keyword, "conj", &program::conj, "complex conjugate" },
{ cmd_keyword, "arg", &program::arg, "complex argument in radians" },
{ cmd_keyword, "c->r", &program::c2r, "transform a complex in 2 reals" },
{ cmd_keyword, "r->c", &program::r2c, "transform 2 reals in a complex" },
//TEST
{ cmd_undef, "", NULL, "\nTEST"},

View file

@ -47,3 +47,37 @@ void conj()
complex* cplx = (complex*)_stack->back();
CHECK_MPFR(mpfr_neg(cplx->im()->mpfr, cplx->im()->mpfr, floating_t::s_mpfr_rnd));
}
void r2c()
{
MIN_ARGUMENTS(2);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number);
stack::copy_and_push_back(*_stack, _stack->size()-2, _branch_stack);
stack::copy_and_push_back(*_stack, _stack->size()-1, _branch_stack);
_stack->pop_back();
_stack->pop_back();
complex* cplx = (complex*)_stack->allocate_back(complex::calc_size(), cmd_complex);
CHECK_MPFR(mpfr_set(cplx->re()->mpfr, ((number*)_branch_stack.get_obj(1))->_value.mpfr, floating_t::s_mpfr_rnd));
CHECK_MPFR(mpfr_set(cplx->im()->mpfr, ((number*)_branch_stack.get_obj(0))->_value.mpfr, floating_t::s_mpfr_rnd));
_branch_stack.pop_back();
_branch_stack.pop_back();
}
void c2r()
{
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_complex);
stack::copy_and_push_back(*_stack, _stack->size()-1, _branch_stack);
_stack->pop_back();
number* re = (number*)_stack->allocate_back(number::calc_size(), cmd_number);
number* im = (number*)_stack->allocate_back(number::calc_size(), cmd_number);
CHECK_MPFR(mpfr_set(re->_value.mpfr, ((complex*)_branch_stack.back())->re()->mpfr, floating_t::s_mpfr_rnd));
CHECK_MPFR(mpfr_set(im->_value.mpfr, ((complex*)_branch_stack.back())->im()->mpfr, floating_t::s_mpfr_rnd));
_branch_stack.pop_back();
}

View file

@ -230,3 +230,29 @@ drop
# conj
(1,2) conj dup conj
-> stack should be (1,-2), (1,2)
erase
# r->c (1)
1 2 r->c
-> stack should be (1,2)
erase
# r->c (2)
1 r->c
-> error should be 2
drop
# r->c (3)
r->c
-> error should be 2
drop
# c->r (1)
(1,2) c->r
-> stack should be 1, 2
drop
# c->r (2)
c->r
-> error should be 3
drop