00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef RUBY_BIG_DECIMAL_H
00017 #define RUBY_BIG_DECIMAL_H 1
00018
00019 #include "ruby/ruby.h"
00020 #include <float.h>
00021
00022 #if defined(__cplusplus)
00023 extern "C" {
00024 #endif
00025
00026 #ifndef HAVE_LABS
00027 static inline long
00028 labs(long const x)
00029 {
00030 if (x < 0) return -x;
00031 return x;
00032 }
00033 #endif
00034
00035 #ifndef HAVE_LLABS
00036 static inline LONG_LONG
00037 llabs(LONG_LONG const x)
00038 {
00039 if (x < 0) return -x;
00040 return x;
00041 }
00042 #endif
00043
00044 #ifdef vabs
00045 # undef vabs
00046 #endif
00047 #if SIZEOF_VALUE <= SIZEOF_INT
00048 # define vabs abs
00049 #elif SIZEOF_VALUE <= SIZEOF_LONG
00050 # define vabs labs
00051 #elif SIZEOF_VALUE <= SIZEOF_LONG_LONG
00052 # define vabs llabs
00053 #endif
00054
00055 extern VALUE rb_cBigDecimal;
00056
00057 #if 0 || SIZEOF_BDIGITS >= 16
00058 # define RMPD_COMPONENT_FIGURES 38
00059 # define RMPD_BASE ((BDIGIT)100000000000000000000000000000000000000U)
00060 #elif SIZEOF_BDIGITS >= 8
00061 # define RMPD_COMPONENT_FIGURES 19
00062 # define RMPD_BASE ((BDIGIT)10000000000000000000U)
00063 #elif SIZEOF_BDIGITS >= 4
00064 # define RMPD_COMPONENT_FIGURES 9
00065 # define RMPD_BASE ((BDIGIT)1000000000U)
00066 #elif SIZEOF_BDIGITS >= 2
00067 # define RMPD_COMPONENT_FIGURES 4
00068 # define RMPD_BASE ((BDIGIT)10000U)
00069 #else
00070 # define RMPD_COMPONENT_FIGURES 2
00071 # define RMPD_BASE ((BDIGIT)100U)
00072 #endif
00073
00074
00075
00076
00077
00078 #define SZ_NaN "NaN"
00079 #define SZ_INF "Infinity"
00080 #define SZ_PINF "+Infinity"
00081 #define SZ_NINF "-Infinity"
00082
00083
00084
00085
00086
00087 #define VP_EXPORT static
00088
00089
00090 #define VP_EXCEPTION_ALL ((unsigned short)0x00FF)
00091 #define VP_EXCEPTION_INFINITY ((unsigned short)0x0001)
00092 #define VP_EXCEPTION_NaN ((unsigned short)0x0002)
00093 #define VP_EXCEPTION_UNDERFLOW ((unsigned short)0x0004)
00094 #define VP_EXCEPTION_OVERFLOW ((unsigned short)0x0001)
00095 #define VP_EXCEPTION_ZERODIVIDE ((unsigned short)0x0010)
00096
00097
00098 #define VP_EXCEPTION_OP ((unsigned short)0x0020)
00099 #define VP_EXCEPTION_MEMORY ((unsigned short)0x0040)
00100
00101 #define RMPD_EXCEPTION_MODE_DEFAULT 0U
00102
00103
00104 #define VP_ROUND_MODE ((unsigned short)0x0100)
00105 #define VP_ROUND_UP 1
00106 #define VP_ROUND_DOWN 2
00107 #define VP_ROUND_HALF_UP 3
00108 #define VP_ROUND_HALF_DOWN 4
00109 #define VP_ROUND_CEIL 5
00110 #define VP_ROUND_FLOOR 6
00111 #define VP_ROUND_HALF_EVEN 7
00112
00113 #define RMPD_ROUNDING_MODE_DEFAULT VP_ROUND_HALF_UP
00114
00115 #define VP_SIGN_NaN 0
00116 #define VP_SIGN_POSITIVE_ZERO 1
00117 #define VP_SIGN_NEGATIVE_ZERO -1
00118 #define VP_SIGN_POSITIVE_FINITE 2
00119 #define VP_SIGN_NEGATIVE_FINITE -2
00120 #define VP_SIGN_POSITIVE_INFINITE 3
00121 #define VP_SIGN_NEGATIVE_INFINITE -3
00122
00123
00124
00125
00126
00127 typedef struct {
00128 VALUE obj;
00129 size_t MaxPrec;
00130
00131
00132 size_t Prec;
00133
00134
00135 SIGNED_VALUE exponent;
00136 short sign;
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 short flag;
00147 BDIGIT frac[1];
00148 } Real;
00149
00150
00151
00152
00153
00154
00155
00156 VP_EXPORT Real *
00157 VpNewRbClass(size_t mx, char const *str, VALUE klass);
00158
00159 VP_EXPORT Real *VpCreateRbObject(size_t mx,const char *str);
00160
00161 static inline BDIGIT
00162 rmpd_base_value(void) { return RMPD_BASE; }
00163 static inline size_t
00164 rmpd_component_figures(void) { return RMPD_COMPONENT_FIGURES; }
00165 static inline size_t
00166 rmpd_double_figures(void) { return 1+DBL_DIG; }
00167
00168 #define VpBaseFig() rmpd_component_figures()
00169 #define VpDblFig() rmpd_double_figures()
00170 #define VpBaseVal() rmpd_base_value()
00171
00172
00173 VP_EXPORT double VpGetDoubleNaN(void);
00174 VP_EXPORT double VpGetDoublePosInf(void);
00175 VP_EXPORT double VpGetDoubleNegInf(void);
00176 VP_EXPORT double VpGetDoubleNegZero(void);
00177
00178
00179 VP_EXPORT size_t VpGetPrecLimit(void);
00180 VP_EXPORT size_t VpSetPrecLimit(size_t n);
00181
00182
00183 VP_EXPORT int VpIsRoundMode(unsigned short n);
00184 VP_EXPORT unsigned short VpGetRoundMode(void);
00185 VP_EXPORT unsigned short VpSetRoundMode(unsigned short n);
00186
00187 VP_EXPORT int VpException(unsigned short f,const char *str,int always);
00188 #if 0
00189 VP_EXPORT int VpIsNegDoubleZero(double v);
00190 #endif
00191 VP_EXPORT size_t VpNumOfChars(Real *vp,const char *pszFmt);
00192 VP_EXPORT size_t VpInit(BDIGIT BaseVal);
00193 VP_EXPORT void *VpMemAlloc(size_t mb);
00194 VP_EXPORT void VpFree(Real *pv);
00195 VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal);
00196 VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw);
00197 VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation);
00198 VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);
00199 VP_EXPORT size_t VpDivd(Real *c,Real *r,Real *a,Real *b);
00200 VP_EXPORT int VpComp(Real *a,Real *b);
00201 VP_EXPORT ssize_t VpExponent10(Real *a);
00202 VP_EXPORT void VpSzMantissa(Real *a,char *psz);
00203 VP_EXPORT int VpToSpecialString(Real *a,char *psz,int fPlus);
00204 VP_EXPORT void VpToString(Real *a, char *psz, size_t fFmt, int fPlus);
00205 VP_EXPORT void VpToFString(Real *a, char *psz, size_t fFmt, int fPlus);
00206 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);
00207 VP_EXPORT int VpVtoD(double *d, SIGNED_VALUE *e, Real *m);
00208 VP_EXPORT void VpDtoV(Real *m,double d);
00209 #if 0
00210 VP_EXPORT void VpItoV(Real *m,S_INT ival);
00211 #endif
00212 VP_EXPORT int VpSqrt(Real *y,Real *x);
00213 VP_EXPORT int VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t il);
00214 VP_EXPORT int VpMidRound(Real *y, unsigned short f, ssize_t nf);
00215 VP_EXPORT int VpLeftRound(Real *y, unsigned short f, ssize_t nf);
00216 VP_EXPORT void VpFrac(Real *y, Real *x);
00217 VP_EXPORT int VpPower(Real *y, Real *x, SIGNED_VALUE n);
00218
00219
00220 VP_EXPORT Real *VpOne(void);
00221
00222
00223
00224
00225
00226
00227 #define Abs(a) (((a)>= 0)?(a):(-(a)))
00228 #define Max(a, b) (((a)>(b))?(a):(b))
00229 #define Min(a, b) (((a)>(b))?(b):(a))
00230
00231 #define VpMaxPrec(a) ((a)->MaxPrec)
00232 #define VpPrec(a) ((a)->Prec)
00233 #define VpGetFlag(a) ((a)->flag)
00234
00235
00236
00237
00238 #define VpGetSign(a) (((a)->sign>0)?1:(-1))
00239
00240 #define VpChangeSign(a,s) {if((s)>0) (a)->sign=(short)Abs((ssize_t)(a)->sign);else (a)->sign=-(short)Abs((ssize_t)(a)->sign);}
00241
00242 #define VpSetSign(a,s) {if((s)>0) (a)->sign=(short)VP_SIGN_POSITIVE_FINITE;else (a)->sign=(short)VP_SIGN_NEGATIVE_FINITE;}
00243
00244
00245 #define VpSetOne(a) {(a)->Prec=(a)->exponent=(a)->frac[0]=1;(a)->sign=VP_SIGN_POSITIVE_FINITE;}
00246
00247
00248 #define VpIsPosZero(a) ((a)->sign==VP_SIGN_POSITIVE_ZERO)
00249 #define VpIsNegZero(a) ((a)->sign==VP_SIGN_NEGATIVE_ZERO)
00250 #define VpIsZero(a) (VpIsPosZero(a) || VpIsNegZero(a))
00251 #define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_ZERO)
00252 #define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
00253 #define VpSetZero(a,s) ( ((s)>0)?VpSetPosZero(a):VpSetNegZero(a) )
00254
00255
00256 #define VpIsNaN(a) ((a)->sign==VP_SIGN_NaN)
00257 #define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NaN)
00258
00259
00260 #define VpIsPosInf(a) ((a)->sign==VP_SIGN_POSITIVE_INFINITE)
00261 #define VpIsNegInf(a) ((a)->sign==VP_SIGN_NEGATIVE_INFINITE)
00262 #define VpIsInf(a) (VpIsPosInf(a) || VpIsNegInf(a))
00263 #define VpIsDef(a) ( !(VpIsNaN(a)||VpIsInf(a)) )
00264 #define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
00265 #define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
00266 #define VpSetInf(a,s) ( ((s)>0)?VpSetPosInf(a):VpSetNegInf(a) )
00267 #define VpHasVal(a) (a->frac[0])
00268 #define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
00269 #define VpExponent(a) (a->exponent)
00270 #ifdef BIGDECIMAL_DEBUG
00271 int VpVarCheck(Real * v);
00272 VP_EXPORT int VPrint(FILE *fp,const char *cntl_chr,Real *a);
00273 #endif
00274
00275 #if defined(__cplusplus)
00276 }
00277 #endif
00278 #endif
00279