#47: hex, dec, bin are now per real

This commit is contained in:
Louis Rubet 2017-05-02 17:30:34 +02:00
parent f6c18b40a0
commit e935c5356a
3 changed files with 38 additions and 7 deletions

View file

@ -33,9 +33,9 @@ program::keyword_t program::_keywords[] =
//REAL representation
{ cmd_undef, "", NULL, "\nREAL REPRESENTATION"},
//{ cmd_keyword, "dec", &program::dec, "decimal representation" },
//{ cmd_keyword, "hex", &program::hex, "hexadecimal representation" },
//{ cmd_keyword, "bin", &program::bin, "binary representation" },
{ cmd_keyword, "dec", &program::dec, "decimal representation" },
{ cmd_keyword, "hex", &program::hex, "hexadecimal representation" },
{ cmd_keyword, "bin", &program::bin, "binary representation" },
{ cmd_keyword, "std", &program::std, "standard floating numbers representation. ex: [25] std" },
{ cmd_keyword, "fix", &program::fix, "fixed point representation. ex: 6 fix" },
{ cmd_keyword, "sci", &program::sci, "scientific floating point representation. ex: 20 sci" },

View file

@ -126,3 +126,24 @@ void modulo()
CHECK_MPFR(mpfr_fmod(left->_value.mpfr, left->_value.mpfr, right->_value.mpfr, s_mpfr_rnd));
}
void hex()
{
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
((number*)_stack->back())->_representation = number::hex;
}
void bin()
{
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
((number*)_stack->back())->_representation = number::bin;
}
void dec()
{
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number);
((number*)_stack->back())->_representation = number::dec;
}

View file

@ -48,12 +48,11 @@ using namespace std;
#define MPFR_128BITS_PREC 128
#define MPFR_128BITS_STORING_LENGTH 16
static string s_mpfr_printf_format_hex = "0x";
static string s_mpfr_printf_format_bin = "0b";
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_hex = "%Ra";
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;
@ -373,8 +372,19 @@ void object::show(ostream& stream)
switch(_type)
{
case cmd_number:
(void)mpfr_sprintf(buffer, s_mpfr_printf_format.c_str(), ((number*)this)->_value.mpfr);
stream<<buffer;
switch(((number*)this)->_representation)
{
case number::dec:
(void)mpfr_sprintf(buffer, s_mpfr_printf_format.c_str(), ((number*)this)->_value.mpfr);
stream<<buffer;
break;
case number::hex:
(void)mpfr_sprintf(buffer, s_mpfr_printf_format_hex.c_str(), ((number*)this)->_value.mpfr);
stream<<buffer;
break;
case number::bin:
cout<<"<binary representation TODO>";
}
break;
case cmd_string:
stream << "\"" << ((ostring*)this)->_value << "\"";