#133: added complex re

This commit is contained in:
Louis Rubet 2017-06-15 23:30:20 +02:00
parent c77599f2d0
commit 4e655b3948
5 changed files with 57 additions and 14 deletions

View file

@ -84,7 +84,7 @@ rpn>
|type | show type of stack first entry
|default | set float representation and precision to default
### real
### real and complex
|keyword|description|
|-|-|
@ -94,24 +94,36 @@ rpn>
|*| multiplication
|/| division
|inv| inverse
|%| purcent
|%|CH inverse purcent
|^| (or pow) power
|sqrt| square root
|sq| (or sqr) square
|mod| modulo
|abs| absolute value
|abs| absolute value for a number or sqrt(re*re+im*im) for a complex
|dec| decimal representation
|hex| hexadecimal representation
|prec| get float precision in bits when first stack is not a number, set float precision in bits when first stack entry is a number. ex: ```256 prec```
|round| set float rounding mode. Authoerized values are: ```["nearest", "toward zero", "toward +inf", "toward -inf", "away from zero"] round```. ex: ```"nearest" round```
|fact| n! for integer n or Gamma(x+1) for fractional x
|sign| 1 if number at stack level 1 is > 0, 0 if == 0, -1 if <= 0
### real
|%| purcent
|%|CH inverse purcent
|mod| modulo
|fact| n! for integer n or Gamma(x+1) for fractional x
|mant| mantissa of a real number
|xpon| exponant of a real number
|min| min of 2 real numbers
|max| max of 2 real numbers
### complex
|re| complex real part
|im| complex imaginary part
|conj| complex conjugate
|arg| complex argument in radians
|r->p| rectangular to polar coordinates
|p->r| polar to rectangular coordinates
|r->c| transform 2 reals in a complex
|c->r| transform a complex in 2 reals
### mode
|keyword|description|

View file

@ -27,8 +27,8 @@ program::keyword_t program::s_keywords[] =
{ cmd_keyword, "fix", &program::fix, "fixed point representation. ex: 6 fix" },
{ cmd_keyword, "sci", &program::sci, "scientific floating point representation. ex: 20 sci" },
//REAL
{ cmd_undef, "", NULL, "\nREAL"},
//REAL AND COMPLEX
{ cmd_undef, "", NULL, "\nREAL AND COMPLEX"},
{ cmd_keyword, "+", &program::plus, "addition" },
{ cmd_keyword, "-", &program::minus, "substraction" },
{ cmd_keyword, "chs", &program::neg , "(or neg) negation" },
@ -36,27 +36,30 @@ program::keyword_t program::s_keywords[] =
{ cmd_keyword, "*", &program::mul, "multiplication" },
{ cmd_keyword, "/", &program::div, "division" },
{ cmd_keyword, "inv", &program::inv, "inverse" },
{ cmd_keyword, "%", &program::purcent, "purcent" },
{ cmd_keyword, "%CH", &program::purcentCH, "inverse purcent" },
{ cmd_keyword, "^", &program::power, "(or pow) power" },
{ cmd_keyword, "pow", &program::power, "" },
{ cmd_keyword, "sqrt", &program::squareroot, "square root" },
{ cmd_keyword, "sq", &program::square, "(or sqr) square" },
{ cmd_keyword, "sqr", &program::square, "" },
{ cmd_keyword, "mod", &program::modulo, "modulo" },
{ cmd_keyword, "abs", &program::rpn_abs, "absolute value" },
{ cmd_keyword, "dec", &program::dec, "decimal representation" },
{ cmd_keyword, "hex", &program::hex, "hexadecimal representation" },
{ cmd_keyword, "prec", &program::precision, "get float precision in bits when first stack is not a number\n\t"
"set float precision in bits when first stack entry is a number. ex: 256 prec" },
{ cmd_keyword, "round", &program::round, "set float rounding mode.\n\tex: [\"nearest\", \"toward zero\", \"toward +inf\", \"toward -inf\", \"away from zero\"] round" },
{ cmd_keyword, "fact", &program::fact, "n! for integer n or Gamma(x+1) for fractional x" },
{ cmd_keyword, "sign", &program::sign, "1 if number at stack level 1 is > 0, 0 if == 0, -1 if <= 0" },
//REAL
{ cmd_keyword, "%", &program::purcent, "purcent" },
{ cmd_keyword, "%CH", &program::purcentCH, "inverse purcent" },
{ cmd_keyword, "mod", &program::modulo, "modulo" },
{ cmd_keyword, "fact", &program::fact, "n! for integer n or Gamma(x+1) for fractional x" },
{ cmd_keyword, "mant", &program::mant, "mantissa of a real number" },
{ cmd_keyword, "xpon", &program::xpon, "exponant of a real number" },
{ cmd_keyword, "min", &program::rpn_min, "min of 2 real numbers" },
{ cmd_keyword, "max", &program::rpn_max, "max of 2 real numbers" },
//COMPLEX
{ cmd_keyword, "re", &program::re, "complex real part" },
//TEST
{ cmd_undef, "", NULL, "\nTEST"},
{ cmd_keyword, ">", &program::sup, "binary operator >" },

View file

@ -572,6 +572,7 @@ private:
// keywords implementation
#include "rpn-general.hpp"
#include "rpn-real.hpp"
#include "rpn-complex.hpp"
#include "rpn-test.hpp"
#include "rpn-stack.hpp"
#include "rpn-string.hpp"

12
src/rpn-complex.hpp Normal file
View file

@ -0,0 +1,12 @@
void re()
{
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);
CHECK_MPFR(mpfr_set(re->_value.mpfr, ((number*)_branch_stack.get_obj(0))->_value.mpfr, floating_t::s_mpfr_rnd));
_branch_stack.pop_back();
}

View file

@ -106,3 +106,18 @@ drop
2 (3,4) /
-> stack should be (0.24,-0.32)
drop
# re (1)
(1.2,3.4) re
-> stack should be 1.2
drop
# re (2)
3 re
-> error should be 3
drop
# re (3)
re
-> error should be 2
drop