Ruby  2.1.10p492(2016-04-01revision54464)
bigdecimal.h
Go to the documentation of this file.
1 /*
2  *
3  * Ruby BigDecimal(Variable decimal precision) extension library.
4  *
5  * Copyright(C) 2002 by Shigeo Kobayashi(shigeo@tinyforest.gr.jp)
6  *
7  */
8 
9 #ifndef RUBY_BIG_DECIMAL_H
10 #define RUBY_BIG_DECIMAL_H 1
11 
12 #include "ruby/ruby.h"
13 #include <float.h>
14 
15 #ifndef RB_UNUSED_VAR
16 # ifdef __GNUC__
17 # define RB_UNUSED_VAR(x) x __attribute__ ((unused))
18 # else
19 # define RB_UNUSED_VAR(x) x
20 # endif
21 #endif
22 
23 #ifndef UNREACHABLE
24 # define UNREACHABLE /* unreachable */
25 #endif
26 
27 #undef BDIGIT
28 #undef SIZEOF_BDIGITS
29 #undef BDIGIT_DBL
30 #undef BDIGIT_DBL_SIGNED
31 #undef PRI_BDIGIT_PREFIX
32 #undef PRI_BDIGIT_DBL_PREFIX
33 
34 #ifdef HAVE_INT64_T
35 # define BDIGIT uint32_t
36 # define BDIGIT_DBL uint64_t
37 # define BDIGIT_DBL_SIGNED int64_t
38 # define SIZEOF_BDIGITS 4
39 #else
40 # define BDIGIT uint16_t
41 # define BDIGIT_DBL uint32_t
42 # define BDIGIT_DBL_SIGNED int32_t
43 # define SIZEOF_BDIGITS 2
44 #endif
45 
46 #if defined(__cplusplus)
47 extern "C" {
48 #if 0
49 } /* satisfy cc-mode */
50 #endif
51 #endif
52 
53 #ifndef HAVE_LABS
54 static inline long
55 labs(long const x)
56 {
57  if (x < 0) return -x;
58  return x;
59 }
60 #endif
61 
62 #ifndef HAVE_LLABS
63 static inline LONG_LONG
64 llabs(LONG_LONG const x)
65 {
66  if (x < 0) return -x;
67  return x;
68 }
69 #endif
70 
71 #ifdef vabs
72 # undef vabs
73 #endif
74 #if SIZEOF_VALUE <= SIZEOF_INT
75 # define vabs abs
76 #elif SIZEOF_VALUE <= SIZEOF_LONG
77 # define vabs labs
78 #elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
79 # define vabs llabs
80 #endif
81 
82 extern VALUE rb_cBigDecimal;
83 
84 #if 0 || SIZEOF_BDIGITS >= 16
85 # define RMPD_COMPONENT_FIGURES 38
86 # define RMPD_BASE ((BDIGIT)100000000000000000000000000000000000000U)
87 #elif SIZEOF_BDIGITS >= 8
88 # define RMPD_COMPONENT_FIGURES 19
89 # define RMPD_BASE ((BDIGIT)10000000000000000000U)
90 #elif SIZEOF_BDIGITS >= 4
91 # define RMPD_COMPONENT_FIGURES 9
92 # define RMPD_BASE ((BDIGIT)1000000000U)
93 #elif SIZEOF_BDIGITS >= 2
94 # define RMPD_COMPONENT_FIGURES 4
95 # define RMPD_BASE ((BDIGIT)10000U)
96 #else
97 # define RMPD_COMPONENT_FIGURES 2
98 # define RMPD_BASE ((BDIGIT)100U)
99 #endif
100 
101 
102 /*
103  * NaN & Infinity
104  */
105 #define SZ_NaN "NaN"
106 #define SZ_INF "Infinity"
107 #define SZ_PINF "+Infinity"
108 #define SZ_NINF "-Infinity"
109 
110 /*
111  * #define VP_EXPORT other than static to let VP_ routines
112  * be called from outside of this module.
113  */
114 #define VP_EXPORT static
115 
116 /* Exception codes */
117 #define VP_EXCEPTION_ALL ((unsigned short)0x00FF)
118 #define VP_EXCEPTION_INFINITY ((unsigned short)0x0001)
119 #define VP_EXCEPTION_NaN ((unsigned short)0x0002)
120 #define VP_EXCEPTION_UNDERFLOW ((unsigned short)0x0004)
121 #define VP_EXCEPTION_OVERFLOW ((unsigned short)0x0001) /* 0x0008) */
122 #define VP_EXCEPTION_ZERODIVIDE ((unsigned short)0x0010)
123 
124 /* Following 2 exceptions can't controlled by user */
125 #define VP_EXCEPTION_OP ((unsigned short)0x0020)
126 #define VP_EXCEPTION_MEMORY ((unsigned short)0x0040)
127 
128 #define RMPD_EXCEPTION_MODE_DEFAULT 0U
129 
130 /* Computation mode */
131 #define VP_ROUND_MODE ((unsigned short)0x0100)
132 #define VP_ROUND_UP 1
133 #define VP_ROUND_DOWN 2
134 #define VP_ROUND_HALF_UP 3
135 #define VP_ROUND_HALF_DOWN 4
136 #define VP_ROUND_CEIL 5
137 #define VP_ROUND_FLOOR 6
138 #define VP_ROUND_HALF_EVEN 7
139 
140 #define RMPD_ROUNDING_MODE_DEFAULT VP_ROUND_HALF_UP
141 
142 #define VP_SIGN_NaN 0 /* NaN */
143 #define VP_SIGN_POSITIVE_ZERO 1 /* Positive zero */
144 #define VP_SIGN_NEGATIVE_ZERO -1 /* Negative zero */
145 #define VP_SIGN_POSITIVE_FINITE 2 /* Positive finite number */
146 #define VP_SIGN_NEGATIVE_FINITE -2 /* Negative finite number */
147 #define VP_SIGN_POSITIVE_INFINITE 3 /* Positive infinite number */
148 #define VP_SIGN_NEGATIVE_INFINITE -3 /* Negative infinite number */
149 
150 #ifdef __GNUC__
151 #define FLEXIBLE_ARRAY_SIZE 0
152 #else
153 #define FLEXIBLE_ARRAY_SIZE 1
154 #endif
155 
156 /*
157  * VP representation
158  * r = 0.xxxxxxxxx *BASE**exponent
159  */
160 typedef struct {
161  VALUE obj; /* Back pointer(VALUE) for Ruby object. */
162  size_t MaxPrec; /* Maximum precision size */
163  /* This is the actual size of pfrac[] */
164  /*(frac[0] to frac[MaxPrec] are available). */
165  size_t Prec; /* Current precision size. */
166  /* This indicates how much the. */
167  /* the array frac[] is actually used. */
168  SIGNED_VALUE exponent; /* Exponent part. */
169  short sign; /* Attributes of the value. */
170  /*
171  * ==0 : NaN
172  * 1 : Positive zero
173  * -1 : Negative zero
174  * 2 : Positive number
175  * -2 : Negative number
176  * 3 : Positive infinite number
177  * -3 : Negative infinite number
178  */
179  short flag; /* Not used in vp_routines,space for user. */
180  BDIGIT frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */
181 } Real;
182 
183 /*
184  * ------------------
185  * EXPORTables.
186  * ------------------
187  */
188 
189 VP_EXPORT Real *
190 VpNewRbClass(size_t mx, char const *str, VALUE klass);
191 
192 VP_EXPORT Real *VpCreateRbObject(size_t mx,const char *str);
193 
194 static inline BDIGIT
195 rmpd_base_value(void) { return RMPD_BASE; }
196 static inline size_t
198 static inline size_t
199 rmpd_double_figures(void) { return 1+DBL_DIG; }
200 
201 #define VpBaseFig() rmpd_component_figures()
202 #define VpDblFig() rmpd_double_figures()
203 #define VpBaseVal() rmpd_base_value()
204 
205 /* Zero,Inf,NaN (isinf(),isnan() used to check) */
206 VP_EXPORT double VpGetDoubleNaN(void);
207 VP_EXPORT double VpGetDoublePosInf(void);
208 VP_EXPORT double VpGetDoubleNegInf(void);
209 VP_EXPORT double VpGetDoubleNegZero(void);
210 
211 /* These 2 functions added at v1.1.7 */
212 VP_EXPORT size_t VpGetPrecLimit(void);
213 VP_EXPORT size_t VpSetPrecLimit(size_t n);
214 
215 /* Round mode */
216 VP_EXPORT int VpIsRoundMode(unsigned short n);
217 VP_EXPORT unsigned short VpGetRoundMode(void);
218 VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
219 
220 VP_EXPORT int VpException(unsigned short f,const char *str,int always);
221 #if 0 /* unused */
222 VP_EXPORT int VpIsNegDoubleZero(double v);
223 #endif
224 VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
225 VP_EXPORT size_t VpInit(BDIGIT BaseVal);
226 VP_EXPORT void *VpMemAlloc(size_t mb);
227 VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb);
228 VP_EXPORT void VpFree(Real *pv);
229 VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal);
230 VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
231 VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
232 VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
233 VP_EXPORT size_t VpDivd(Real *c,Real *r,Real *a,Real *b);
234 VP_EXPORT int VpComp(Real *a,Real *b);
235 VP_EXPORT ssize_t VpExponent10(Real *a);
236 VP_EXPORT void VpSzMantissa(Real *a,char *psz);
237 VP_EXPORT int VpToSpecialString(Real *a,char *psz,int fPlus);
238 VP_EXPORT void VpToString(Real *a, char *psz, size_t fFmt, int fPlus);
239 VP_EXPORT void VpToFString(Real *a, char *psz, size_t fFmt, int fPlus);
240 VP_EXPORT int VpCtoV(Real *a, const char *int_chr, size_t ni, const char *frac, size_t nf, const char *exp_chr, size_t ne);
241 VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m);
242 VP_EXPORT void VpDtoV(Real *m,double d);
243 #if 0 /* unused */
244 VP_EXPORT void VpItoV(Real *m,S_INT ival);
245 #endif
246 VP_EXPORT int VpSqrt(Real *y,Real *x);
247 VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
248 VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
249 VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
250 VP_EXPORT void VpFrac(Real *y, Real *x);
251 VP_EXPORT int VpPower(Real *y, Real *x, SIGNED_VALUE n);
252 
253 /* VP constants */
254 VP_EXPORT Real *VpOne(void);
255 
256 /*
257  * ------------------
258  * MACRO definitions.
259  * ------------------
260  */
261 #define Abs(a) (((a)>= 0)?(a):(-(a)))
262 #define Max(a, b) (((a)>(b))?(a):(b))
263 #define Min(a, b) (((a)>(b))?(b):(a))
264 
265 #define VpMaxPrec(a) ((a)->MaxPrec)
266 #define VpPrec(a) ((a)->Prec)
267 #define VpGetFlag(a) ((a)->flag)
268 
269 /* Sign */
270 
271 /* VpGetSign(a) returns 1,-1 if a>0,a<0 respectively */
272 #define VpGetSign(a) (((a)->sign>0)?1:(-1))
273 /* Change sign of a to a>0,a<0 if s = 1,-1 respectively */
274 #define VpChangeSign(a,s) {if((s)>0) (a)->sign=(short)Abs((ssize_t)(a)->sign);else (a)->sign=-(short)Abs((ssize_t)(a)->sign);}
275 /* Sets sign of a to a>0,a<0 if s = 1,-1 respectively */
276 #define VpSetSign(a,s) {if((s)>0) (a)->sign=(short)VP_SIGN_POSITIVE_FINITE;else (a)->sign=(short)VP_SIGN_NEGATIVE_FINITE;}
277 
278 /* 1 */
279 #define VpSetOne(a) {(a)->Prec=(a)->exponent=(a)->frac[0]=1;(a)->sign=VP_SIGN_POSITIVE_FINITE;}
280 
281 /* ZEROs */
282 #define VpIsPosZero(a) ((a)->sign==VP_SIGN_POSITIVE_ZERO)
283 #define VpIsNegZero(a) ((a)->sign==VP_SIGN_NEGATIVE_ZERO)
284 #define VpIsZero(a) (VpIsPosZero(a) || VpIsNegZero(a))
285 #define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_ZERO)
286 #define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
287 #define VpSetZero(a,s) ( ((s)>0)?VpSetPosZero(a):VpSetNegZero(a) )
288 
289 /* NaN */
290 #define VpIsNaN(a) ((a)->sign==VP_SIGN_NaN)
291 #define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NaN)
292 
293 /* Infinity */
294 #define VpIsPosInf(a) ((a)->sign==VP_SIGN_POSITIVE_INFINITE)
295 #define VpIsNegInf(a) ((a)->sign==VP_SIGN_NEGATIVE_INFINITE)
296 #define VpIsInf(a) (VpIsPosInf(a) || VpIsNegInf(a))
297 #define VpIsDef(a) ( !(VpIsNaN(a)||VpIsInf(a)) )
298 #define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
299 #define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
300 #define VpSetInf(a,s) ( ((s)>0)?VpSetPosInf(a):VpSetNegInf(a) )
301 #define VpHasVal(a) (a->frac[0])
302 #define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
303 #define VpExponent(a) (a->exponent)
304 #ifdef BIGDECIMAL_DEBUG
305 int VpVarCheck(Real * v);
306 #endif /* BIGDECIMAL_DEBUG */
307 
308 #if defined(__cplusplus)
309 #if 0
310 { /* satisfy cc-mode */
311 #endif
312 } /* extern "C" { */
313 #endif
314 #endif /* RUBY_BIG_DECIMAL_H */
VP_EXPORT void * VpMemRealloc(void *ptr, size_t mb)
Definition: bigdecimal.c:3350
VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il)
Definition: bigdecimal.c:5965
VP_EXPORT ssize_t VpExponent10(Real *a)
Definition: bigdecimal.c:5106
SIGNED_VALUE exponent
Definition: bigdecimal.h:168
VP_EXPORT double VpGetDoubleNaN(void)
Definition: bigdecimal.c:3541
#define DBL_DIG
Definition: numeric.c:67
VALUE obj
Definition: bigdecimal.h:161
size_t Prec
Definition: bigdecimal.h:165
static long labs(long const x)
Definition: bigdecimal.h:55
VP_EXPORT size_t VpGetPrecLimit(void)
Definition: bigdecimal.c:3425
VP_EXPORT size_t VpDivd(Real *c, Real *r, Real *a, Real *b)
Definition: bigdecimal.c:4622
VP_EXPORT size_t VpMult(Real *c, Real *a, Real *b)
Definition: bigdecimal.c:4493
VP_EXPORT int VpComp(Real *a, Real *b)
Definition: bigdecimal.c:4875
VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m)
Definition: bigdecimal.c:5479
static LONG_LONG llabs(LONG_LONG const x)
Definition: bigdecimal.h:64
VP_EXPORT void VpFrac(Real *y, Real *x)
Definition: bigdecimal.c:6060
static size_t rmpd_component_figures(void)
Definition: bigdecimal.h:197
VP_EXPORT size_t VpAddSub(Real *c, Real *a, Real *b, int operation)
Definition: bigdecimal.c:4038
VP_EXPORT void VpToFString(Real *a, char *psz, size_t fFmt, int fPlus)
Definition: bigdecimal.c:5253
VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf)
Definition: bigdecimal.c:5783
VP_EXPORT Real * VpNewRbClass(size_t mx, char const *str, VALUE klass)
Definition: bigdecimal.c:564
#define RMPD_COMPONENT_FIGURES
Definition: bigdecimal.h:94
VP_EXPORT void * VpMemAlloc(size_t mb)
Definition: bigdecimal.c:3336
#define ne(x, y)
Definition: time.c:66
VP_EXPORT void VpToString(Real *a, char *psz, size_t fFmt, int fPlus)
Definition: bigdecimal.c:5205
#define VP_EXPORT
Definition: bigdecimal.h:114
#define RMPD_BASE
Definition: bigdecimal.h:95
VP_EXPORT size_t VpNumOfChars(Real *vp, const char *pszFmt)
Definition: bigdecimal.c:3705
VALUE rb_cBigDecimal
Definition: bigdecimal.c:44
VP_EXPORT void VpFree(Real *pv)
Definition: bigdecimal.c:3360
VP_EXPORT Real * VpCreateRbObject(size_t mx, const char *str)
Definition: bigdecimal.c:572
VP_EXPORT double VpGetDoubleNegZero(void)
Definition: bigdecimal.c:3565
VP_EXPORT int VpIsRoundMode(unsigned short n)
Definition: bigdecimal.c:3476
static VALUE int_chr(int argc, VALUE *argv, VALUE num)
Definition: numeric.c:2557
short flag
Definition: bigdecimal.h:179
VP_EXPORT int VpSqrt(Real *y, Real *x)
Definition: bigdecimal.c:5675
VP_EXPORT void VpDtoV(Real *m, double d)
Definition: bigdecimal.c:5543
VP_EXPORT int VpToSpecialString(Real *a, char *psz, int fPlus)
Definition: bigdecimal.c:5170
unsigned long VALUE
Definition: ruby.h:88
static BDIGIT rmpd_base_value(void)
Definition: bigdecimal.h:195
VP_EXPORT double VpGetDoubleNegInf(void)
Definition: bigdecimal.c:3557
VP_EXPORT size_t VpSetPrecLimit(size_t n)
Definition: bigdecimal.c:3441
VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw)
Definition: bigdecimal.c:3994
short sign
Definition: bigdecimal.h:169
static size_t rmpd_double_figures(void)
Definition: bigdecimal.h:199
VP_EXPORT int VpCtoV(Real *a, const char *int_chr, size_t ni, const char *frac, size_t nf, const char *exp_chr, size_t ne)
Definition: bigdecimal.c:5319
#define f
VP_EXPORT void VpSzMantissa(Real *a, char *psz)
Definition: bigdecimal.c:5123
VP_EXPORT int VpPower(Real *y, Real *x, SIGNED_VALUE n)
Definition: bigdecimal.c:6108
VP_EXPORT unsigned short VpGetRoundMode(void)
Definition: bigdecimal.c:3460
VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf)
Definition: bigdecimal.c:5950
VP_EXPORT Real * VpAlloc(size_t mx, const char *szVal)
Definition: bigdecimal.c:3837
#define FLEXIBLE_ARRAY_SIZE
Definition: bigdecimal.h:153
VP_EXPORT size_t VpInit(BDIGIT BaseVal)
Definition: bigdecimal.c:3749
VP_EXPORT double VpGetDoublePosInf(void)
Definition: bigdecimal.c:3549
VP_EXPORT int VpException(unsigned short f, const char *str, int always)
Definition: bigdecimal.c:3582
VP_EXPORT Real * VpOne(void)
Definition: bigdecimal.c:3780
VP_EXPORT unsigned short VpSetRoundMode(unsigned short n)
Definition: bigdecimal.c:3494
#define BDIGIT
Definition: bigdecimal.h:40
size_t MaxPrec
Definition: bigdecimal.h:162
#define SIGNED_VALUE
Definition: ruby.h:90