[6164d0]: src / ov-mp.h  Maximize  Restore  History

Download this file

169 lines (118 with data), 6.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright (C) 2014 Pascal Dupuis <cdemills@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined (ov_mp_h)
#define ov_mp_h 1
// GNU MPFR and GMP
#include <gmp.h>
#include <mpfr.h>
// Generic Library
#include <map>
#include <octave/ov.h>
#include <octave/oct-alloc.h>
#include <octave/ov-typeinfo.h>
// mp values.
class
octave_mp : public octave_base_value
{
public:
octave_mp (void);
octave_mp (signed long si, mpfr_prec_t sprec = mpfr_get_default_prec (), mp_rnd_t srnd = mpfr_get_default_rounding_mode ());
octave_mp (signed long si, mpfr_prec_t sprec, const std::string srnd);
octave_mp (unsigned long ui, mpfr_prec_t sprec = mpfr_get_default_prec (), mp_rnd_t srnd = mpfr_get_default_rounding_mode ());
octave_mp (unsigned long ui, mpfr_prec_t sprec, const std::string srnd);
octave_mp (const octave_mp& s);
octave_mp (const std::string& x, mpfr_prec_t sprec = mpfr_get_default_prec (), mp_rnd_t srnd = mpfr_get_default_rounding_mode ());
octave_mp (const std::string& x, mpfr_prec_t sprec, const std::string srnd);
octave_mp (const float& x, mpfr_prec_t sprec = mpfr_get_default_prec (), mp_rnd_t srnd = mpfr_get_default_rounding_mode ());
octave_mp (const float& x, mpfr_prec_t sprec, const std::string srnd);
octave_mp (const double& x, mpfr_prec_t sprec = mpfr_get_default_prec (), mp_rnd_t srnd = mpfr_get_default_rounding_mode ());
octave_mp (const double& x, mpfr_prec_t sprec, const std::string srnd);
octave_mp (const octave_value& x, mpfr_prec_t sprec = mpfr_get_default_prec (), mp_rnd_t srnd = mpfr_get_default_rounding_mode ());
octave_mp (const octave_value& x, mpfr_prec_t sprec, const std::string srnd);
~octave_mp (void) { if (0 == scalar->_mpfr_d) return;
mpfr_clear (scalar); scalar->_mpfr_d = 0; }
OV_REP_TYPE *clone (void) const { return new octave_mp (*this); }
#if 0
void *operator new (size_t size);
void operator delete (void *p, size_t size);
#endif
type_conv_info numeric_conversion_function (void) const;
dim_vector dims (void) const { static dim_vector dv (1, 1); return dv; }
int rows (void) const { return 1; }
int columns (void) const { return 1; }
bool is_constant (void) const { return true; }
bool is_defined (void) const { return true; }
bool is_real_scalar (void) const { return true; }
octave_value all (void) const { return (double) (mpfr_regular_p (scalar)); }
octave_value any (void) const { return (double) (mpfr_regular_p (scalar)); }
bool is_real_type (void) const { return true; }
bool is_scalar_type (void) const { return true; }
bool is_mp_type (void) const { return true; }
/* A few proxy to work on octave_base_value.
Instead of downclassing, upclass both arguments */
octave_value oct_monocmp_proxy (const octave_base_value& a1,
int (*func) (mpfr_srcptr op1));
octave_value oct_monop_proxy (const octave_base_value& a1,
int (*func) (mpfr_ptr rop, mpfr_srcptr op1, mpfr_rnd_t rnd));
octave_value oct_bincmp_proxy (const octave_base_value& a1,
const octave_base_value& a2,
int (*func) (mpfr_srcptr op1, mpfr_srcptr op2));
octave_value oct_binop_proxy (const octave_base_value& a1,
const octave_base_value& a2,
int (*func) (mpfr_ptr rop, mpfr_srcptr op1, mpfr_srcptr op2, mpfr_rnd_t rnd));
bool valid_as_scalar_index (void) const
{
mpfr_t one;
int mpeq;
mpfr_init_set_ui (one, 1, rnd);
mpeq = mpfr_equal_p (scalar, one);
mpfr_clear (one);
return (bool) mpeq;
}
bool valid_as_zero_index (void) const
{ return mpfr_zero_p(scalar); }
bool is_true (void) const { return (mpfr_regular_p (scalar)); }
long int long_value (bool = false) const { return mpfr_get_si (scalar, rnd); }
unsigned long int ulong_value (bool = false) const { return mpfr_get_ui (scalar, rnd); }
float float_value (bool = false) const { return mpfr_get_flt (scalar, rnd); }
double double_value (bool = false) const { return mpfr_get_d (scalar, rnd); }
double mp_value (bool = false) const { return 42; }
octave_value hermitian (void) const { return new octave_mp (*this); }
void increment (void) { mpfr_add_ui(scalar, scalar, 1, rnd); }
void decrement (void) { mpfr_sub_ui(scalar, scalar, 1, rnd); }
void print (std::ostream& os, bool pr_as_read_syntax = false) const;
octave_value map (unary_mapper_t umap) const;
mpfr_rnd_t convert_rnd_mode (const std::string mode);
void commun_string_parser (const std::string src, mp_rnd_t srnd);
void commun_ov_parser (const octave_value &x, mp_rnd_t srnd);
private:
mpfr_t scalar;
static mpfr_prec_t prec;
mpfr_rnd_t rnd;
static std::map<char, int> rndmap;
static std::map<char, int> initmap() {
std::map<char, int> somemap;
/* This should match MPFR definitions ! */
somemap['N'] = MPFR_RNDN; /* round to nearest, with ties to even */
somemap['Z'] = MPFR_RNDZ; /* round toward zero */
somemap['U'] = MPFR_RNDU; /* round toward +Inf */
somemap['D'] = MPFR_RNDD; /* round toward -Inf */
somemap['A'] = MPFR_RNDA; /* round away from zero */
somemap['F'] = MPFR_RNDF; /* faithful rounding (not implemented yet) */
return somemap;
}
DECLARE_OCTAVE_ALLOCATOR
DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
};
#endif

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks