Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file
9 * of this BigDecimal distribution.
10 *
11 * NOTES:
12 * 2003-03-28 V1.0 checked in.
13 *
14 */
15
16 #ifndef RUBY_BIG_DECIMAL_H
17 #define RUBY_BIG_DECIMAL_H 1
18
19 #include "ruby/ruby.h"
20 #include <float.h>
21
22 #if defined(__cplusplus)
23 extern "C" {
24 #if 0
25 } /* satisfy cc-mode */
26 #endif
27 #endif
28
29 #ifndef HAVE_LABS
30 static inline long
31 labs(long const x)
32 {
33 if (x < 0) return -x;
34 return x;
35 }
36 #endif
37
38 #ifndef HAVE_LLABS
39 static inline LONG_LONG
40 llabs(LONG_LONG const x)
41 {
42 if (x < 0) return -x;
43 return x;
44 }
45 #endif
46
47 #ifdef vabs
48 # undef vabs
49 #endif
50 #if SIZEOF_VALUE <= SIZEOF_INT
51 # define vabs abs
52 #elif SIZEOF_VALUE <= SIZEOF_LONG
53 # define vabs labs
54 #elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
55 # define vabs llabs
56 #endif
57
58 extern VALUE rb_cBigDecimal;
59
60 #if 0 || SIZEOF_BDIGITS >= 16
61 # define RMPD_COMPONENT_FIGURES 38
62 # define RMPD_BASE ((BDIGIT)100000000000000000000000000000000000000U)
63 #elif SIZEOF_BDIGITS >= 8
64 # define RMPD_COMPONENT_FIGURES 19
65 # define RMPD_BASE ((BDIGIT)10000000000000000000U)
66 #elif SIZEOF_BDIGITS >= 4
67 # define RMPD_COMPONENT_FIGURES 9
68 # define RMPD_BASE ((BDIGIT)1000000000U)
69 #elif SIZEOF_BDIGITS >= 2
70 # define RMPD_COMPONENT_FIGURES 4
71 # define RMPD_BASE ((BDIGIT)10000U)
72 #else
73 # define RMPD_COMPONENT_FIGURES 2
74 # define RMPD_BASE ((BDIGIT)100U)
75 #endif
76
77
78 /*
79 * NaN & Infinity
80 */
81 #define SZ_NaN "NaN"
82 #define SZ_INF "Infinity"
83 #define SZ_PINF "+Infinity"
84 #define SZ_NINF "-Infinity"
85
86 /*
87 * #define VP_EXPORT other than static to let VP_ routines
88 * be called from outside of this module.
89 */
90 #define VP_EXPORT static
91
92 /* Exception codes */
93 #define VP_EXCEPTION_ALL ((unsigned short)0x00FF)
94 #define VP_EXCEPTION_INFINITY ((unsigned short)0x0001)
95 #define VP_EXCEPTION_NaN ((unsigned short)0x0002)
96 #define VP_EXCEPTION_UNDERFLOW ((unsigned short)0x0004)
97 #define VP_EXCEPTION_OVERFLOW ((unsigned short)0x0001) /* 0x0008) */
98 #define VP_EXCEPTION_ZERODIVIDE ((unsigned short)0x0010)
99
100 /* Following 2 exceptions cann't controlled by user */
101 #define VP_EXCEPTION_OP ((unsigned short)0x0020)
102 #define VP_EXCEPTION_MEMORY ((unsigned short)0x0040)
103
104 #define RMPD_EXCEPTION_MODE_DEFAULT 0U
105
106 /* Computation mode */
107 #define VP_ROUND_MODE ((unsigned short)0x0100)
108 #define VP_ROUND_UP 1
109 #define VP_ROUND_DOWN 2
110 #define VP_ROUND_HALF_UP 3
111 #define VP_ROUND_HALF_DOWN 4
112 #define VP_ROUND_CEIL 5
113 #define VP_ROUND_FLOOR 6
114 #define VP_ROUND_HALF_EVEN 7
115
116 #define RMPD_ROUNDING_MODE_DEFAULT VP_ROUND_HALF_UP
117
118 #define VP_SIGN_NaN 0 /* NaN */
119 #define VP_SIGN_POSITIVE_ZERO 1 /* Positive zero */
120 #define VP_SIGN_NEGATIVE_ZERO -1 /* Negative zero */
121 #define VP_SIGN_POSITIVE_FINITE 2 /* Positive finite number */
122 #define VP_SIGN_NEGATIVE_FINITE -2 /* Negative finite number */
123 #define VP_SIGN_POSITIVE_INFINITE 3 /* Positive infinite number */
124 #define VP_SIGN_NEGATIVE_INFINITE -3 /* Negative infinite number */
125
126 #ifdef __GNUC__
127 #define FLEXIBLE_ARRAY_SIZE 0
128 #else
129 #define FLEXIBLE_ARRAY_SIZE 1
130 #endif
131
132 /*
133 * VP representation
134 * r = 0.xxxxxxxxx *BASE**exponent
135 */
136 typedef struct {
137 VALUE obj; /* Back pointer(VALUE) for Ruby object. */
138 size_t MaxPrec; /* Maximum precision size */
139 /* This is the actual size of pfrac[] */
140 /*(frac[0] to frac[MaxPrec] are available). */
141 size_t Prec; /* Current precision size. */
142 /* This indicates how much the. */
143 /* the array frac[] is actually used. */
144 SIGNED_VALUE exponent; /* Exponent part. */
145 short sign; /* Attributes of the value. */
146 /*
147 * ==0 : NaN
148 * 1 : Positive zero
149 * -1 : Negative zero
150 * 2 : Positive number
151 * -2 : Negative number
152 * 3 : Positive infinite number
153 * -3 : Negative infinite number
154 */
155 short flag; /* Not used in vp_routines,space for user. */
156 BDIGIT frac[FLEXIBLE_ARRAY_SIZE]; /* Array of fraction part. */
157 } Real;
158
159 /*
160 * ------------------
161 * EXPORTables.
162 * ------------------
163 */
164
165 VP_EXPORT Real *
166 VpNewRbClass(size_t mx, char const *str, VALUE klass);
167
168 VP_EXPORT Real *VpCreateRbObject(size_t mx,const char *str);
169
170 static inline BDIGIT
171 rmpd_base_value(void) { return RMPD_BASE; }
172 static inline size_t
173 rmpd_component_figures(void) { return RMPD_COMPONENT_FIGURES; }
174 static inline size_t
175 rmpd_double_figures(void) { return 1+DBL_DIG; }
176
177 #define VpBaseFig() rmpd_component_figures()
178 #define VpDblFig() rmpd_double_figures()
179 #define VpBaseVal() rmpd_base_value()
180
181 /* Zero,Inf,NaN (isinf(),isnan() used to check) */
182 VP_EXPORT double VpGetDoubleNaN(void);
183 VP_EXPORT double VpGetDoublePosInf(void);
184 VP_EXPORT double VpGetDoubleNegInf(void);
185 VP_EXPORT double VpGetDoubleNegZero(void);
186
187 /* These 2 functions added at v1.1.7 */
188 VP_EXPORT size_t VpGetPrecLimit(void);
189 VP_EXPORT size_t VpSetPrecLimit(size_t n);
190
191 /* Round mode */
192 VP_EXPORT int VpIsRoundMode(unsigned short n);
193 VP_EXPORT unsigned short VpGetRoundMode(void);
194 VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
195
196 VP_EXPORT int VpException(unsigned short f,const char *str,int always);
197 #if 0 /* unused */
198 VP_EXPORT int VpIsNegDoubleZero(double v);
199 #endif
200 VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
201 VP_EXPORT size_t VpInit(BDIGIT BaseVal);
202 VP_EXPORT void *VpMemAlloc(size_t mb);
203 VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb);
204 VP_EXPORT void VpFree(Real *pv);
205 VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal);
206 VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
207 VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
208 VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
209 VP_EXPORT size_t VpDivd(Real *c,Real *r,Real *a,Real *b);
210 VP_EXPORT int VpComp(Real *a,Real *b);
211 VP_EXPORT ssize_t VpExponent10(Real *a);
212 VP_EXPORT void VpSzMantissa(Real *a,char *psz);
213 VP_EXPORT int VpToSpecialString(Real *a,char *psz,int fPlus);
214 VP_EXPORT void VpToString(Real *a, char *psz, size_t fFmt, int fPlus);
215 VP_EXPORT void VpToFString(Real *a, char *psz, size_t fFmt, int fPlus);
216 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);
217 VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m);
218 VP_EXPORT void VpDtoV(Real *m,double d);
219 #if 0 /* unused */
220 VP_EXPORT void VpItoV(Real *m,S_INT ival);
221 #endif
222 VP_EXPORT int VpSqrt(Real *y,Real *x);
223 VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
224 VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
225 VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
226 VP_EXPORT void VpFrac(Real *y, Real *x);
227 VP_EXPORT int VpPower(Real *y, Real *x, SIGNED_VALUE n);
228
229 /* VP constants */
230 VP_EXPORT Real *VpOne(void);
231
232 /*
233 * ------------------
234 * MACRO definitions.
235 * ------------------
236 */
237 #define Abs(a) (((a)>= 0)?(a):(-(a)))
238 #define Max(a, b) (((a)>(b))?(a):(b))
239 #define Min(a, b) (((a)>(b))?(b):(a))
240
241 #define VpMaxPrec(a) ((a)->MaxPrec)
242 #define VpPrec(a) ((a)->Prec)
243 #define VpGetFlag(a) ((a)->flag)
244
245 /* Sign */
246
247 /* VpGetSign(a) returns 1,-1 if a>0,a<0 respectively */
248 #define VpGetSign(a) (((a)->sign>0)?1:(-1))
249 /* Change sign of a to a>0,a<0 if s = 1,-1 respectively */
250 #define VpChangeSign(a,s) {if((s)>0) (a)->sign=(short)Abs((ssize_t)(a)->sign);else (a)->sign=-(short)Abs((ssize_t)(a)->sign);}
251 /* Sets sign of a to a>0,a<0 if s = 1,-1 respectively */
252 #define VpSetSign(a,s) {if((s)>0) (a)->sign=(short)VP_SIGN_POSITIVE_FINITE;else (a)->sign=(short)VP_SIGN_NEGATIVE_FINITE;}
253
254 /* 1 */
255 #define VpSetOne(a) {(a)->Prec=(a)->exponent=(a)->frac[0]=1;(a)->sign=VP_SIGN_POSITIVE_FINITE;}
256
257 /* ZEROs */
258 #define VpIsPosZero(a) ((a)->sign==VP_SIGN_POSITIVE_ZERO)
259 #define VpIsNegZero(a) ((a)->sign==VP_SIGN_NEGATIVE_ZERO)
260 #define VpIsZero(a) (VpIsPosZero(a) || VpIsNegZero(a))
261 #define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_ZERO)
262 #define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
263 #define VpSetZero(a,s) ( ((s)>0)?VpSetPosZero(a):VpSetNegZero(a) )
264
265 /* NaN */
266 #define VpIsNaN(a) ((a)->sign==VP_SIGN_NaN)
267 #define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NaN)
268
269 /* Infinity */
270 #define VpIsPosInf(a) ((a)->sign==VP_SIGN_POSITIVE_INFINITE)
271 #define VpIsNegInf(a) ((a)->sign==VP_SIGN_NEGATIVE_INFINITE)
272 #define VpIsInf(a) (VpIsPosInf(a) || VpIsNegInf(a))
273 #define VpIsDef(a) ( !(VpIsNaN(a)||VpIsInf(a)) )
274 #define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
275 #define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
276 #define VpSetInf(a,s) ( ((s)>0)?VpSetPosInf(a):VpSetNegInf(a) )
277 #define VpHasVal(a) (a->frac[0])
278 #define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
279 #define VpExponent(a) (a->exponent)
280 #ifdef BIGDECIMAL_DEBUG
281 int VpVarCheck(Real * v);
282 VP_EXPORT int VPrint(FILE *fp,const char *cntl_chr,Real *a);
283 #endif /* BIGDECIMAL_DEBUG */
284
285 #if defined(__cplusplus)
286 #if 0
287 { /* satisfy cc-mode */
288 #endif
289 } /* extern "C" { */
290 #endif
291 #endif /* RUBY_BIG_DECIMAL_H */
VpDivd
VP_EXPORT size_t VpDivd(Real *c, Real *r, Real *a, Real *b)
Definition: bigdecimal.c:4522
VpGetDoublePosInf
VP_EXPORT double VpGetDoublePosInf(void)
Definition: bigdecimal.c:3481
VpActiveRound
VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il)
n
ssize_t n
Definition: bigdecimal.c:5655
Real::exponent
SIGNED_VALUE exponent
Definition: bigdecimal.h:144
VpException
VP_EXPORT int VpException(unsigned short f, const char *str, int always)
Definition: bigdecimal.c:3514
ruby.h
DBL_DIG
#define DBL_DIG
Definition: numeric.c:59
Real::obj
VALUE obj
Definition: bigdecimal.h:137
VpIsRoundMode
VP_EXPORT int VpIsRoundMode(unsigned short n)
Definition: bigdecimal.c:3408
a
Real * a
Definition: bigdecimal.c:1182
Real::Prec
size_t Prec
Definition: bigdecimal.h:141
labs
static long labs(long const x)
Definition: bigdecimal.h:31
VpGetRoundMode
VP_EXPORT unsigned short VpGetRoundMode(void)
Definition: bigdecimal.c:3392
mx
size_t mx
Definition: bigdecimal.c:1183
VpSetRoundMode
VP_EXPORT unsigned short VpSetRoundMode(unsigned short n)
Definition: bigdecimal.c:3426
VpDtoV
VP_EXPORT void VpDtoV(Real *m, double d)
Definition: bigdecimal.c:5405
VpFrac
VP_EXPORT void VpFrac(Real *y, Real *x)
Definition: bigdecimal.c:5913
r
r
Definition: bigdecimal.c:1196
VpCreateRbObject
VP_EXPORT Real * VpCreateRbObject(size_t mx, const char *str)
Definition: bigdecimal.c:567
VpVtoD
VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m)
Definition: bigdecimal.c:5341
VpComp
VP_EXPORT int VpComp(Real *a, Real *b)
Definition: bigdecimal.c:4771
d
d
Definition: strlcat.c:58
VpCtoV
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:5192
VpNewRbClass
VP_EXPORT Real * VpNewRbClass(size_t mx, const char *str, VALUE klass)
Definition: bigdecimal.c:559
__sFILE
Definition: vsnprintf.c:175
llabs
static LONG_LONG llabs(LONG_LONG const x)
Definition: bigdecimal.h:40
rmpd_component_figures
static size_t rmpd_component_figures(void)
Definition: bigdecimal.h:173
VpToFString
VP_EXPORT void VpToFString(Real *a, char *psz, size_t fFmt, int fPlus)
VpMidRound
VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf)
m
BDIGIT m
Definition: bigdecimal.c:5085
RMPD_COMPONENT_FIGURES
#define RMPD_COMPONENT_FIGURES
Definition: bigdecimal.h:73
ne
#define ne(x, y)
Definition: time.c:66
VpToString
VP_EXPORT void VpToString(Real *a, char *psz, size_t fFmt, int fPlus)
str
static VALUE char * str
Definition: tcltklib.c:3547
VP_EXPORT
#define VP_EXPORT
Definition: bigdecimal.h:90
VpGetDoubleNaN
VP_EXPORT double VpGetDoubleNaN(void)
Definition: bigdecimal.c:3473
VpGetPrecLimit
VP_EXPORT size_t VpGetPrecLimit(void)
Definition: bigdecimal.c:3357
VpMemAlloc
VP_EXPORT void * VpMemAlloc(size_t mb)
Definition: bigdecimal.c:3268
VpMult
VP_EXPORT size_t VpMult(Real *c, Real *a, Real *b)
Definition: bigdecimal.c:4397
RMPD_BASE
#define RMPD_BASE
Definition: bigdecimal.h:74
VpOne
VP_EXPORT Real * VpOne(void)
Definition: bigdecimal.c:3726
VpSzMantissa
VP_EXPORT void VpSzMantissa(Real *a, char *psz)
Definition: bigdecimal.c:5002
Real
Definition: bigdecimal.h:136
int_chr
static VALUE int_chr(int argc, VALUE *argv, VALUE num)
Definition: numeric.c:2451
Real::flag
short flag
Definition: bigdecimal.h:155
VpFree
VP_EXPORT void VpFree(Real *pv)
Definition: bigdecimal.c:3292
VpAlloc
VP_EXPORT Real * VpAlloc(size_t mx, const char *szVal)
Definition: bigdecimal.c:3776
VpToSpecialString
VP_EXPORT int VpToSpecialString(Real *a, char *psz, int fPlus)
rmpd_base_value
static BDIGIT rmpd_base_value(void)
Definition: bigdecimal.h:171
VpInit
VP_EXPORT size_t VpInit(BDIGIT BaseVal)
Definition: bigdecimal.c:3695
BDIGIT
#define BDIGIT
Definition: ripper.y:184
Real::sign
short sign
Definition: bigdecimal.h:145
b
Real * b
Definition: bigdecimal.c:1182
ptr
return ptr
Definition: tcltklib.c:784
VpGetDoubleNegInf
VP_EXPORT double VpGetDoubleNegInf(void)
Definition: bigdecimal.c:3489
c
VpDivd * c
Definition: bigdecimal.c:1205
VpAsgn
VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw)
Definition: bigdecimal.c:3929
nf
nf
Definition: bigdecimal.c:5659
rb_cBigDecimal
VALUE rb_cBigDecimal
Definition: bigdecimal.c:41
rmpd_double_figures
static size_t rmpd_double_figures(void)
Definition: bigdecimal.h:175
f
#define f
VpAddSub
VP_EXPORT size_t VpAddSub(Real *c, Real *a, Real *b, int operation)
Definition: bigdecimal.c:3971
klass
klass
Definition: tcltklib.c:3504
VpLeftRound
VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf)
VpPower
VP_EXPORT int VpPower(Real *y, Real *x, SIGNED_VALUE n)
Definition: bigdecimal.c:5961
SIGNED_VALUE
#define SIGNED_VALUE
FLEXIBLE_ARRAY_SIZE
#define FLEXIBLE_ARRAY_SIZE
Definition: bigdecimal.h:129
VpSetPrecLimit
VP_EXPORT size_t VpSetPrecLimit(size_t n)
Definition: bigdecimal.c:3373
e
BDIGIT e
Definition: bigdecimal.c:5085
VALUE
unsigned long VALUE
Definition: ripper.y:104
VpNumOfChars
VP_EXPORT size_t VpNumOfChars(Real *vp, const char *pszFmt)
Definition: bigdecimal.c:3651
v
BDIGIT v
Definition: bigdecimal.c:5656
VpGetDoubleNegZero
VP_EXPORT double VpGetDoubleNegZero(void)
Definition: bigdecimal.c:3497
VpMemRealloc
VP_EXPORT void * VpMemRealloc(void *ptr, size_t mb)
Definition: bigdecimal.c:3282
VpExponent10
VP_EXPORT ssize_t VpExponent10(Real *a)
Definition: bigdecimal.c:4985
Real::MaxPrec
size_t MaxPrec
Definition: bigdecimal.h:138
VpSqrt
VP_EXPORT int VpSqrt(Real *y, Real *x)
Definition: bigdecimal.c:5535