#180: corrected bug in rpn_power

This commit is contained in:
Louis Rubet 2017-07-01 17:32:19 +02:00
parent a7f2288bf3
commit 06ef6e44ff
3 changed files with 54 additions and 21 deletions

View file

@ -6,6 +6,36 @@
- brings powerfull calculation facilities on floating point numbers with __arbitrary precision__ provided by **GNU MP** and **GNU MPFR** libraries
- uses that so cool **reverse polish notation**
## Doc overview
This page gives some examples and lists the commands currently implemented.
For a most complete help, please refer to HP28S and HP48GX manuals provided in the sources
An help command is provided by rpn:
```
rpn> help
rpn> h
rpn v2.3, (c) 2017 <louis@rubet.fr>, GNU LGPL v3
Reverse Polish Notation language
using GMP v6.1.2 under GNU LGPL
MPFR v3.1.5-p8 under GNU LGPL
and linenoise-ng v1.0.0 under BSD
Syntax: rpn [command]
with optional command = list of commands
GENERAL
nop no operation
help this help message
(...)
```
## Quick examples
### easy calculation with **stacked results**

View file

@ -1,6 +1,6 @@
# **rpn** - **R**everse **P**olish **N**otation language [![License: LGPLv3](https://www.gnu.org/graphics/lgplv3-88x31.png)](https://www.gnu.org/licenses/lgpl-3.0.en.html)
### a lisp-based math language using polish notation
### a math language using reverse polish notation
```
rpn> 1 2 + 2 sqrt
@ -8,7 +8,7 @@ rpn> 1 2 + 2 sqrt
1> 1.4142135623730950488
```
### with arbitrary precision
### arbitrary precision provided by GNU MPFR
```
rpn> 256 prec
rpn> pi
@ -16,7 +16,7 @@ rpn> pi
rpn>
```
### providing variables, programs
### variables, programs
```
rpn> << rot * swap 2 / chs dup sq rot - sqrt >> 'quad' sto
rpn> << -> x y << x y + ln >> >> 'P' sto

View file

@ -336,31 +336,34 @@ void rpn_power()
}
// carrefull, no 'else' here
if (!done_on_real && _stack->get_type(1) == cmd_complex)
if (!done_on_real)
{
ARG_MUST_BE_OF_TYPE(0, cmd_number);
if (_stack->get_type(1) == cmd_complex)
{
ARG_MUST_BE_OF_TYPE(0, cmd_number);
//power on tmp stack
stack::copy_and_push_back(*_stack, _stack->size()-1, _calc_stack);
_stack->pop_back();
//power on tmp stack
stack::copy_and_push_back(*_stack, _stack->size()-1, _calc_stack);
_stack->pop_back();
//switch complex to polar
complex* cplx = (complex*)_stack->back();
rpn_r2p();
//switch complex to polar
complex* cplx = (complex*)_stack->back();
rpn_r2p();
//new abs=abs^exponent
number* exponent = (number*)_calc_stack.back();
CHECK_MPFR(mpfr_pow(cplx->re()->mpfr, cplx->re()->mpfr, exponent->_value.mpfr, floating_t::s_mpfr_rnd));
//new abs=abs^exponent
number* exponent = (number*)_calc_stack.back();
CHECK_MPFR(mpfr_pow(cplx->re()->mpfr, cplx->re()->mpfr, exponent->_value.mpfr, floating_t::s_mpfr_rnd));
//new arg=arg*exponent
CHECK_MPFR(mpfr_mul(cplx->im()->mpfr, cplx->im()->mpfr, exponent->_value.mpfr, floating_t::s_mpfr_rnd));
//new arg=arg*exponent
CHECK_MPFR(mpfr_mul(cplx->im()->mpfr, cplx->im()->mpfr, exponent->_value.mpfr, floating_t::s_mpfr_rnd));
//back to cartesian
rpn_p2r();
_calc_stack.pop_back();
//back to cartesian
rpn_p2r();
_calc_stack.pop_back();
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void rpn_squareroot()