#64: added constant.h

This commit is contained in:
Louis Rubet 2017-05-23 14:47:30 +02:00
parent a05de24703
commit 30aba89b5e
3 changed files with 49 additions and 32 deletions

35
src/constant.h Normal file
View file

@ -0,0 +1,35 @@
// default mode and number of printed digits
//
#define DEFAULT_MODE number::std
#define DEFAULT_PRECISION 20
// MPFR related constants
//
// rounding method
#define MPFR_DEF_RND MPFR_RNDN
// 128 bits significand precision
#define MPFR_DEFAULT_PREC_BITS 128
// 128 bits significand storing length in bytes, result of mpfr_custom_get_size(128)
#define MPFR_DEFAULT_STORING_LENGTH_BYTES 16
// show formats
#define MPFR_FORMAT_BEG "%."
#define MPFR_FORMAT_STD "Rg"
#define MPFR_FORMAT_FIX "Rf"
#define MPFR_FORMAT_SCI "Re"
#define MPFR_FORMAT_HEX "%Ra"
#define MPFR_FORMAT "%.20Rg"
#define MPFR_RND_STRINGS { "nearest", "toward zero", "toward +inf", "toward -inf", "away from zero" }
// return values
//
#define RET_VALUE_STRINGS { \
"ok", "unknown command", "missing operand", "bad operand type", "out of range", "unknown variable", "internal error, aborting", \
"deadly", "goodbye", "not implemented", "no operation", "syntax error", "division by zero", "runtime error" \
}
// command types
//
#define CMD_TYPE_STRINGS { "undef", "number", "string", "symbol", "program", "keyword", "keyword" }

View file

@ -75,7 +75,7 @@ void std()
// format for mpfr_printf // format for mpfr_printf
stringstream ss; stringstream ss;
ss << number::s_current_precision; ss << number::s_current_precision;
s_mpfr_printf_format = s_mpfr_printf_format_beg + ss.str() + s_mpfr_printf_format_std; s_mpfr_printf_format = string(MPFR_FORMAT_BEG) + ss.str() + string(MPFR_FORMAT_STD);
} }
void fix() void fix()
@ -91,7 +91,7 @@ void fix()
// format for mpfr_printf // format for mpfr_printf
stringstream ss; stringstream ss;
ss << number::s_current_precision; ss << number::s_current_precision;
s_mpfr_printf_format = s_mpfr_printf_format_beg + ss.str() + s_mpfr_printf_format_fix; s_mpfr_printf_format = string(MPFR_FORMAT_BEG) + ss.str() + string(MPFR_FORMAT_FIX);
} }
void sci() void sci()
@ -107,7 +107,7 @@ void sci()
// format for mpfr_printf // format for mpfr_printf
stringstream ss; stringstream ss;
ss << number::s_current_precision; ss << number::s_current_precision;
s_mpfr_printf_format = s_mpfr_printf_format_beg + ss.str() + s_mpfr_printf_format_sci; s_mpfr_printf_format = string(MPFR_FORMAT_BEG) + ss.str() + string(MPFR_FORMAT_SCI);
} }
void rpn_version() void rpn_version()

View file

@ -25,6 +25,8 @@
#include <mpfr.h> #include <mpfr.h>
#include <math.h> #include <math.h>
#include "constant.h"
#include "debug.h" #include "debug.h"
extern "C" { extern "C" {
@ -39,27 +41,12 @@ extern "C" {
#include <fstream> #include <fstream>
using namespace std; using namespace std;
// default mode and number of printed digits static string s_mpfr_printf_format = string(MPFR_FORMAT);
#define DEFAULT_MODE number::std static mpfr_prec_t s_mpfr_prec = MPFR_DEFAULT_PREC_BITS;
#define DEFAULT_PRECISION 20 static unsigned int s_mpfr_prec_bytes = MPFR_DEFAULT_STORING_LENGTH_BYTES;
// MPFR related constants
// 128 bits significand storing length in bytes, result of mpfr_custom_get_size(128)
#define MPFR_DEF_RND MPFR_RNDN
#define MPFR_128BITS_PREC 128
#define MPFR_128BITS_STORING_LENGTH 16
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 = "%.20Rg";
static mpfr_prec_t s_mpfr_prec = MPFR_128BITS_PREC;
static unsigned int s_mpfr_prec_bytes = MPFR_128BITS_STORING_LENGTH;
static mpfr_rnd_t s_mpfr_rnd = MPFR_DEF_RND; static mpfr_rnd_t s_mpfr_rnd = MPFR_DEF_RND;
static const char* s_mpfr_rnd_str[5] = { "nearest", "toward zero", "toward +inf", "toward -inf", "away from zero" }; static const char* s_mpfr_rnd_str[5] = MPFR_RND_STRINGS;
// //
#include "escape.h" #include "escape.h"
@ -83,10 +70,7 @@ typedef enum {
ret_max ret_max
} ret_value; } ret_value;
static const char* ret_value_string[ret_max] = { static const char* ret_value_string[ret_max] = RET_VALUE_STRINGS;
"ok", "unknown command", "missing operand", "bad operand type", "out of range", "unknown variable", "internal error, aborting",
"deadly", "goodbye", "not implemented", "no operation", "syntax error", "division by zero", "runtime error"
};
typedef enum { typedef enum {
cmd_undef, cmd_undef,
@ -99,9 +83,7 @@ typedef enum {
cmd_max cmd_max
} cmd_type_t; } cmd_type_t;
const char* cmd_type_string[cmd_max] = { const char* cmd_type_string[cmd_max] = CMD_TYPE_STRINGS;
"undef", "number", "string", "symbol", "program", "keyword", "keyword"
};
// MPFR object // MPFR object
struct floating_t struct floating_t
@ -110,7 +92,7 @@ struct floating_t
void init(void* significand) void init(void* significand)
{ {
mpfr_custom_init(significand, MPFR_128BITS_PREC); mpfr_custom_init(significand, MPFR_DEFAULT_PREC_BITS);
mpfr_custom_init_set(mpfr, MPFR_ZERO_KIND, 0, s_mpfr_prec, significand); mpfr_custom_init_set(mpfr, MPFR_ZERO_KIND, 0, s_mpfr_prec, significand);
} }
@ -376,7 +358,7 @@ void object::show(ostream& stream)
//stream<<buffer; //stream<<buffer;
break; break;
case number::hex: case number::hex:
(void)mpfr_printf(s_mpfr_printf_format_hex.c_str(), ((number*)this)->_value.mpfr); (void)mpfr_printf(string(MPFR_FORMAT_HEX).c_str(), ((number*)this)->_value.mpfr);
//stream<<buffer; //stream<<buffer;
break; break;
case number::bin: case number::bin:
@ -791,7 +773,7 @@ public:
// format for mpfr_printf // format for mpfr_printf
stringstream ss; stringstream ss;
ss << number::s_current_precision; ss << number::s_current_precision;
s_mpfr_printf_format = s_mpfr_printf_format_beg + ss.str() + s_mpfr_printf_format_std; s_mpfr_printf_format = string(MPFR_FORMAT_BEG) + ss.str() + string(MPFR_FORMAT_STD);
} }
private: private: