PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/bigdecimal/bigdecimal.c

https://github.com/nazy/ruby
C | 5053 lines | 3569 code | 423 blank | 1061 comment | 737 complexity | a241cd5abe75ccc357df5e107464434b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0, 0BSD, Unlicense
  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. * NOTE: Change log in this source removed to reduce source code size.
  12. * See rev. 1.25 if needed.
  13. *
  14. */
  15. /* #define BIGDECIMAL_DEBUG 1 */
  16. #include "bigdecimal.h"
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <math.h>
  23. #include "math.h"
  24. #ifdef HAVE_IEEEFP_H
  25. #include <ieeefp.h>
  26. #endif
  27. /* #define ENABLE_NUMERIC_STRING */
  28. VALUE rb_cBigDecimal;
  29. static ID id_BigDecimal_exception_mode;
  30. static ID id_BigDecimal_rounding_mode;
  31. static ID id_BigDecimal_precision_limit;
  32. static ID id_up;
  33. static ID id_down;
  34. static ID id_truncate;
  35. static ID id_half_up;
  36. static ID id_default;
  37. static ID id_half_down;
  38. static ID id_half_even;
  39. static ID id_banker;
  40. static ID id_ceiling;
  41. static ID id_ceil;
  42. static ID id_floor;
  43. /* MACRO's to guard objects from GC by keeping them in stack */
  44. #define ENTER(n) volatile VALUE vStack[n];int iStack=0
  45. #define PUSH(x) vStack[iStack++] = (unsigned long)(x);
  46. #define SAVE(p) PUSH(p->obj);
  47. #define GUARD_OBJ(p,y) {p=y;SAVE(p);}
  48. #define BASE_FIG RMPD_COMPONENT_FIGURES
  49. #define BASE RMPD_BASE
  50. #define HALF_BASE (BASE/2)
  51. #define BASE1 (BASE/10)
  52. #ifndef DBLE_FIG
  53. #define DBLE_FIG (DBL_DIG+1) /* figure of double */
  54. #endif
  55. /*
  56. * ================== Ruby Interface part ==========================
  57. */
  58. #define DoSomeOne(x,y,f) rb_num_coerce_bin(x,y,f)
  59. /*
  60. * Returns the BigDecimal version number.
  61. *
  62. * Ruby 1.8.0 returns 1.0.0.
  63. * Ruby 1.8.1 thru 1.8.3 return 1.0.1.
  64. */
  65. static VALUE
  66. BigDecimal_version(VALUE self)
  67. {
  68. /*
  69. * 1.0.0: Ruby 1.8.0
  70. * 1.0.1: Ruby 1.8.1
  71. */
  72. return rb_str_new2("1.0.1");
  73. }
  74. /*
  75. * VP routines used in BigDecimal part
  76. */
  77. static unsigned short VpGetException(void);
  78. static void VpSetException(unsigned short f);
  79. static void VpInternalRound(Real *c, size_t ixDigit, BDIGIT vPrev, BDIGIT v);
  80. static int VpLimitRound(Real *c, size_t ixDigit);
  81. /*
  82. * **** BigDecimal part ****
  83. */
  84. static void
  85. BigDecimal_delete(void *pv)
  86. {
  87. VpFree(pv);
  88. }
  89. static size_t
  90. BigDecimal_memsize(const void *ptr)
  91. {
  92. const Real *pv = ptr;
  93. return pv ? (sizeof(*pv) + pv->MaxPrec * sizeof(BDIGIT)) : 0;
  94. }
  95. static const rb_data_type_t BigDecimal_data_type = {
  96. "BigDecimal",
  97. {0, BigDecimal_delete, BigDecimal_memsize,},
  98. };
  99. static VALUE
  100. ToValue(Real *p)
  101. {
  102. if(VpIsNaN(p)) {
  103. VpException(VP_EXCEPTION_NaN,"Computation results to 'NaN'(Not a Number)",0);
  104. } else if(VpIsPosInf(p)) {
  105. VpException(VP_EXCEPTION_INFINITY,"Computation results to 'Infinity'",0);
  106. } else if(VpIsNegInf(p)) {
  107. VpException(VP_EXCEPTION_INFINITY,"Computation results to '-Infinity'",0);
  108. }
  109. return p->obj;
  110. }
  111. static Real *
  112. GetVpValue(VALUE v, int must)
  113. {
  114. Real *pv;
  115. VALUE bg;
  116. char szD[128];
  117. VALUE orig = Qundef;
  118. int util_loaded = 0;
  119. again:
  120. switch(TYPE(v))
  121. {
  122. case T_RATIONAL:
  123. if(orig == Qundef ? (orig = v, 1) : orig != v) {
  124. if(!util_loaded) {
  125. rb_require("bigdecimal/util");
  126. util_loaded = 1;
  127. }
  128. v = rb_funcall2(v, rb_intern("to_d"), 0, 0);
  129. goto again;
  130. }
  131. v = orig;
  132. goto SomeOneMayDoIt;
  133. case T_DATA:
  134. if(rb_typeddata_is_kind_of(v, &BigDecimal_data_type)) {
  135. pv = DATA_PTR(v);
  136. return pv;
  137. } else {
  138. goto SomeOneMayDoIt;
  139. }
  140. break;
  141. case T_FIXNUM:
  142. sprintf(szD, "%ld", FIX2LONG(v));
  143. return VpCreateRbObject(VpBaseFig() * 2 + 1, szD);
  144. #ifdef ENABLE_NUMERIC_STRING
  145. case T_STRING:
  146. SafeStringValue(v);
  147. return VpCreateRbObject(strlen(RSTRING_PTR(v)) + VpBaseFig() + 1,
  148. RSTRING_PTR(v));
  149. #endif /* ENABLE_NUMERIC_STRING */
  150. case T_BIGNUM:
  151. bg = rb_big2str(v, 10);
  152. return VpCreateRbObject(strlen(RSTRING_PTR(bg)) + VpBaseFig() + 1,
  153. RSTRING_PTR(bg));
  154. default:
  155. goto SomeOneMayDoIt;
  156. }
  157. SomeOneMayDoIt:
  158. if(must) {
  159. rb_raise(rb_eTypeError, "%s can't be coerced into BigDecimal",
  160. rb_special_const_p(v)?
  161. RSTRING_PTR(rb_inspect(v)):
  162. rb_obj_classname(v)
  163. );
  164. }
  165. return NULL; /* NULL means to coerce */
  166. }
  167. /* call-seq:
  168. * BigDecimal.double_fig
  169. *
  170. * The BigDecimal.double_fig class method returns the number of digits a
  171. * Float number is allowed to have. The result depends upon the CPU and OS
  172. * in use.
  173. */
  174. static VALUE
  175. BigDecimal_double_fig(VALUE self)
  176. {
  177. return INT2FIX(VpDblFig());
  178. }
  179. /* call-seq:
  180. * precs
  181. *
  182. * Returns an Array of two Integer values.
  183. *
  184. * The first value is the current number of significant digits in the
  185. * BigDecimal. The second value is the maximum number of significant digits
  186. * for the BigDecimal.
  187. */
  188. static VALUE
  189. BigDecimal_prec(VALUE self)
  190. {
  191. ENTER(1);
  192. Real *p;
  193. VALUE obj;
  194. GUARD_OBJ(p,GetVpValue(self,1));
  195. obj = rb_assoc_new(INT2NUM(p->Prec*VpBaseFig()),
  196. INT2NUM(p->MaxPrec*VpBaseFig()));
  197. return obj;
  198. }
  199. static VALUE
  200. BigDecimal_hash(VALUE self)
  201. {
  202. ENTER(1);
  203. Real *p;
  204. st_index_t hash;
  205. GUARD_OBJ(p,GetVpValue(self,1));
  206. hash = (st_index_t)p->sign;
  207. /* hash!=2: the case for 0(1),NaN(0) or +-Infinity(3) is sign itself */
  208. if(hash == 2 || hash == (st_index_t)-2) {
  209. hash ^= rb_memhash(p->frac, sizeof(BDIGIT)*p->Prec);
  210. hash += p->exponent;
  211. }
  212. return INT2FIX(hash);
  213. }
  214. static VALUE
  215. BigDecimal_dump(int argc, VALUE *argv, VALUE self)
  216. {
  217. ENTER(5);
  218. Real *vp;
  219. char *psz;
  220. VALUE dummy;
  221. volatile VALUE dump;
  222. rb_scan_args(argc, argv, "01", &dummy);
  223. GUARD_OBJ(vp,GetVpValue(self,1));
  224. dump = rb_str_new(0,VpNumOfChars(vp,"E")+50);
  225. psz = RSTRING_PTR(dump);
  226. sprintf(psz, "%"PRIuSIZE":", VpMaxPrec(vp)*VpBaseFig());
  227. VpToString(vp, psz+strlen(psz), 0, 0);
  228. rb_str_resize(dump, strlen(psz));
  229. return dump;
  230. }
  231. /*
  232. * Internal method used to provide marshalling support. See the Marshal module.
  233. */
  234. static VALUE
  235. BigDecimal_load(VALUE self, VALUE str)
  236. {
  237. ENTER(2);
  238. Real *pv;
  239. unsigned char *pch;
  240. unsigned char ch;
  241. unsigned long m=0;
  242. SafeStringValue(str);
  243. pch = (unsigned char *)RSTRING_PTR(str);
  244. /* First get max prec */
  245. while((*pch)!=(unsigned char)'\0' && (ch=*pch++)!=(unsigned char)':') {
  246. if(!ISDIGIT(ch)) {
  247. rb_raise(rb_eTypeError, "load failed: invalid character in the marshaled string");
  248. }
  249. m = m*10 + (unsigned long)(ch-'0');
  250. }
  251. if(m>VpBaseFig()) m -= VpBaseFig();
  252. GUARD_OBJ(pv,VpNewRbClass(m,(char *)pch,self));
  253. m /= VpBaseFig();
  254. if(m && pv->MaxPrec>m) pv->MaxPrec = m+1;
  255. return ToValue(pv);
  256. }
  257. static unsigned short
  258. check_rounding_mode(VALUE const v)
  259. {
  260. unsigned short sw;
  261. ID id;
  262. switch (TYPE(v)) {
  263. case T_SYMBOL:
  264. id = SYM2ID(v);
  265. if (id == id_up)
  266. return VP_ROUND_UP;
  267. if (id == id_down || id == id_truncate)
  268. return VP_ROUND_DOWN;
  269. if (id == id_half_up || id == id_default)
  270. return VP_ROUND_HALF_UP;
  271. if (id == id_half_down)
  272. return VP_ROUND_HALF_DOWN;
  273. if (id == id_half_even || id == id_banker)
  274. return VP_ROUND_HALF_EVEN;
  275. if (id == id_ceiling || id == id_ceil)
  276. return VP_ROUND_CEIL;
  277. if (id == id_floor)
  278. return VP_ROUND_FLOOR;
  279. rb_raise(rb_eArgError, "invalid rounding mode");
  280. default:
  281. break;
  282. }
  283. Check_Type(v, T_FIXNUM);
  284. sw = (unsigned short)FIX2UINT(v);
  285. if (!VpIsRoundMode(sw)) {
  286. rb_raise(rb_eArgError, "invalid rounding mode");
  287. }
  288. return sw;
  289. }
  290. /* call-seq:
  291. * BigDecimal.mode(mode, value)
  292. *
  293. * Controls handling of arithmetic exceptions and rounding. If no value
  294. * is supplied, the current value is returned.
  295. *
  296. * Six values of the mode parameter control the handling of arithmetic
  297. * exceptions:
  298. *
  299. * BigDecimal::EXCEPTION_NaN
  300. * BigDecimal::EXCEPTION_INFINITY
  301. * BigDecimal::EXCEPTION_UNDERFLOW
  302. * BigDecimal::EXCEPTION_OVERFLOW
  303. * BigDecimal::EXCEPTION_ZERODIVIDE
  304. * BigDecimal::EXCEPTION_ALL
  305. *
  306. * For each mode parameter above, if the value set is false, computation
  307. * continues after an arithmetic exception of the appropriate type.
  308. * When computation continues, results are as follows:
  309. *
  310. * EXCEPTION_NaN:: NaN
  311. * EXCEPTION_INFINITY:: +infinity or -infinity
  312. * EXCEPTION_UNDERFLOW:: 0
  313. * EXCEPTION_OVERFLOW:: +infinity or -infinity
  314. * EXCEPTION_ZERODIVIDE:: +infinity or -infinity
  315. *
  316. * One value of the mode parameter controls the rounding of numeric values:
  317. * BigDecimal::ROUND_MODE. The values it can take are:
  318. *
  319. * ROUND_UP, :up:: round away from zero
  320. * ROUND_DOWN, :down, :truncate:: round towards zero (truncate)
  321. * ROUND_HALF_UP, :half_up, :default:: round towards the nearest neighbor, unless both neighbors are equidistant, in which case round away from zero. (default)
  322. * ROUND_HALF_DOWN, :half_down:: round towards the nearest neighbor, unless both neighbors are equidistant, in which case round towards zero.
  323. * ROUND_HALF_EVEN, :half_even, :banker:: round towards the nearest neighbor, unless both neighbors are equidistant, in which case round towards the even neighbor (Banker's rounding)
  324. * ROUND_CEILING, :ceiling, :ceil:: round towards positive infinity (ceil)
  325. * ROUND_FLOOR, :floor:: round towards negative infinity (floor)
  326. *
  327. */
  328. static VALUE
  329. BigDecimal_mode(int argc, VALUE *argv, VALUE self)
  330. {
  331. VALUE which;
  332. VALUE val;
  333. unsigned long f,fo;
  334. if(rb_scan_args(argc,argv,"11",&which,&val)==1) val = Qnil;
  335. Check_Type(which, T_FIXNUM);
  336. f = (unsigned long)FIX2INT(which);
  337. if(f&VP_EXCEPTION_ALL) {
  338. /* Exception mode setting */
  339. fo = VpGetException();
  340. if(val==Qnil) return INT2FIX(fo);
  341. if(val!=Qfalse && val!=Qtrue) {
  342. rb_raise(rb_eArgError, "second argument must be true or false");
  343. return Qnil; /* Not reached */
  344. }
  345. if(f&VP_EXCEPTION_INFINITY) {
  346. VpSetException((unsigned short)((val==Qtrue)?(fo|VP_EXCEPTION_INFINITY):
  347. (fo&(~VP_EXCEPTION_INFINITY))));
  348. }
  349. fo = VpGetException();
  350. if(f&VP_EXCEPTION_NaN) {
  351. VpSetException((unsigned short)((val==Qtrue)?(fo|VP_EXCEPTION_NaN):
  352. (fo&(~VP_EXCEPTION_NaN))));
  353. }
  354. fo = VpGetException();
  355. if(f&VP_EXCEPTION_UNDERFLOW) {
  356. VpSetException((unsigned short)((val==Qtrue)?(fo|VP_EXCEPTION_UNDERFLOW):
  357. (fo&(~VP_EXCEPTION_UNDERFLOW))));
  358. }
  359. fo = VpGetException();
  360. if(f&VP_EXCEPTION_ZERODIVIDE) {
  361. VpSetException((unsigned short)((val==Qtrue)?(fo|VP_EXCEPTION_ZERODIVIDE):
  362. (fo&(~VP_EXCEPTION_ZERODIVIDE))));
  363. }
  364. fo = VpGetException();
  365. return INT2FIX(fo);
  366. }
  367. if (VP_ROUND_MODE == f) {
  368. /* Rounding mode setting */
  369. unsigned short sw;
  370. fo = VpGetRoundMode();
  371. if (NIL_P(val)) return INT2FIX(fo);
  372. sw = check_rounding_mode(val);
  373. fo = VpSetRoundMode(sw);
  374. return INT2FIX(fo);
  375. }
  376. rb_raise(rb_eTypeError, "first argument for BigDecimal#mode invalid");
  377. return Qnil;
  378. }
  379. static size_t
  380. GetAddSubPrec(Real *a, Real *b)
  381. {
  382. size_t mxs;
  383. size_t mx = a->Prec;
  384. SIGNED_VALUE d;
  385. if(!VpIsDef(a) || !VpIsDef(b)) return (size_t)-1L;
  386. if(mx < b->Prec) mx = b->Prec;
  387. if(a->exponent!=b->exponent) {
  388. mxs = mx;
  389. d = a->exponent - b->exponent;
  390. if (d < 0) d = -d;
  391. mx = mx + (size_t)d;
  392. if (mx<mxs) {
  393. return VpException(VP_EXCEPTION_INFINITY,"Exponent overflow",0);
  394. }
  395. }
  396. return mx;
  397. }
  398. static SIGNED_VALUE
  399. GetPositiveInt(VALUE v)
  400. {
  401. SIGNED_VALUE n;
  402. Check_Type(v, T_FIXNUM);
  403. n = FIX2INT(v);
  404. if (n < 0) {
  405. rb_raise(rb_eArgError, "argument must be positive");
  406. }
  407. return n;
  408. }
  409. VP_EXPORT Real *
  410. VpNewRbClass(size_t mx, char *str, VALUE klass)
  411. {
  412. Real *pv = VpAlloc(mx,str);
  413. pv->obj = TypedData_Wrap_Struct(klass, &BigDecimal_data_type, pv);
  414. return pv;
  415. }
  416. VP_EXPORT Real *
  417. VpCreateRbObject(size_t mx, const char *str)
  418. {
  419. Real *pv = VpAlloc(mx,str);
  420. pv->obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, pv);
  421. return pv;
  422. }
  423. /* Returns True if the value is Not a Number */
  424. static VALUE
  425. BigDecimal_IsNaN(VALUE self)
  426. {
  427. Real *p = GetVpValue(self,1);
  428. if(VpIsNaN(p)) return Qtrue;
  429. return Qfalse;
  430. }
  431. /* Returns nil, -1, or +1 depending on whether the value is finite,
  432. * -infinity, or +infinity.
  433. */
  434. static VALUE
  435. BigDecimal_IsInfinite(VALUE self)
  436. {
  437. Real *p = GetVpValue(self,1);
  438. if(VpIsPosInf(p)) return INT2FIX(1);
  439. if(VpIsNegInf(p)) return INT2FIX(-1);
  440. return Qnil;
  441. }
  442. /* Returns True if the value is finite (not NaN or infinite) */
  443. static VALUE
  444. BigDecimal_IsFinite(VALUE self)
  445. {
  446. Real *p = GetVpValue(self,1);
  447. if(VpIsNaN(p)) return Qfalse;
  448. if(VpIsInf(p)) return Qfalse;
  449. return Qtrue;
  450. }
  451. static void
  452. BigDecimal_check_num(Real *p)
  453. {
  454. if(VpIsNaN(p)) {
  455. VpException(VP_EXCEPTION_NaN,"Computation results to 'NaN'(Not a Number)",1);
  456. } else if(VpIsPosInf(p)) {
  457. VpException(VP_EXCEPTION_INFINITY,"Computation results to 'Infinity'",1);
  458. } else if(VpIsNegInf(p)) {
  459. VpException(VP_EXCEPTION_INFINITY,"Computation results to '-Infinity'",1);
  460. }
  461. }
  462. static VALUE BigDecimal_split(VALUE self);
  463. /* Returns the value as an integer (Fixnum or Bignum).
  464. *
  465. * If the BigNumber is infinity or NaN, raises FloatDomainError.
  466. */
  467. static VALUE
  468. BigDecimal_to_i(VALUE self)
  469. {
  470. ENTER(5);
  471. ssize_t e, nf;
  472. Real *p;
  473. GUARD_OBJ(p,GetVpValue(self,1));
  474. BigDecimal_check_num(p);
  475. e = VpExponent10(p);
  476. if(e<=0) return INT2FIX(0);
  477. nf = VpBaseFig();
  478. if(e<=nf) {
  479. return LONG2NUM((long)(VpGetSign(p)*(BDIGIT_DBL_SIGNED)p->frac[0]));
  480. }
  481. else {
  482. VALUE a = BigDecimal_split(self);
  483. VALUE digits = RARRAY_PTR(a)[1];
  484. VALUE numerator = rb_funcall(digits, rb_intern("to_i"), 0);
  485. ssize_t dpower = e - (ssize_t)RSTRING_LEN(digits);
  486. if (VpGetSign(p) < 0) {
  487. numerator = rb_funcall(numerator, '*', 1, INT2FIX(-1));
  488. }
  489. if (dpower < 0) {
  490. return rb_funcall(numerator, rb_intern("div"), 1,
  491. rb_funcall(INT2FIX(10), rb_intern("**"), 1,
  492. INT2FIX(-dpower)));
  493. }
  494. return rb_funcall(numerator, '*', 1,
  495. rb_funcall(INT2FIX(10), rb_intern("**"), 1,
  496. INT2FIX(dpower)));
  497. }
  498. }
  499. /* Returns a new Float object having approximately the same value as the
  500. * BigDecimal number. Normal accuracy limits and built-in errors of binary
  501. * Float arithmetic apply.
  502. */
  503. static VALUE
  504. BigDecimal_to_f(VALUE self)
  505. {
  506. ENTER(1);
  507. Real *p;
  508. double d;
  509. SIGNED_VALUE e;
  510. char *buf;
  511. volatile VALUE str;
  512. GUARD_OBJ(p, GetVpValue(self, 1));
  513. if (VpVtoD(&d, &e, p) != 1)
  514. return rb_float_new(d);
  515. if (e > (SIGNED_VALUE)(DBL_MAX_10_EXP+BASE_FIG))
  516. goto overflow;
  517. if (e < (SIGNED_VALUE)(DBL_MIN_10_EXP-BASE_FIG))
  518. goto underflow;
  519. str = rb_str_new(0, VpNumOfChars(p,"E"));
  520. buf = RSTRING_PTR(str);
  521. VpToString(p, buf, 0, 0);
  522. errno = 0;
  523. d = strtod(buf, 0);
  524. if (errno == ERANGE)
  525. goto overflow;
  526. return rb_float_new(d);
  527. overflow:
  528. VpException(VP_EXCEPTION_OVERFLOW, "BigDecimal to Float conversion", 0);
  529. if (d > 0.0)
  530. return rb_float_new(VpGetDoublePosInf());
  531. else
  532. return rb_float_new(VpGetDoubleNegInf());
  533. underflow:
  534. VpException(VP_EXCEPTION_UNDERFLOW, "BigDecimal to Float conversion", 0);
  535. if (d > 0.0)
  536. return rb_float_new(0.0);
  537. else
  538. return rb_float_new(-0.0);
  539. }
  540. /* Converts a BigDecimal to a Rational.
  541. */
  542. static VALUE
  543. BigDecimal_to_r(VALUE self)
  544. {
  545. Real *p;
  546. ssize_t sign, power, denomi_power;
  547. VALUE a, digits, numerator;
  548. p = GetVpValue(self,1);
  549. BigDecimal_check_num(p);
  550. sign = VpGetSign(p);
  551. power = VpExponent10(p);
  552. a = BigDecimal_split(self);
  553. digits = RARRAY_PTR(a)[1];
  554. denomi_power = power - RSTRING_LEN(digits);
  555. numerator = rb_funcall(digits, rb_intern("to_i"), 0);
  556. if (sign < 0) {
  557. numerator = rb_funcall(numerator, '*', 1, INT2FIX(-1));
  558. }
  559. if (denomi_power < 0) {
  560. return rb_Rational(numerator,
  561. rb_funcall(INT2FIX(10), rb_intern("**"), 1,
  562. INT2FIX(-denomi_power)));
  563. }
  564. else {
  565. return rb_Rational1(rb_funcall(numerator, '*', 1,
  566. rb_funcall(INT2FIX(10), rb_intern("**"), 1,
  567. INT2FIX(denomi_power))));
  568. }
  569. }
  570. /* The coerce method provides support for Ruby type coercion. It is not
  571. * enabled by default.
  572. *
  573. * This means that binary operations like + * / or - can often be performed
  574. * on a BigDecimal and an object of another type, if the other object can
  575. * be coerced into a BigDecimal value.
  576. *
  577. * e.g.
  578. * a = BigDecimal.new("1.0")
  579. * b = a / 2.0 -> 0.5
  580. *
  581. * Note that coercing a String to a BigDecimal is not supported by default;
  582. * it requires a special compile-time option when building Ruby.
  583. */
  584. static VALUE
  585. BigDecimal_coerce(VALUE self, VALUE other)
  586. {
  587. ENTER(2);
  588. VALUE obj;
  589. Real *b;
  590. if (TYPE(other) == T_FLOAT) {
  591. obj = rb_assoc_new(other, BigDecimal_to_f(self));
  592. } else {
  593. GUARD_OBJ(b,GetVpValue(other,1));
  594. obj = rb_assoc_new(b->obj, self);
  595. }
  596. return obj;
  597. }
  598. static VALUE
  599. BigDecimal_uplus(VALUE self)
  600. {
  601. return self;
  602. }
  603. /* call-seq:
  604. * add(value, digits)
  605. *
  606. * Add the specified value.
  607. *
  608. * e.g.
  609. * c = a.add(b,n)
  610. * c = a + b
  611. *
  612. * digits:: If specified and less than the number of significant digits of the result, the result is rounded to that number of digits, according to BigDecimal.mode.
  613. */
  614. static VALUE
  615. BigDecimal_add(VALUE self, VALUE r)
  616. {
  617. ENTER(5);
  618. Real *c, *a, *b;
  619. size_t mx;
  620. GUARD_OBJ(a,GetVpValue(self,1));
  621. b = GetVpValue(r,0);
  622. if(!b) return DoSomeOne(self,r,'+');
  623. SAVE(b);
  624. if(VpIsNaN(b)) return b->obj;
  625. if(VpIsNaN(a)) return a->obj;
  626. mx = GetAddSubPrec(a,b);
  627. if (mx == (size_t)-1L) {
  628. GUARD_OBJ(c,VpCreateRbObject(VpBaseFig() + 1, "0"));
  629. VpAddSub(c, a, b, 1);
  630. } else {
  631. GUARD_OBJ(c,VpCreateRbObject(mx *(VpBaseFig() + 1), "0"));
  632. if(!mx) {
  633. VpSetInf(c,VpGetSign(a));
  634. } else {
  635. VpAddSub(c, a, b, 1);
  636. }
  637. }
  638. return ToValue(c);
  639. }
  640. /* call-seq:
  641. * sub(value, digits)
  642. *
  643. * Subtract the specified value.
  644. *
  645. * e.g.
  646. * c = a.sub(b,n)
  647. * c = a - b
  648. *
  649. * digits:: If specified and less than the number of significant digits of the result, the result is rounded to that number of digits, according to BigDecimal.mode.
  650. */
  651. static VALUE
  652. BigDecimal_sub(VALUE self, VALUE r)
  653. {
  654. ENTER(5);
  655. Real *c, *a, *b;
  656. size_t mx;
  657. GUARD_OBJ(a,GetVpValue(self,1));
  658. b = GetVpValue(r,0);
  659. if(!b) return DoSomeOne(self,r,'-');
  660. SAVE(b);
  661. if(VpIsNaN(b)) return b->obj;
  662. if(VpIsNaN(a)) return a->obj;
  663. mx = GetAddSubPrec(a,b);
  664. if (mx == (size_t)-1L) {
  665. GUARD_OBJ(c,VpCreateRbObject(VpBaseFig() + 1, "0"));
  666. VpAddSub(c, a, b, -1);
  667. } else {
  668. GUARD_OBJ(c,VpCreateRbObject(mx *(VpBaseFig() + 1), "0"));
  669. if(!mx) {
  670. VpSetInf(c,VpGetSign(a));
  671. } else {
  672. VpAddSub(c, a, b, -1);
  673. }
  674. }
  675. return ToValue(c);
  676. }
  677. static VALUE
  678. BigDecimalCmp(VALUE self, VALUE r,char op)
  679. {
  680. ENTER(5);
  681. SIGNED_VALUE e;
  682. Real *a, *b;
  683. GUARD_OBJ(a,GetVpValue(self,1));
  684. b = GetVpValue(r,0);
  685. if(!b) {
  686. ID f = 0;
  687. switch(op)
  688. {
  689. case '*': return rb_num_coerce_cmp(self,r,rb_intern("<=>"));
  690. case '=': return RTEST(rb_num_coerce_cmp(self,r,rb_intern("=="))) ? Qtrue : Qfalse;
  691. case 'G': f = rb_intern(">="); break;
  692. case 'L': f = rb_intern("<="); break;
  693. case '>': case '<': f = (ID)op; break;
  694. }
  695. return rb_num_coerce_relop(self,r,f);
  696. }
  697. SAVE(b);
  698. e = VpComp(a, b);
  699. if(e==999) return (op == '*') ? Qnil : Qfalse;
  700. switch(op)
  701. {
  702. case '*': return INT2FIX(e); /* any op */
  703. case '=': if(e==0) return Qtrue ; return Qfalse;
  704. case 'G': if(e>=0) return Qtrue ; return Qfalse;
  705. case '>': if(e> 0) return Qtrue ; return Qfalse;
  706. case 'L': if(e<=0) return Qtrue ; return Qfalse;
  707. case '<': if(e< 0) return Qtrue ; return Qfalse;
  708. }
  709. rb_bug("Undefined operation in BigDecimalCmp()");
  710. }
  711. /* Returns True if the value is zero. */
  712. static VALUE
  713. BigDecimal_zero(VALUE self)
  714. {
  715. Real *a = GetVpValue(self,1);
  716. return VpIsZero(a) ? Qtrue : Qfalse;
  717. }
  718. /* Returns self if the value is non-zero, nil otherwise. */
  719. static VALUE
  720. BigDecimal_nonzero(VALUE self)
  721. {
  722. Real *a = GetVpValue(self,1);
  723. return VpIsZero(a) ? Qnil : self;
  724. }
  725. /* The comparison operator.
  726. * a <=> b is 0 if a == b, 1 if a > b, -1 if a < b.
  727. */
  728. static VALUE
  729. BigDecimal_comp(VALUE self, VALUE r)
  730. {
  731. return BigDecimalCmp(self, r, '*');
  732. }
  733. /*
  734. * Tests for value equality; returns true if the values are equal.
  735. *
  736. * The == and === operators and the eql? method have the same implementation
  737. * for BigDecimal.
  738. *
  739. * Values may be coerced to perform the comparison:
  740. *
  741. * BigDecimal.new('1.0') == 1.0 -> true
  742. */
  743. static VALUE
  744. BigDecimal_eq(VALUE self, VALUE r)
  745. {
  746. return BigDecimalCmp(self, r, '=');
  747. }
  748. /* call-seq:
  749. * a < b
  750. *
  751. * Returns true if a is less than b. Values may be coerced to perform the
  752. * comparison (see ==, coerce).
  753. */
  754. static VALUE
  755. BigDecimal_lt(VALUE self, VALUE r)
  756. {
  757. return BigDecimalCmp(self, r, '<');
  758. }
  759. /* call-seq:
  760. * a <= b
  761. *
  762. * Returns true if a is less than or equal to b. Values may be coerced to
  763. * perform the comparison (see ==, coerce).
  764. */
  765. static VALUE
  766. BigDecimal_le(VALUE self, VALUE r)
  767. {
  768. return BigDecimalCmp(self, r, 'L');
  769. }
  770. /* call-seq:
  771. * a > b
  772. *
  773. * Returns true if a is greater than b. Values may be coerced to
  774. * perform the comparison (see ==, coerce).
  775. */
  776. static VALUE
  777. BigDecimal_gt(VALUE self, VALUE r)
  778. {
  779. return BigDecimalCmp(self, r, '>');
  780. }
  781. /* call-seq:
  782. * a >= b
  783. *
  784. * Returns true if a is greater than or equal to b. Values may be coerced to
  785. * perform the comparison (see ==, coerce)
  786. */
  787. static VALUE
  788. BigDecimal_ge(VALUE self, VALUE r)
  789. {
  790. return BigDecimalCmp(self, r, 'G');
  791. }
  792. static VALUE
  793. BigDecimal_neg(VALUE self)
  794. {
  795. ENTER(5);
  796. Real *c, *a;
  797. GUARD_OBJ(a,GetVpValue(self,1));
  798. GUARD_OBJ(c,VpCreateRbObject(a->Prec *(VpBaseFig() + 1), "0"));
  799. VpAsgn(c, a, -1);
  800. return ToValue(c);
  801. }
  802. /* call-seq:
  803. * mult(value, digits)
  804. *
  805. * Multiply by the specified value.
  806. *
  807. * e.g.
  808. * c = a.mult(b,n)
  809. * c = a * b
  810. *
  811. * digits:: If specified and less than the number of significant digits of the result, the result is rounded to that number of digits, according to BigDecimal.mode.
  812. */
  813. static VALUE
  814. BigDecimal_mult(VALUE self, VALUE r)
  815. {
  816. ENTER(5);
  817. Real *c, *a, *b;
  818. size_t mx;
  819. GUARD_OBJ(a,GetVpValue(self,1));
  820. b = GetVpValue(r,0);
  821. if(!b) return DoSomeOne(self,r,'*');
  822. SAVE(b);
  823. mx = a->Prec + b->Prec;
  824. GUARD_OBJ(c,VpCreateRbObject(mx *(VpBaseFig() + 1), "0"));
  825. VpMult(c, a, b);
  826. return ToValue(c);
  827. }
  828. static VALUE
  829. BigDecimal_divide(Real **c, Real **res, Real **div, VALUE self, VALUE r)
  830. /* For c = self.div(r): with round operation */
  831. {
  832. ENTER(5);
  833. Real *a, *b;
  834. size_t mx;
  835. GUARD_OBJ(a,GetVpValue(self,1));
  836. b = GetVpValue(r,0);
  837. if(!b) return DoSomeOne(self,r,'/');
  838. SAVE(b);
  839. *div = b;
  840. mx = a->Prec + vabs(a->exponent);
  841. if(mx<b->Prec + vabs(b->exponent)) mx = b->Prec + vabs(b->exponent);
  842. mx =(mx + 1) * VpBaseFig();
  843. GUARD_OBJ((*c),VpCreateRbObject(mx, "#0"));
  844. GUARD_OBJ((*res),VpCreateRbObject((mx+1) * 2 +(VpBaseFig() + 1), "#0"));
  845. VpDivd(*c, *res, a, b);
  846. return (VALUE)0;
  847. }
  848. /* call-seq:
  849. * div(value, digits)
  850. * quo(value)
  851. *
  852. * Divide by the specified value.
  853. *
  854. * e.g.
  855. * c = a.div(b,n)
  856. *
  857. * digits:: If specified and less than the number of significant digits of the result, the result is rounded to that number of digits, according to BigDecimal.mode.
  858. *
  859. * If digits is 0, the result is the same as the / operator. If not, the
  860. * result is an integer BigDecimal, by analogy with Float#div.
  861. *
  862. * The alias quo is provided since div(value, 0) is the same as computing
  863. * the quotient; see divmod.
  864. */
  865. static VALUE
  866. BigDecimal_div(VALUE self, VALUE r)
  867. /* For c = self/r: with round operation */
  868. {
  869. ENTER(5);
  870. Real *c=NULL, *res=NULL, *div = NULL;
  871. r = BigDecimal_divide(&c, &res, &div, self, r);
  872. if(r!=(VALUE)0) return r; /* coerced by other */
  873. SAVE(c);SAVE(res);SAVE(div);
  874. /* a/b = c + r/b */
  875. /* c xxxxx
  876. r 00000yyyyy ==> (y/b)*BASE >= HALF_BASE
  877. */
  878. /* Round */
  879. if(VpHasVal(div)) { /* frac[0] must be zero for NaN,INF,Zero */
  880. VpInternalRound(c, 0, c->frac[c->Prec-1], (BDIGIT)(VpBaseVal()*(BDIGIT_DBL)res->frac[0]/div->frac[0]));
  881. }
  882. return ToValue(c);
  883. }
  884. /*
  885. * %: mod = a%b = a - (a.to_f/b).floor * b
  886. * div = (a.to_f/b).floor
  887. */
  888. static VALUE
  889. BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod)
  890. {
  891. ENTER(8);
  892. Real *c=NULL, *d=NULL, *res=NULL;
  893. Real *a, *b;
  894. size_t mx;
  895. GUARD_OBJ(a,GetVpValue(self,1));
  896. b = GetVpValue(r,0);
  897. if(!b) return Qfalse;
  898. SAVE(b);
  899. if(VpIsNaN(a) || VpIsNaN(b)) goto NaN;
  900. if(VpIsInf(a) && VpIsInf(b)) goto NaN;
  901. if(VpIsZero(b)) {
  902. rb_raise(rb_eZeroDivError, "divided by 0");
  903. }
  904. if(VpIsInf(a)) {
  905. GUARD_OBJ(d,VpCreateRbObject(1, "0"));
  906. VpSetInf(d, (SIGNED_VALUE)(VpGetSign(a) == VpGetSign(b) ? 1 : -1));
  907. GUARD_OBJ(c,VpCreateRbObject(1, "NaN"));
  908. *div = d;
  909. *mod = c;
  910. return Qtrue;
  911. }
  912. if(VpIsInf(b)) {
  913. GUARD_OBJ(d,VpCreateRbObject(1, "0"));
  914. *div = d;
  915. *mod = a;
  916. return Qtrue;
  917. }
  918. if(VpIsZero(a)) {
  919. GUARD_OBJ(c,VpCreateRbObject(1, "0"));
  920. GUARD_OBJ(d,VpCreateRbObject(1, "0"));
  921. *div = d;
  922. *mod = c;
  923. return Qtrue;
  924. }
  925. mx = a->Prec + vabs(a->exponent);
  926. if(mx<b->Prec + vabs(b->exponent)) mx = b->Prec + vabs(b->exponent);
  927. mx =(mx + 1) * VpBaseFig();
  928. GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
  929. GUARD_OBJ(res,VpCreateRbObject((mx+1) * 2 +(VpBaseFig() + 1), "#0"));
  930. VpDivd(c, res, a, b);
  931. mx = c->Prec *(VpBaseFig() + 1);
  932. GUARD_OBJ(d,VpCreateRbObject(mx, "0"));
  933. VpActiveRound(d,c,VP_ROUND_DOWN,0);
  934. VpMult(res,d,b);
  935. VpAddSub(c,a,res,-1);
  936. if(!VpIsZero(c) && (VpGetSign(a)*VpGetSign(b)<0)) {
  937. VpAddSub(res,d,VpOne(),-1);
  938. GUARD_OBJ(d,VpCreateRbObject(GetAddSubPrec(c, b)*(VpBaseFig() + 1), "0"));
  939. VpAddSub(d ,c,b, 1);
  940. *div = res;
  941. *mod = d;
  942. } else {
  943. *div = d;
  944. *mod = c;
  945. }
  946. return Qtrue;
  947. NaN:
  948. GUARD_OBJ(c,VpCreateRbObject(1, "NaN"));
  949. GUARD_OBJ(d,VpCreateRbObject(1, "NaN"));
  950. *div = d;
  951. *mod = c;
  952. return Qtrue;
  953. }
  954. /* call-seq:
  955. * a % b
  956. * a.modulo(b)
  957. *
  958. * Returns the modulus from dividing by b. See divmod.
  959. */
  960. static VALUE
  961. BigDecimal_mod(VALUE self, VALUE r) /* %: a%b = a - (a.to_f/b).floor * b */
  962. {
  963. ENTER(3);
  964. Real *div=NULL, *mod=NULL;
  965. if(BigDecimal_DoDivmod(self,r,&div,&mod)) {
  966. SAVE(div); SAVE(mod);
  967. return ToValue(mod);
  968. }
  969. return DoSomeOne(self,r,'%');
  970. }
  971. static VALUE
  972. BigDecimal_divremain(VALUE self, VALUE r, Real **dv, Real **rv)
  973. {
  974. ENTER(10);
  975. size_t mx;
  976. Real *a=NULL, *b=NULL, *c=NULL, *res=NULL, *d=NULL, *rr=NULL, *ff=NULL;
  977. Real *f=NULL;
  978. GUARD_OBJ(a,GetVpValue(self,1));
  979. b = GetVpValue(r,0);
  980. if(!b) return DoSomeOne(self,r,rb_intern("remainder"));
  981. SAVE(b);
  982. mx =(a->MaxPrec + b->MaxPrec) *VpBaseFig();
  983. GUARD_OBJ(c ,VpCreateRbObject(mx, "0"));
  984. GUARD_OBJ(res,VpCreateRbObject((mx+1) * 2 +(VpBaseFig() + 1), "#0"));
  985. GUARD_OBJ(rr ,VpCreateRbObject((mx+1) * 2 +(VpBaseFig() + 1), "#0"));
  986. GUARD_OBJ(ff ,VpCreateRbObject((mx+1) * 2 +(VpBaseFig() + 1), "#0"));
  987. VpDivd(c, res, a, b);
  988. mx = c->Prec *(VpBaseFig() + 1);
  989. GUARD_OBJ(d,VpCreateRbObject(mx, "0"));
  990. GUARD_OBJ(f,VpCreateRbObject(mx, "0"));
  991. VpActiveRound(d,c,VP_ROUND_DOWN,0); /* 0: round off */
  992. VpFrac(f, c);
  993. VpMult(rr,f,b);
  994. VpAddSub(ff,res,rr,1);
  995. *dv = d;
  996. *rv = ff;
  997. return (VALUE)0;
  998. }
  999. /* Returns the remainder from dividing by the value.
  1000. *
  1001. * x.remainder(y) means x-y*(x/y).truncate
  1002. */
  1003. static VALUE
  1004. BigDecimal_remainder(VALUE self, VALUE r) /* remainder */
  1005. {
  1006. VALUE f;
  1007. Real *d,*rv=0;
  1008. f = BigDecimal_divremain(self,r,&d,&rv);
  1009. if(f!=(VALUE)0) return f;
  1010. return ToValue(rv);
  1011. }
  1012. /* Divides by the specified value, and returns the quotient and modulus
  1013. * as BigDecimal numbers. The quotient is rounded towards negative infinity.
  1014. *
  1015. * For example:
  1016. *
  1017. * require 'bigdecimal'
  1018. *
  1019. * a = BigDecimal.new("42")
  1020. * b = BigDecimal.new("9")
  1021. *
  1022. * q,m = a.divmod(b)
  1023. *
  1024. * c = q * b + m
  1025. *
  1026. * a == c -> true
  1027. *
  1028. * The quotient q is (a/b).floor, and the modulus is the amount that must be
  1029. * added to q * b to get a.
  1030. */
  1031. static VALUE
  1032. BigDecimal_divmod(VALUE self, VALUE r)
  1033. {
  1034. ENTER(5);
  1035. Real *div=NULL, *mod=NULL;
  1036. if(BigDecimal_DoDivmod(self,r,&div,&mod)) {
  1037. SAVE(div); SAVE(mod);
  1038. return rb_assoc_new(ToValue(div), ToValue(mod));
  1039. }
  1040. return DoSomeOne(self,r,rb_intern("divmod"));
  1041. }
  1042. static VALUE
  1043. BigDecimal_div2(int argc, VALUE *argv, VALUE self)
  1044. {
  1045. ENTER(5);
  1046. VALUE b,n;
  1047. int na = rb_scan_args(argc,argv,"11",&b,&n);
  1048. if(na==1) { /* div in Float sense */
  1049. Real *div=NULL;
  1050. Real *mod;
  1051. if(BigDecimal_DoDivmod(self,b,&div,&mod)) {
  1052. return BigDecimal_to_i(ToValue(div));
  1053. }
  1054. return DoSomeOne(self,b,rb_intern("div"));
  1055. } else { /* div in BigDecimal sense */
  1056. SIGNED_VALUE ix = GetPositiveInt(n);
  1057. if (ix == 0) return BigDecimal_div(self, b);
  1058. else {
  1059. Real *res=NULL;
  1060. Real *av=NULL, *bv=NULL, *cv=NULL;
  1061. size_t mx = (ix+VpBaseFig()*2);
  1062. size_t pl = VpSetPrecLimit(0);
  1063. GUARD_OBJ(cv,VpCreateRbObject(mx,"0"));
  1064. GUARD_OBJ(av,GetVpValue(self,1));
  1065. GUARD_OBJ(bv,GetVpValue(b,1));
  1066. mx = av->Prec + bv->Prec + 2;
  1067. if(mx <= cv->MaxPrec) mx = cv->MaxPrec+1;
  1068. GUARD_OBJ(res,VpCreateRbObject((mx * 2 + 2)*VpBaseFig(), "#0"));
  1069. VpDivd(cv,res,av,bv);
  1070. VpSetPrecLimit(pl);
  1071. VpLeftRound(cv,(int)VpGetRoundMode(),ix);
  1072. return ToValue(cv);
  1073. }
  1074. }
  1075. }
  1076. static VALUE
  1077. BigDecimal_add2(VALUE self, VALUE b, VALUE n)
  1078. {
  1079. ENTER(2);
  1080. Real *cv;
  1081. SIGNED_VALUE mx = GetPositiveInt(n);
  1082. if (mx == 0) return BigDecimal_add(self, b);
  1083. else {
  1084. size_t pl = VpSetPrecLimit(0);
  1085. VALUE c = BigDecimal_add(self,b);
  1086. VpSetPrecLimit(pl);
  1087. GUARD_OBJ(cv,GetVpValue(c,1));
  1088. VpLeftRound(cv,(int)VpGetRoundMode(),mx);
  1089. return ToValue(cv);
  1090. }
  1091. }
  1092. static VALUE
  1093. BigDecimal_sub2(VALUE self, VALUE b, VALUE n)
  1094. {
  1095. ENTER(2);
  1096. Real *cv;
  1097. SIGNED_VALUE mx = GetPositiveInt(n);
  1098. if (mx == 0) return BigDecimal_sub(self, b);
  1099. else {
  1100. size_t pl = VpSetPrecLimit(0);
  1101. VALUE c = BigDecimal_sub(self,b);
  1102. VpSetPrecLimit(pl);
  1103. GUARD_OBJ(cv,GetVpValue(c,1));
  1104. VpLeftRound(cv,(int)VpGetRoundMode(),mx);
  1105. return ToValue(cv);
  1106. }
  1107. }
  1108. static VALUE
  1109. BigDecimal_mult2(VALUE self, VALUE b, VALUE n)
  1110. {
  1111. ENTER(2);
  1112. Real *cv;
  1113. SIGNED_VALUE mx = GetPositiveInt(n);
  1114. if (mx == 0) return BigDecimal_mult(self, b);
  1115. else {
  1116. size_t pl = VpSetPrecLimit(0);
  1117. VALUE c = BigDecimal_mult(self,b);
  1118. VpSetPrecLimit(pl);
  1119. GUARD_OBJ(cv,GetVpValue(c,1));
  1120. VpLeftRound(cv,(int)VpGetRoundMode(),mx);
  1121. return ToValue(cv);
  1122. }
  1123. }
  1124. /* Returns the absolute value.
  1125. *
  1126. * BigDecimal('5').abs -> 5
  1127. *
  1128. * BigDecimal('-3').abs -> 3
  1129. */
  1130. static VALUE
  1131. BigDecimal_abs(VALUE self)
  1132. {
  1133. ENTER(5);
  1134. Real *c, *a;
  1135. size_t mx;
  1136. GUARD_OBJ(a,GetVpValue(self,1));
  1137. mx = a->Prec *(VpBaseFig() + 1);
  1138. GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
  1139. VpAsgn(c, a, 1);
  1140. VpChangeSign(c, 1);
  1141. return ToValue(c);
  1142. }
  1143. /* call-seq:
  1144. * sqrt(n)
  1145. *
  1146. * Returns the square root of the value.
  1147. *
  1148. * If n is specified, returns at least that many significant digits.
  1149. */
  1150. static VALUE
  1151. BigDecimal_sqrt(VALUE self, VALUE nFig)
  1152. {
  1153. ENTER(5);
  1154. Real *c, *a;
  1155. size_t mx, n;
  1156. GUARD_OBJ(a,GetVpValue(self,1));
  1157. mx = a->Prec *(VpBaseFig() + 1);
  1158. n = GetPositiveInt(nFig) + VpDblFig() + 1;
  1159. if(mx <= n) mx = n;
  1160. GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
  1161. VpSqrt(c, a);
  1162. return ToValue(c);
  1163. }
  1164. /* Return the integer part of the number.
  1165. */
  1166. static VALUE
  1167. BigDecimal_fix(VALUE self)
  1168. {
  1169. ENTER(5);
  1170. Real *c, *a;
  1171. size_t mx;
  1172. GUARD_OBJ(a,GetVpValue(self,1));
  1173. mx = a->Prec *(VpBaseFig() + 1);
  1174. GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
  1175. VpActiveRound(c,a,VP_ROUND_DOWN,0); /* 0: round off */
  1176. return ToValue(c);
  1177. }
  1178. /* call-seq:
  1179. * round(n, mode)
  1180. *
  1181. * Round to the nearest 1 (by default), returning the result as a BigDecimal.
  1182. *
  1183. * BigDecimal('3.14159').round -> 3
  1184. *
  1185. * BigDecimal('8.7').round -> 9
  1186. *
  1187. * If n is specified and positive, the fractional part of the result has no
  1188. * more than that many digits.
  1189. *
  1190. * If n is specified and negative, at least that many digits to the left of the
  1191. * decimal point will be 0 in the result.
  1192. *
  1193. * BigDecimal('3.14159').round(3) -> 3.142
  1194. *
  1195. * BigDecimal('13345.234').round(-2) -> 13300.0
  1196. *
  1197. * The value of the optional mode argument can be used to determine how
  1198. * rounding is performed; see BigDecimal.mode.
  1199. */
  1200. static VALUE
  1201. BigDecimal_round(int argc, VALUE *argv, VALUE self)
  1202. {
  1203. ENTER(5);
  1204. Real *c, *a;
  1205. int iLoc = 0;
  1206. VALUE vLoc;
  1207. VALUE vRound;
  1208. size_t mx, pl;
  1209. unsigned short sw = VpGetRoundMode();
  1210. switch (rb_scan_args(argc, argv, "02", &vLoc, &vRound)) {
  1211. case 0:
  1212. iLoc = 0;
  1213. break;
  1214. case 1:
  1215. Check_Type(vLoc, T_FIXNUM);
  1216. iLoc = FIX2INT(vLoc);
  1217. break;
  1218. case 2:
  1219. Check_Type(vLoc, T_FIXNUM);
  1220. iLoc = FIX2INT(vLoc);
  1221. sw = check_rounding_mode(vRound);
  1222. break;
  1223. }
  1224. pl = VpSetPrecLimit(0);
  1225. GUARD_OBJ(a,GetVpValue(self,1));
  1226. mx = a->Prec *(VpBaseFig() + 1);
  1227. GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
  1228. VpSetPrecLimit(pl);
  1229. VpActiveRound(c,a,sw,iLoc);
  1230. if (argc == 0) {
  1231. return BigDecimal_to_i(ToValue(c));
  1232. }
  1233. return ToValue(c);
  1234. }
  1235. /* call-seq:
  1236. * truncate(n)
  1237. *
  1238. * Truncate to the nearest 1, returning the result as a BigDecimal.
  1239. *
  1240. * BigDecimal('3.14159').truncate -> 3
  1241. *
  1242. * BigDecimal('8.7').truncate -> 8
  1243. *
  1244. * If n is specified and positive, the fractional part of the result has no
  1245. * more than that many digits.
  1246. *
  1247. * If n is specified and negative, at least that many digits to the left of the
  1248. * decimal point will be 0 in the result.
  1249. *
  1250. * BigDecimal('3.14159').truncate(3) -> 3.141
  1251. *
  1252. * BigDecimal('13345.234').truncate(-2) -> 13300.0
  1253. */
  1254. static VALUE
  1255. BigDecimal_truncate(int argc, VALUE *argv, VALUE self)
  1256. {
  1257. ENTER(5);
  1258. Real *c, *a;
  1259. int iLoc;
  1260. VALUE vLoc;
  1261. size_t mx, pl = VpSetPrecLimit(0);
  1262. if(rb_scan_args(argc,argv,"01",&vLoc)==0) {
  1263. iLoc = 0;
  1264. } else {
  1265. Check_Type(vLoc, T_FIXNUM);
  1266. iLoc = FIX2INT(vLoc);
  1267. }
  1268. GUARD_OBJ(a,GetVpValue(self,1));
  1269. mx = a->Prec *(VpBaseFig() + 1);
  1270. GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
  1271. VpSetPrecLimit(pl);
  1272. VpActiveRound(c,a,VP_ROUND_DOWN,iLoc); /* 0: truncate */
  1273. if (argc == 0) {
  1274. return BigDecimal_to_i(ToValue(c));
  1275. }
  1276. return ToValue(c);
  1277. }
  1278. /* Return the fractional part of the number.
  1279. */
  1280. static VALUE
  1281. BigDecimal_frac(VALUE self)
  1282. {
  1283. ENTER(5);
  1284. Real *c, *a;
  1285. size_t mx;
  1286. GUARD_OBJ(a,GetVpValue(self,1));
  1287. mx = a->Prec *(VpBaseFig() + 1);
  1288. GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
  1289. VpFrac(c, a);
  1290. return ToValue(c);
  1291. }
  1292. /* call-seq:
  1293. * floor(n)
  1294. *
  1295. * Return the largest integer less than or equal to the value, as a BigDecimal.
  1296. *
  1297. * BigDecimal('3.14159').floor -> 3
  1298. *
  1299. * BigDecimal('-9.1').floor -> -10
  1300. *
  1301. * If n is specified and positive, the fractional part of the result has no
  1302. * more than that many digits.
  1303. *
  1304. * If n is specified and negative, at least that
  1305. * many digits to the left of the decimal point will be 0 in the result.
  1306. *
  1307. * BigDecimal('3.14159').floor(3) -> 3.141
  1308. *
  1309. * BigDecimal('13345.234').floor(-2) -> 13300.0
  1310. */
  1311. static VALUE
  1312. BigDecimal_floor(int argc, VALUE *argv, VALUE self)
  1313. {
  1314. ENTER(5);
  1315. Real *c, *a;
  1316. int iLoc;
  1317. VALUE vLoc;
  1318. size_t mx, pl = VpSetPrecLimit(0);
  1319. if(rb_scan_args(argc,argv,"01",&vLoc)==0) {
  1320. iLoc = 0;
  1321. } else {
  1322. Check_Type(vLoc, T_FIXNUM);
  1323. iLoc = FIX2INT(vLoc);
  1324. }
  1325. GUARD_OBJ(a,GetVpValue(self,1));
  1326. mx = a->Prec *(VpBaseFig() + 1);
  1327. GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
  1328. VpSetPrecLimit(pl);
  1329. VpActiveRound(c,a,VP_ROUND_FLOOR,iLoc);
  1330. #ifdef BIGDECIMAL_DEBUG
  1331. VPrint(stderr, "floor: c=%\n", c);
  1332. #endif
  1333. if (argc == 0) {
  1334. return BigDecimal_to_i(ToValue(c));
  1335. }
  1336. return ToValue(c);
  1337. }
  1338. /* call-seq:
  1339. * ceil(n)
  1340. *
  1341. * Return the smallest integer greater than or equal to the value, as a BigDecimal.
  1342. *
  1343. * BigDecimal('3.14159').ceil -> 4
  1344. *
  1345. * BigDecimal('-9.1').ceil -> -9
  1346. *
  1347. * If n is specified and positive, the fractional part of the result has no
  1348. * more than that many digits.
  1349. *
  1350. * If n is specified and negative, at least that
  1351. * many digits to the left of the decimal point will be 0 in the result.
  1352. *
  1353. * BigDecimal('3.14159').ceil(3) -> 3.142
  1354. *
  1355. * BigDecimal('13345.234').ceil(-2) -> 13400.0
  1356. */
  1357. static VALUE
  1358. BigDecimal_ceil(int argc, VALUE *argv, VALUE self)
  1359. {
  1360. ENTER(5);
  1361. Real *c, *a;
  1362. int iLoc;
  1363. VALUE vLoc;
  1364. size_t mx, pl = VpSetPrecLimit(0);
  1365. if(rb_scan_args(argc,argv,"01",&vLoc)==0) {
  1366. iLoc = 0;
  1367. } else {
  1368. Check_Type(vLoc, T_FIXNUM);
  1369. iLoc = FIX2INT(vLoc);
  1370. }
  1371. GUARD_OBJ(a,GetVpValue(self,1));
  1372. mx = a->Prec *(VpBaseFig() + 1);
  1373. GUARD_OBJ(c,VpCreateRbObject(mx, "0"));
  1374. VpSetPrecLimit(pl);
  1375. VpActiveRound(c,a,VP_ROUND_CEIL,iLoc);
  1376. if (argc == 0) {
  1377. return BigDecimal_to_i(ToValue(c));
  1378. }
  1379. return ToValue(c);
  1380. }
  1381. /* call-seq:
  1382. * to_s(s)
  1383. *
  1384. * Converts the value to a string.
  1385. *
  1386. * The default format looks like 0.xxxxEnn.
  1387. *
  1388. * The optional parameter s consists of either an integer; or an optional '+'
  1389. * or ' ', followed by an optional number, followed by an optional 'E' or 'F'.
  1390. *
  1391. * If there is a '+' at the start of s, positive values are returned with
  1392. * a leading '+'.
  1393. *
  1394. * A space at the start of s returns positive values with a leading space.
  1395. *
  1396. * If s contains a number, a space is inserted after each group of that many
  1397. * fractional digits.
  1398. *
  1399. * If s ends with an 'E', engineering notation (0.xxxxEnn) is used.
  1400. *
  1401. * If s ends with an 'F', conventional floating point notation is used.
  1402. *
  1403. * Examples:
  1404. *
  1405. * BigDecimal.new('-123.45678901234567890').to_s('5F') -> '-123.45678 90123 45678 9'
  1406. *
  1407. * BigDecimal.new('123.45678901234567890').to_s('+8F') -> '+123.45678901 23456789'
  1408. *
  1409. * BigDecimal.new('123.45678901234567890').to_s(' F') -> ' 123.4567890123456789'
  1410. */
  1411. static VALUE
  1412. BigDecimal_to_s(int argc, VALUE *argv, VALUE self)
  1413. {
  1414. ENTER(5);
  1415. int fmt=0; /* 0:E format */
  1416. int fPlus=0; /* =0:default,=1: set ' ' before digits ,set '+' before digits. */
  1417. Real *vp;
  1418. volatile VALUE str;
  1419. char *psz;
  1420. char ch;
  1421. size_t nc, mc = 0;
  1422. VALUE f;
  1423. GUARD_OBJ(vp,GetVpValue(self,1));
  1424. if(rb_scan_args(argc,argv,"01",&f)==1) {
  1425. if(TYPE(f)==T_STRING) {
  1426. SafeStringValue(f);
  1427. psz = RSTRING_PTR(f);
  1428. if(*psz==' ') {
  1429. fPlus = 1; psz++;
  1430. } else if(*psz=='+') {
  1431. fPlus = 2; psz++;
  1432. }
  1433. while((ch=*psz++)!=0) {
  1434. if(ISSPACE(ch)) continue;
  1435. if(!ISDIGIT(ch)) {
  1436. if(ch=='F' || ch=='f') fmt = 1; /* F format */
  1437. break;
  1438. }
  1439. mc = mc * 10 + ch - '0';
  1440. }
  1441. }
  1442. else {
  1443. mc = (size_t)GetPositiveInt(f);
  1444. }
  1445. }
  1446. if(fmt) {
  1447. nc = VpNumOfChars(vp,"F");
  1448. } else {
  1449. nc = VpNumOfChars(vp,"E");
  1450. }
  1451. if(mc>0) nc += (nc + mc - 1) / mc + 1;
  1452. str = rb_str_new(0, nc);
  1453. psz = RSTRING_PTR(str);
  1454. if(fmt) {
  1455. VpToFString(vp, psz, mc, fPlus);
  1456. } else {
  1457. VpToString (vp, psz, mc, fPlus);
  1458. }
  1459. rb_str_resize(str, strlen(psz));
  1460. return str;
  1461. }
  1462. /* Splits a BigDecimal number into four parts, returned as an array of values.
  1463. *
  1464. * The first value represents the sign of the BigDecimal, and is -1 or 1, or 0
  1465. * if the BigDecimal is Not a Number.
  1466. *
  1467. * The second value is a string representing the significant digits of the
  1468. * BigDecimal, with no leading zeros.
  1469. *
  1470. * The third value is the base used for arithmetic (currently always 10) as an
  1471. * Integer.
  1472. *
  1473. * The fourth value is an Integer exponent.
  1474. *
  1475. * If the BigDecimal can be represented as 0.xxxxxx*10**n, then xxxxxx is the
  1476. * string of significant digits with no leading zeros, and n is the exponent.
  1477. *
  1478. * From these values, you can translate a BigDecimal to a float as follows:
  1479. *
  1480. * sign, significant_digits, base, exponent = a.split
  1481. * f = sign * "0.#{significant_digits}".to_f * (base ** exponent)
  1482. *
  1483. * (Note that the to_f method is provided as a more convenient way to translate
  1484. * a BigDecimal to a Float.)
  1485. */
  1486. static VALUE
  1487. BigDecimal_split(VALUE self)
  1488. {
  1489. ENTER(5);
  1490. Real *vp;
  1491. VALUE obj,str;
  1492. ssize_t e, s;
  1493. char *psz1;
  1494. GUARD_OBJ(vp,GetVpValue(self,1));
  1495. str = rb_str_new(0, VpNumOfChars(vp,"E"));
  1496. psz1 = RSTRING_PTR(str);
  1497. VpSzMantissa(vp,psz1);
  1498. s = 1;
  1499. if(psz1[0]=='-') {
  1500. size_t len = strlen(psz1+1);
  1501. memmove(psz1, psz1+1, len);
  1502. psz1[len] = '\0';
  1503. s = -1;
  1504. }
  1505. if(psz1[0]=='N') s=0; /* NaN */
  1506. e = VpExponent10(vp);
  1507. obj = rb_ary_new2(4);
  1508. rb_ary_push(obj, INT2FIX(s));
  1509. rb_ary_push(obj, str);
  1510. rb_str_resize(str, strlen(psz1));
  1511. rb_ary_push(obj, INT2FIX(10));
  1512. rb_ary_push(obj, INT2NUM(e));
  1513. return obj;
  1514. }
  1515. /* Returns the exponent of the BigDecimal number, as an Integer.
  1516. *
  1517. * If the number can be represented as 0.xxxxxx*10**n where xxxxxx is a string
  1518. * of digits with no leading zeros, then n is the exponent.
  1519. */
  1520. static VALUE
  1521. BigDecimal_exponent(VALUE self)
  1522. {
  1523. ssize_t e = VpExponent10(GetVpValue(self, 1));
  1524. return INT2NUM(e);
  1525. }
  1526. /* Returns debugging information about the value as a string of comma-separated
  1527. * values in angle brackets with a leading #:
  1528. *
  1529. * BigDecimal.new("1234.5678").inspect ->
  1530. * "#<BigDecimal:b7ea1130,'0.12345678E4',8(12)>"
  1531. *
  1532. * The first part is the address, the second is the value as a string, and
  1533. * the final part ss(mm) is the current number of significant digits and the
  1534. * maximum number of significant digits, respectively.
  1535. */
  1536. static VALUE
  1537. BigDecimal_inspect(VALUE self)
  1538. {
  1539. ENTER(5);
  1540. Real *vp;
  1541. volatile VALUE obj;
  1542. size_t nc;
  1543. char *psz, *tmp;
  1544. GUARD_OBJ(vp,GetVpValue(self,1));
  1545. nc = VpNumOfChars(vp,"E");
  1546. nc +=(nc + 9) / 10;
  1547. obj = rb_str_new(0, nc+256);
  1548. psz = RSTRING_PTR(obj);
  1549. sprintf(psz,"#<BigDecimal:%"PRIxVALUE",'",self);
  1550. tmp = psz + strlen(psz);
  1551. VpToString(vp, tmp, 10, 0);
  1552. tmp += strlen(tmp);
  1553. sprintf(tmp, "',%"PRIuSIZE"(%"PRIuSIZE")>", VpPrec(vp)*VpBaseFig(), VpMaxPrec(vp)*VpBaseFig());
  1554. rb_str_resize(obj, strlen(psz));
  1555. return obj;
  1556. }
  1557. /* call-seq:
  1558. * power(n)
  1559. *
  1560. * Returns the value raised to the power of n. Note that n must be an Integer.
  1561. *
  1562. * Also available as the operator **
  1563. */
  1564. static VALUE
  1565. BigDecimal_power(VALUE self, VALUE p)
  1566. {
  1567. ENTER(5);
  1568. Real *x, *y;
  1569. ssize_t mp, ma;
  1570. SIGNED_VALUE n;
  1571. Check_Type(p, T_FIXNUM);
  1572. n = FIX2INT(p);
  1573. ma = n;
  1574. if (ma < 0) ma = -ma;
  1575. if (ma == 0) ma = 1;
  1576. GUARD_OBJ(x, GetVpValue(self, 1));
  1577. if (VpIsDef(x)) {
  1578. mp = x->Prec * (VpBaseFig() + 1);
  1579. GUARD_OBJ(y, VpCreateRbObject(mp * (ma + 1), "0"));
  1580. }
  1581. else {
  1582. GUARD_OBJ(y, VpCreateRbObject(1, "0"));
  1583. }
  1584. VpPower(y, x, n);
  1585. return ToValue(y);
  1586. }
  1587. static VALUE
  1588. BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
  1589. {
  1590. ENTER(5);
  1591. Real *pv;
  1592. size_t mf;
  1593. VALUE nFig;
  1594. VALUE iniValue;
  1595. if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
  1596. mf = 0;
  1597. } else {
  1598. mf = GetPositiveInt(nFig);
  1599. }
  1600. SafeStringValue(iniValue);
  1601. GUARD_OBJ(pv,VpCreateRbObject(mf, RSTRING_PTR(iniValue)));
  1602. return ToValue(pv);
  1603. }
  1604. /* call-seq:
  1605. * new(initial, digits)
  1606. *
  1607. * Create a new BigDecimal object.
  1608. *
  1609. * initial:: The initial value, as a String. Spaces are ignored, unrecognized characters terminate the value.
  1610. *
  1611. * digits:: The number of significant digits, as a Fixnum. If omitted or 0, the number of significant digits is determined from the initial value.
  1612. *
  1613. * The actual number of significant digits used in computation is usually
  1614. * larger than the specified number.
  1615. */
  1616. static VALUE
  1617. BigDecimal_new(int argc, VALUE *argv, VALUE self)
  1618. {
  1619. ENTER(5);
  1620. Real *pv;
  1621. size_t mf;
  1622. VALUE nFig;
  1623. VALUE iniValue;
  1624. if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
  1625. mf = 0;
  1626. } else {
  1627. mf = GetPositiveInt(nFig);
  1628. }
  1629. SafeStringValue(iniValue);
  1630. GUARD_OBJ(pv,VpNewRbClass(mf, RSTRING_PTR(iniValue),self));
  1631. return ToValue(pv);
  1632. }
  1633. /* call-seq:
  1634. * BigDecimal.limit(digits)
  1635. *
  1636. * Limit the number of significant digits in newly created BigDecimal
  1637. * numbers to the specified value. Rounding is performed as necessary,
  1638. * as specified by BigDecimal.mode.
  1639. *
  1640. * A limit of 0, the default, means no upper limit.
  1641. *
  1642. * The limit specified by this method takes less priority over any limit
  1643. * specified to instance methods such as ceil, floor, truncate, or round.
  1644. */
  1645. static VALUE
  1646. BigDecimal_limit(int argc, VALUE *argv, VALUE self)
  1647. {
  1648. VALUE nFig;
  1649. VALUE nCur = INT2NUM(VpGetPrecLimit());
  1650. if(rb_scan_args(argc,argv,"01",&nFig)==1) {
  1651. int nf;
  1652. if(nFig==Qnil) return nCur;
  1653. Check_Type(nFig, T_FIXNUM);
  1654. nf = FIX2INT(nFig);
  1655. if(nf<0) {
  1656. rb_raise(rb_eArgError, "argument must be positive");
  1657. }
  1658. VpSetPrecLimit(nf);
  1659. }
  1660. return nCur;
  1661. }
  1662. /* Returns the sign of the value.
  1663. *
  1664. * Returns a positive value if > 0, a negative value if < 0, and a
  1665. * zero if == 0.
  1666. *
  1667. * The specific value returned indicates the type and sign of the BigDecimal,
  1668. * as follows:
  1669. *
  1670. * BigDecimal::SIGN_NaN:: value is Not a Number
  1671. * BigDecimal::SIGN_POSITIVE_ZERO:: value is +0
  1672. * BigDecimal::SIGN_NEGATIVE_ZERO:: value is -0
  1673. * BigDecimal::SIGN_POSITIVE_INFINITE:: value is +infinity
  1674. * BigDecimal::SIGN_NEGATIVE_INFINITE:: value is -infinity
  1675. * BigDecimal::SIGN_POSITIVE_FINITE:: value is positive
  1676. * BigDecimal::SIGN_NEGATIVE_FINITE:: value is negative
  1677. */
  1678. static VALUE
  1679. BigDecimal_sign(VALUE self)
  1680. { /* sign */
  1681. int s = GetVpValue(self,1)->sign;
  1682. return INT2FIX(s);
  1683. }
  1684. /* call-seq:
  1685. * BigDecimal.save_exception_mode { ... }
  1686. */
  1687. static VALUE
  1688. BigDecimal_save_exception_mode(VALUE self)
  1689. {
  1690. unsigned short const exception_mode = VpGetException();
  1691. int state;
  1692. VALUE ret = rb_protect(rb_yield, Qnil, &state);
  1693. VpSetException(exception_mode);
  1694. if (state) rb_jump_tag(state);
  1695. return ret;
  1696. }
  1697. /* call-seq:
  1698. * BigDecimal.save_rounding_mode { ... }
  1699. */
  1700. static VALUE
  1701. BigDecimal_save_rounding_mode(VALUE self)
  1702. {
  1703. unsigned short const round_mode = VpGetRoundMode();
  1704. int state;
  1705. VALUE ret = rb_protect(rb_yield, Qnil, &state);
  1706. VpSetRoundMode(round_mode);
  1707. if (state) rb_jump_tag(state);
  1708. return Qnil;
  1709. }
  1710. /* call-seq:
  1711. * BigDecimal.save_limit { ... }
  1712. */
  1713. static VALUE
  1714. BigDecimal_save_limit(VALUE self)
  1715. {
  1716. size_t const limit = VpGetPrecLimit();
  1717. int state;
  1718. VALUE ret = rb_protect(rb_yield, Qnil, &state);
  1719. VpSetPrecLimit(limit);
  1720. if (state) rb_jump_tag(state);
  1721. return Qnil;
  1722. }
  1723. /* Document-class: BigDecimal
  1724. * BigDecimal provides arbitrary-precision floating point decimal arithmetic.
  1725. *
  1726. * Copyright (C) 2002 by Shigeo Kobayashi <shigeo@tinyforest.gr.jp>.
  1727. * You may distribute under the terms of either the GNU General Public
  1728. * License or the Artistic License, as specified in the README file
  1729. * of the BigDecimal distribution.
  1730. *
  1731. * Documented by mathew <meta@pobox.com>.
  1732. *
  1733. * = Introduction
  1734. *
  1735. * Ruby provides built-in support for arbitrary precision integer arithmetic.
  1736. * For example:
  1737. *
  1738. * 42**13 -> 1265437718438866624512
  1739. *
  1740. * BigDecimal provides similar support for very large or very accurate floating
  1741. * point numbers.
  1742. *
  1743. * Decimal arithmetic is also useful for general calculation, because it
  1744. * provides the correct answers people expect--whereas normal binary floating
  1745. * point arithmetic often introduces subtle errors because of the conversion
  1746. * between base 10 and base 2. For example, try:
  1747. *
  1748. * sum = 0
  1749. * for i in (1..10000)
  1750. * sum = sum + 0.0001
  1751. * end
  1752. * print sum
  1753. *
  1754. * and contrast with the output from:
  1755. *
  1756. * require 'bigdecimal'
  1757. *
  1758. * sum = BigDecimal.new("0")
  1759. * for i in (1..10000)
  1760. * sum = sum + BigDecimal.new("0.0001")
  1761. * end
  1762. * print sum
  1763. *
  1764. * Similarly:
  1765. *
  1766. * (BigDecimal.new("1.2") - BigDecimal("1.0")) == BigDecimal("0.2") -> true
  1767. *
  1768. * (1.2 - 1.0) == 0.2 -> false
  1769. *
  1770. * = Special features of accurate decimal arithmetic
  1771. *
  1772. * Because BigDecimal is more accurate than normal binary floating point
  1773. * arithmetic, it requires some special values.
  1774. *
  1775. * == Infinity
  1776. *
  1777. * BigDecimal sometimes needs to return infinity, for example if you divide
  1778. * a value by zero.
  1779. *
  1780. * BigDecimal.new("1.0") / BigDecimal.new("0.0") -> infinity
  1781. *
  1782. * BigDecimal.new("-1.0") / BigDecimal.new("0.0") -> -infinity
  1783. *
  1784. * You can represent infinite numbers to BigDecimal using the strings
  1785. * 'Infinity', '+Infinity' and '-Infinity' (case-sensitive)
  1786. *
  1787. * == Not a Number
  1788. *
  1789. * When a computation results in an undefined value, the special value NaN
  1790. * (for 'not a number') is returned.
  1791. *
  1792. * Example:
  1793. *
  1794. * BigDecimal.new("0.0") / BigDecimal.new("0.0") -> NaN
  1795. *
  1796. * You can also create undefined values. NaN is never considered to be the
  1797. * same as any other value, even NaN itself:
  1798. *
  1799. * n = BigDecimal.new('NaN')
  1800. *
  1801. * n == 0.0 -> nil
  1802. *
  1803. * n == n -> nil
  1804. *
  1805. * == Positive and negative zero
  1806. *
  1807. * If a computation results in a value which is too small to be represented as
  1808. * a BigDecimal within the currently specified limits of precision, zero must
  1809. * be returned.
  1810. *
  1811. * If the value which is too small to be represented is negative, a BigDecimal
  1812. * value of negative zero is returned. If the value is positive, a value of
  1813. * positive zero is returned.
  1814. *
  1815. * BigDecimal.new("1.0") / BigDecimal.new("-Infinity") -> -0.0
  1816. *
  1817. * BigDecimal.new("1.0") / BigDecimal.new("Infinity") -> 0.0
  1818. *
  1819. * (See BigDecimal.mode for how to specify limits of precision.)
  1820. *
  1821. * Note that -0.0 and 0.0 are considered to be the same for the purposes of
  1822. * comparison.
  1823. *
  1824. * Note also that in mathematics, there is no particular concept of negative
  1825. * or positive zero; true mathematical zero has no sign.
  1826. */
  1827. void
  1828. Init_bigdecimal(void)
  1829. {
  1830. VALUE arg;
  1831. /* Initialize VP routines */
  1832. VpInit(0UL);
  1833. /* Class and method registration */
  1834. rb_cBigDecimal = rb_define_class("BigDecimal",rb_cNumeric);
  1835. /* Global function */
  1836. rb_define_global_function("BigDecimal", BigDecimal_global_new, -1);
  1837. /* Class methods */
  1838. rb_define_singleton_method(rb_cBigDecimal, "new", BigDecimal_new, -1);
  1839. rb_define_singleton_method(rb_cBigDecimal, "mode", BigDecimal_mode, -1);
  1840. rb_define_singleton_method(rb_cBigDecimal, "limit", BigDecimal_limit, -1);
  1841. rb_define_singleton_method(rb_cBigDecimal, "double_fig", BigDecimal_double_fig, 0);
  1842. rb_define_singleton_method(rb_cBigDecimal, "_load", BigDecimal_load, 1);
  1843. rb_define_singleton_method(rb_cBigDecimal, "ver", BigDecimal_version, 0);
  1844. rb_define_singleton_method(rb_cBigDecimal, "save_exception_mode", BigDecimal_save_exception_mode, 0);
  1845. rb_define_singleton_method(rb_cBigDecimal, "save_rounding_mode", BigDecimal_save_rounding_mode, 0);
  1846. rb_define_singleton_method(rb_cBigDecimal, "save_limit", BigDecimal_save_limit, 0);
  1847. /* Constants definition */
  1848. /*
  1849. * Base value used in internal calculations. On a 32 bit system, BASE
  1850. * is 10000, indicating that calculation is done in groups of 4 digits.
  1851. * (If it were larger, BASE**2 wouldn't fit in 32 bits, so you couldn't
  1852. * guarantee that two groups could always be multiplied together without
  1853. * overflow.)
  1854. */
  1855. rb_define_const(rb_cBigDecimal, "BASE", INT2FIX((SIGNED_VALUE)VpBaseVal()));
  1856. /* Exceptions */
  1857. /*
  1858. * 0xff: Determines whether overflow, underflow or zero divide result in
  1859. * an exception being thrown. See BigDecimal.mode.
  1860. */
  1861. rb_define_const(rb_cBigDecimal, "EXCEPTION_ALL",INT2FIX(VP_EXCEPTION_ALL));
  1862. /*
  1863. * 0x02: Determines what happens when the result of a computation is not a
  1864. * number (NaN). See BigDecimal.mode.
  1865. */
  1866. rb_define_const(rb_cBigDecimal, "EXCEPTION_NaN",INT2FIX(VP_EXCEPTION_NaN));
  1867. /*
  1868. * 0x01: Determines what happens when the result of a computation is
  1869. * infinity. See BigDecimal.mode.
  1870. */
  1871. rb_define_const(rb_cBigDecimal, "EXCEPTION_INFINITY",INT2FIX(VP_EXCEPTION_INFINITY));
  1872. /*
  1873. * 0x04: Determines what happens when the result of a computation is an
  1874. * underflow (a result too small to be represented). See BigDecimal.mode.
  1875. */
  1876. rb_define_const(rb_cBigDecimal, "EXCEPTION_UNDERFLOW",INT2FIX(VP_EXCEPTION_UNDERFLOW));
  1877. /*
  1878. * 0x01: Determines what happens when the result of a computation is an
  1879. * overflow (a result too large to be represented). See BigDecimal.mode.
  1880. */
  1881. rb_define_const(rb_cBigDecimal, "EXCEPTION_OVERFLOW",INT2FIX(VP_EXCEPTION_OVERFLOW));
  1882. /*
  1883. * 0x01: Determines what happens when a division by zero is performed.
  1884. * See BigDecimal.mode.
  1885. */
  1886. rb_define_const(rb_cBigDecimal, "EXCEPTION_ZERODIVIDE",INT2FIX(VP_EXCEPTION_ZERODIVIDE));
  1887. /*
  1888. * 0x100: Determines what happens when a result must be rounded in order to
  1889. * fit in the appropriate number of significant digits. See
  1890. * BigDecimal.mode.
  1891. */
  1892. rb_define_const(rb_cBigDecimal, "ROUND_MODE",INT2FIX(VP_ROUND_MODE));
  1893. /* 1: Indicates that values should be rounded away from zero. See
  1894. * BigDecimal.mode.
  1895. */
  1896. rb_define_const(rb_cBigDecimal, "ROUND_UP",INT2FIX(VP_ROUND_UP));
  1897. /* 2: Indicates that values should be rounded towards zero. See
  1898. * BigDecimal.mode.
  1899. */
  1900. rb_define_const(rb_cBigDecimal, "ROUND_DOWN",INT2FIX(VP_ROUND_DOWN));
  1901. /* 3: Indicates that digits >= 5 should be rounded up, others rounded down.
  1902. * See BigDecimal.mode. */
  1903. rb_define_const(rb_cBigDecimal, "ROUND_HALF_UP",INT2FIX(VP_ROUND_HALF_UP));
  1904. /* 4: Indicates that digits >= 6 should be rounded up, others rounded down.
  1905. * See BigDecimal.mode.
  1906. */
  1907. rb_define_const(rb_cBigDecimal, "ROUND_HALF_DOWN",INT2FIX(VP_ROUND_HALF_DOWN));
  1908. /* 5: Round towards +infinity. See BigDecimal.mode. */
  1909. rb_define_const(rb_cBigDecimal, "ROUND_CEILING",INT2FIX(VP_ROUND_CEIL));
  1910. /* 6: Round towards -infinity. See BigDecimal.mode. */
  1911. rb_define_const(rb_cBigDecimal, "ROUND_FLOOR",INT2FIX(VP_ROUND_FLOOR));
  1912. /* 7: Round towards the even neighbor. See BigDecimal.mode. */
  1913. rb_define_const(rb_cBigDecimal, "ROUND_HALF_EVEN",INT2FIX(VP_ROUND_HALF_EVEN));
  1914. /* 0: Indicates that a value is not a number. See BigDecimal.sign. */
  1915. rb_define_const(rb_cBigDecimal, "SIGN_NaN",INT2FIX(VP_SIGN_NaN));
  1916. /* 1: Indicates that a value is +0. See BigDecimal.sign. */
  1917. rb_define_const(rb_cBigDecimal, "SIGN_POSITIVE_ZERO",INT2FIX(VP_SIGN_POSITIVE_ZERO));
  1918. /* -1: Indicates that a value is -0. See BigDecimal.sign. */
  1919. rb_define_const(rb_cBigDecimal, "SIGN_NEGATIVE_ZERO",INT2FIX(VP_SIGN_NEGATIVE_ZERO));
  1920. /* 2: Indicates that a value is positive and finite. See BigDecimal.sign. */
  1921. rb_define_const(rb_cBigDecimal, "SIGN_POSITIVE_FINITE",INT2FIX(VP_SIGN_POSITIVE_FINITE));
  1922. /* -2: Indicates that a value is negative and finite. See BigDecimal.sign. */
  1923. rb_define_const(rb_cBigDecimal, "SIGN_NEGATIVE_FINITE",INT2FIX(VP_SIGN_NEGATIVE_FINITE));
  1924. /* 3: Indicates that a value is positive and infinite. See BigDecimal.sign. */
  1925. rb_define_const(rb_cBigDecimal, "SIGN_POSITIVE_INFINITE",INT2FIX(VP_SIGN_POSITIVE_INFINITE));
  1926. /* -3: Indicates that a value is negative and infinite. See BigDecimal.sign. */
  1927. rb_define_const(rb_cBigDecimal, "SIGN_NEGATIVE_INFINITE",INT2FIX(VP_SIGN_NEGATIVE_INFINITE));
  1928. arg = rb_str_new2("+Infinity");
  1929. rb_define_const(rb_cBigDecimal, "INFINITY", BigDecimal_global_new(1, &arg, rb_cBigDecimal));
  1930. arg = rb_str_new2("NaN");
  1931. rb_define_const(rb_cBigDecimal, "NAN", BigDecimal_global_new(1, &arg, rb_cBigDecimal));
  1932. /* instance methods */
  1933. rb_define_method(rb_cBigDecimal, "precs", BigDecimal_prec, 0);
  1934. rb_define_method(rb_cBigDecimal, "add", BigDecimal_add2, 2);
  1935. rb_define_method(rb_cBigDecimal, "sub", BigDecimal_sub2, 2);
  1936. rb_define_method(rb_cBigDecimal, "mult", BigDecimal_mult2, 2);
  1937. rb_define_method(rb_cBigDecimal, "div", BigDecimal_div2, -1);
  1938. rb_define_method(rb_cBigDecimal, "hash", BigDecimal_hash, 0);
  1939. rb_define_method(rb_cBigDecimal, "to_s", BigDecimal_to_s, -1);
  1940. rb_define_method(rb_cBigDecimal, "to_i", BigDecimal_to_i, 0);
  1941. rb_define_method(rb_cBigDecimal, "to_int", BigDecimal_to_i, 0);
  1942. rb_define_method(rb_cBigDecimal, "to_r", BigDecimal_to_r, 0);
  1943. rb_define_method(rb_cBigDecimal, "split", BigDecimal_split, 0);
  1944. rb_define_method(rb_cBigDecimal, "+", BigDecimal_add, 1);
  1945. rb_define_method(rb_cBigDecimal, "-", BigDecimal_sub, 1);
  1946. rb_define_method(rb_cBigDecimal, "+@", BigDecimal_uplus, 0);
  1947. rb_define_method(rb_cBigDecimal, "-@", BigDecimal_neg, 0);
  1948. rb_define_method(rb_cBigDecimal, "*", BigDecimal_mult, 1);
  1949. rb_define_method(rb_cBigDecimal, "/", BigDecimal_div, 1);
  1950. rb_define_method(rb_cBigDecimal, "quo", BigDecimal_div, 1);
  1951. rb_define_method(rb_cBigDecimal, "%", BigDecimal_mod, 1);
  1952. rb_define_method(rb_cBigDecimal, "modulo", BigDecimal_mod, 1);
  1953. rb_define_method(rb_cBigDecimal, "remainder", BigDecimal_remainder, 1);
  1954. rb_define_method(rb_cBigDecimal, "divmod", BigDecimal_divmod, 1);
  1955. /* rb_define_method(rb_cBigDecimal, "dup", BigDecimal_dup, 0); */
  1956. rb_define_method(rb_cBigDecimal, "to_f", BigDecimal_to_f, 0);
  1957. rb_define_method(rb_cBigDecimal, "abs", BigDecimal_abs, 0);
  1958. rb_define_method(rb_cBigDecimal, "sqrt", BigDecimal_sqrt, 1);
  1959. rb_define_method(rb_cBigDecimal, "fix", BigDecimal_fix, 0);
  1960. rb_define_method(rb_cBigDecimal, "round", BigDecimal_round, -1);
  1961. rb_define_method(rb_cBigDecimal, "frac", BigDecimal_frac, 0);
  1962. rb_define_method(rb_cBigDecimal, "floor", BigDecimal_floor, -1);
  1963. rb_define_method(rb_cBigDecimal, "ceil", BigDecimal_ceil, -1);
  1964. rb_define_method(rb_cBigDecimal, "power", BigDecimal_power, 1);
  1965. rb_define_method(rb_cBigDecimal, "**", BigDecimal_power, 1);
  1966. rb_define_method(rb_cBigDecimal, "<=>", BigDecimal_comp, 1);
  1967. rb_define_method(rb_cBigDecimal, "==", BigDecimal_eq, 1);
  1968. rb_define_method(rb_cBigDecimal, "===", BigDecimal_eq, 1);
  1969. rb_define_method(rb_cBigDecimal, "eql?", BigDecimal_eq, 1);
  1970. rb_define_method(rb_cBigDecimal, "<", BigDecimal_lt, 1);
  1971. rb_define_method(rb_cBigDecimal, "<=", BigDecimal_le, 1);
  1972. rb_define_method(rb_cBigDecimal, ">", BigDecimal_gt, 1);
  1973. rb_define_method(rb_cBigDecimal, ">=", BigDecimal_ge, 1);
  1974. rb_define_method(rb_cBigDecimal, "zero?", BigDecimal_zero, 0);
  1975. rb_define_method(rb_cBigDecimal, "nonzero?", BigDecimal_nonzero, 0);
  1976. rb_define_method(rb_cBigDecimal, "coerce", BigDecimal_coerce, 1);
  1977. rb_define_method(rb_cBigDecimal, "inspect", BigDecimal_inspect, 0);
  1978. rb_define_method(rb_cBigDecimal, "exponent", BigDecimal_exponent, 0);
  1979. rb_define_method(rb_cBigDecimal, "sign", BigDecimal_sign, 0);
  1980. rb_define_method(rb_cBigDecimal, "nan?", BigDecimal_IsNaN, 0);
  1981. rb_define_method(rb_cBigDecimal, "infinite?", BigDecimal_IsInfinite, 0);
  1982. rb_define_method(rb_cBigDecimal, "finite?", BigDecimal_IsFinite, 0);
  1983. rb_define_method(rb_cBigDecimal, "truncate", BigDecimal_truncate, -1);
  1984. rb_define_method(rb_cBigDecimal, "_dump", BigDecimal_dump, -1);
  1985. id_BigDecimal_exception_mode = rb_intern_const("BigDecimal.exception_mode");
  1986. id_BigDecimal_rounding_mode = rb_intern_const("BigDecimal.rounding_mode");
  1987. id_BigDecimal_precision_limit = rb_intern_const("BigDecimal.precision_limit");
  1988. id_up = rb_intern_const("up");
  1989. id_down = rb_intern_const("down");
  1990. id_truncate = rb_intern_const("truncate");
  1991. id_half_up = rb_intern_const("half_up");
  1992. id_default = rb_intern_const("default");
  1993. id_half_down = rb_intern_const("half_down");
  1994. id_half_even = rb_intern_const("half_even");
  1995. id_banker = rb_intern_const("banker");
  1996. id_ceiling = rb_intern_const("ceiling");
  1997. id_ceil = rb_intern_const("ceil");
  1998. id_floor = rb_intern_const("floor");
  1999. }
  2000. /*
  2001. *
  2002. * ============================================================================
  2003. *
  2004. * vp_ routines begin from here.
  2005. *
  2006. * ============================================================================
  2007. *
  2008. */
  2009. #ifdef BIGDECIMAL_DEBUG
  2010. static int gfDebug = 1; /* Debug switch */
  2011. #if 0
  2012. static int gfCheckVal = 1; /* Value checking flag in VpNmlz() */
  2013. #endif
  2014. #endif /* BIGDECIMAL_DEBUG */
  2015. static Real *VpConstOne; /* constant 1.0 */
  2016. static Real *VpPt5; /* constant 0.5 */
  2017. #define maxnr 100UL /* Maximum iterations for calcurating sqrt. */
  2018. /* used in VpSqrt() */
  2019. /* ETC */
  2020. #define MemCmp(x,y,z) memcmp(x,y,z)
  2021. #define StrCmp(x,y) strcmp(x,y)
  2022. static int VpIsDefOP(Real *c,Real *a,Real *b,int sw);
  2023. static int AddExponent(Real *a, SIGNED_VALUE n);
  2024. static BDIGIT VpAddAbs(Real *a,Real *b,Real *c);
  2025. static BDIGIT VpSubAbs(Real *a,Real *b,Real *c);
  2026. static size_t VpSetPTR(Real *a, Real *b, Real *c, size_t *a_pos, size_t *b_pos, size_t *c_pos, BDIGIT *av, BDIGIT *bv);
  2027. static int VpNmlz(Real *a);
  2028. static void VpFormatSt(char *psz, size_t fFmt);
  2029. static int VpRdup(Real *m, size_t ind_m);
  2030. #ifdef BIGDECIMAL_DEBUG
  2031. static int gnAlloc=0; /* Memory allocation counter */
  2032. #endif /* BIGDECIMAL_DEBUG */
  2033. VP_EXPORT void *
  2034. VpMemAlloc(size_t mb)
  2035. {
  2036. void *p = xmalloc((unsigned int)mb);
  2037. if(!p) {
  2038. VpException(VP_EXCEPTION_MEMORY,"failed to allocate memory",1);
  2039. }
  2040. memset(p,0,mb);
  2041. #ifdef BIGDECIMAL_DEBUG
  2042. gnAlloc++; /* Count allocation call */
  2043. #endif /* BIGDECIMAL_DEBUG */
  2044. return p;
  2045. }
  2046. VP_EXPORT void
  2047. VpFree(Real *pv)
  2048. {
  2049. if(pv != NULL) {
  2050. xfree(pv);
  2051. #ifdef BIGDECIMAL_DEBUG
  2052. gnAlloc--; /* Decrement allocation count */
  2053. if(gnAlloc==0) {
  2054. printf(" *************** All memories allocated freed ****************");
  2055. getchar();
  2056. }
  2057. if(gnAlloc<0) {
  2058. printf(" ??????????? Too many memory free calls(%d) ?????????????\n",gnAlloc);
  2059. getchar();
  2060. }
  2061. #endif /* BIGDECIMAL_DEBUG */
  2062. }
  2063. }
  2064. /*
  2065. * EXCEPTION Handling.
  2066. */
  2067. #define rmpd_set_thread_local_exception_mode(mode) \
  2068. rb_thread_local_aset( \
  2069. rb_thread_current(), \
  2070. id_BigDecimal_exception_mode, \
  2071. INT2FIX((int)(mode)) \
  2072. )
  2073. static unsigned short
  2074. VpGetException (void)
  2075. {
  2076. VALUE const vmode = rb_thread_local_aref(
  2077. rb_thread_current(),
  2078. id_BigDecimal_exception_mode
  2079. );
  2080. if (NIL_P(vmode)) {
  2081. rmpd_set_thread_local_exception_mode(RMPD_EXCEPTION_MODE_DEFAULT);
  2082. return RMPD_EXCEPTION_MODE_DEFAULT;
  2083. }
  2084. return (unsigned short)FIX2UINT(vmode);
  2085. }
  2086. static void
  2087. VpSetException(unsigned short f)
  2088. {
  2089. rmpd_set_thread_local_exception_mode(f);
  2090. }
  2091. /*
  2092. * Precision limit.
  2093. */
  2094. #define rmpd_set_thread_local_precision_limit(limit) \
  2095. rb_thread_local_aset( \
  2096. rb_thread_current(), \
  2097. id_BigDecimal_precision_limit, \
  2098. SIZET2NUM(limit) \
  2099. )
  2100. #define RMPD_PRECISION_LIMIT_DEFAULT ((size_t)0)
  2101. /* These 2 functions added at v1.1.7 */
  2102. VP_EXPORT size_t
  2103. VpGetPrecLimit(void)
  2104. {
  2105. VALUE const vlimit = rb_thread_local_aref(
  2106. rb_thread_current(),
  2107. id_BigDecimal_precision_limit
  2108. );
  2109. if (NIL_P(vlimit)) {
  2110. rmpd_set_thread_local_precision_limit(RMPD_PRECISION_LIMIT_DEFAULT);
  2111. return RMPD_PRECISION_LIMIT_DEFAULT;
  2112. }
  2113. return NUM2SIZET(vlimit);
  2114. }
  2115. VP_EXPORT size_t
  2116. VpSetPrecLimit(size_t n)
  2117. {
  2118. size_t const s = VpGetPrecLimit();
  2119. rmpd_set_thread_local_precision_limit(n);
  2120. return s;
  2121. }
  2122. /*
  2123. * Rounding mode.
  2124. */
  2125. #define rmpd_set_thread_local_rounding_mode(mode) \
  2126. rb_thread_local_aset( \
  2127. rb_thread_current(), \
  2128. id_BigDecimal_rounding_mode, \
  2129. INT2FIX((int)(mode)) \
  2130. )
  2131. VP_EXPORT unsigned short
  2132. VpGetRoundMode(void)
  2133. {
  2134. VALUE const vmode = rb_thread_local_aref(
  2135. rb_thread_current(),
  2136. id_BigDecimal_rounding_mode
  2137. );
  2138. if (NIL_P(vmode)) {
  2139. rmpd_set_thread_local_rounding_mode(RMPD_ROUNDING_MODE_DEFAULT);
  2140. return RMPD_ROUNDING_MODE_DEFAULT;
  2141. }
  2142. return (unsigned short)FIX2INT(vmode);
  2143. }
  2144. VP_EXPORT int
  2145. VpIsRoundMode(unsigned short n)
  2146. {
  2147. switch (n) {
  2148. case VP_ROUND_UP:
  2149. case VP_ROUND_DOWN:
  2150. case VP_ROUND_HALF_UP:
  2151. case VP_ROUND_HALF_DOWN:
  2152. case VP_ROUND_CEIL:
  2153. case VP_ROUND_FLOOR:
  2154. case VP_ROUND_HALF_EVEN:
  2155. return 1;
  2156. default:
  2157. return 0;
  2158. }
  2159. }
  2160. VP_EXPORT unsigned short
  2161. VpSetRoundMode(unsigned short n)
  2162. {
  2163. if (VpIsRoundMode(n)) {
  2164. rmpd_set_thread_local_rounding_mode(n);
  2165. return n;
  2166. }
  2167. return VpGetRoundMode();
  2168. }
  2169. /*
  2170. * 0.0 & 1.0 generator
  2171. * These gZero_..... and gOne_..... can be any name
  2172. * referenced from nowhere except Zero() and One().
  2173. * gZero_..... and gOne_..... must have global scope
  2174. * (to let the compiler know they may be changed in outside
  2175. * (... but not actually..)).
  2176. */
  2177. volatile const double gZero_ABCED9B1_CE73__00400511F31D = 0.0;
  2178. volatile const double gOne_ABCED9B4_CE73__00400511F31D = 1.0;
  2179. static double
  2180. Zero(void)
  2181. {
  2182. return gZero_ABCED9B1_CE73__00400511F31D;
  2183. }
  2184. static double
  2185. One(void)
  2186. {
  2187. return gOne_ABCED9B4_CE73__00400511F31D;
  2188. }
  2189. /*
  2190. ----------------------------------------------------------------
  2191. Value of sign in Real structure is reserved for future use.
  2192. short sign;
  2193. ==0 : NaN
  2194. 1 : Positive zero
  2195. -1 : Negative zero
  2196. 2 : Positive number
  2197. -2 : Negative number
  2198. 3 : Positive infinite number
  2199. -3 : Negative infinite number
  2200. ----------------------------------------------------------------
  2201. */
  2202. VP_EXPORT double
  2203. VpGetDoubleNaN(void) /* Returns the value of NaN */
  2204. {
  2205. static double fNaN = 0.0;
  2206. if(fNaN==0.0) fNaN = Zero()/Zero();
  2207. return fNaN;
  2208. }
  2209. VP_EXPORT double
  2210. VpGetDoublePosInf(void) /* Returns the value of +Infinity */
  2211. {
  2212. static double fInf = 0.0;
  2213. if(fInf==0.0) fInf = One()/Zero();
  2214. return fInf;
  2215. }
  2216. VP_EXPORT double
  2217. VpGetDoubleNegInf(void) /* Returns the value of -Infinity */
  2218. {
  2219. static double fInf = 0.0;
  2220. if(fInf==0.0) fInf = -(One()/Zero());
  2221. return fInf;
  2222. }
  2223. VP_EXPORT double
  2224. VpGetDoubleNegZero(void) /* Returns the value of -0 */
  2225. {
  2226. static double nzero = 1000.0;
  2227. if(nzero!=0.0) nzero = (One()/VpGetDoubleNegInf());
  2228. return nzero;
  2229. }
  2230. #if 0 /* unused */
  2231. VP_EXPORT int
  2232. VpIsNegDoubleZero(double v)
  2233. {
  2234. double z = VpGetDoubleNegZero();
  2235. return MemCmp(&v,&z,sizeof(v))==0;
  2236. }
  2237. #endif
  2238. VP_EXPORT int
  2239. VpException(unsigned short f, const char *str,int always)
  2240. {
  2241. VALUE exc;
  2242. int fatal=0;
  2243. unsigned short const exception_mode = VpGetException();
  2244. if(f==VP_EXCEPTION_OP || f==VP_EXCEPTION_MEMORY) always = 1;
  2245. if (always || (exception_mode & f)) {
  2246. switch(f)
  2247. {
  2248. /*
  2249. case VP_EXCEPTION_OVERFLOW:
  2250. */
  2251. case VP_EXCEPTION_ZERODIVIDE:
  2252. case VP_EXCEPTION_INFINITY:
  2253. case VP_EXCEPTION_NaN:
  2254. case VP_EXCEPTION_UNDERFLOW:
  2255. case VP_EXCEPTION_OP:
  2256. exc = rb_eFloatDomainError;
  2257. goto raise;
  2258. case VP_EXCEPTION_MEMORY:
  2259. fatal = 1;
  2260. goto raise;
  2261. default:
  2262. fatal = 1;
  2263. goto raise;
  2264. }
  2265. }
  2266. return 0; /* 0 Means VpException() raised no exception */
  2267. raise:
  2268. if(fatal) rb_fatal("%s", str);
  2269. else rb_raise(exc, "%s", str);
  2270. return 0;
  2271. }
  2272. /* Throw exception or returns 0,when resulting c is Inf or NaN */
  2273. /* sw=1:+ 2:- 3:* 4:/ */
  2274. static int
  2275. VpIsDefOP(Real *c,Real *a,Real *b,int sw)
  2276. {
  2277. if(VpIsNaN(a) || VpIsNaN(b)) {
  2278. /* at least a or b is NaN */
  2279. VpSetNaN(c);
  2280. goto NaN;
  2281. }
  2282. if(VpIsInf(a)) {
  2283. if(VpIsInf(b)) {
  2284. switch(sw)
  2285. {
  2286. case 1: /* + */
  2287. if(VpGetSign(a)==VpGetSign(b)) {
  2288. VpSetInf(c,VpGetSign(a));
  2289. goto Inf;
  2290. } else {
  2291. VpSetNaN(c);
  2292. goto NaN;
  2293. }
  2294. case 2: /* - */
  2295. if(VpGetSign(a)!=VpGetSign(b)) {
  2296. VpSetInf(c,VpGetSign(a));
  2297. goto Inf;
  2298. } else {
  2299. VpSetNaN(c);
  2300. goto NaN;
  2301. }
  2302. break;
  2303. case 3: /* * */
  2304. VpSetInf(c,VpGetSign(a)*VpGetSign(b));
  2305. goto Inf;
  2306. break;
  2307. case 4: /* / */
  2308. VpSetNaN(c);
  2309. goto NaN;
  2310. }
  2311. VpSetNaN(c);
  2312. goto NaN;
  2313. }
  2314. /* Inf op Finite */
  2315. switch(sw)
  2316. {
  2317. case 1: /* + */
  2318. case 2: /* - */
  2319. VpSetInf(c,VpGetSign(a));
  2320. break;
  2321. case 3: /* * */
  2322. if(VpIsZero(b)) {
  2323. VpSetNaN(c);
  2324. goto NaN;
  2325. }
  2326. VpSetInf(c,VpGetSign(a)*VpGetSign(b));
  2327. break;
  2328. case 4: /* / */
  2329. VpSetInf(c,VpGetSign(a)*VpGetSign(b));
  2330. }
  2331. goto Inf;
  2332. }
  2333. if(VpIsInf(b)) {
  2334. switch(sw)
  2335. {
  2336. case 1: /* + */
  2337. VpSetInf(c,VpGetSign(b));
  2338. break;
  2339. case 2: /* - */
  2340. VpSetInf(c,-VpGetSign(b));
  2341. break;
  2342. case 3: /* * */
  2343. if(VpIsZero(a)) {
  2344. VpSetNaN(c);
  2345. goto NaN;
  2346. }
  2347. VpSetInf(c,VpGetSign(a)*VpGetSign(b));
  2348. break;
  2349. case 4: /* / */
  2350. VpSetZero(c,VpGetSign(a)*VpGetSign(b));
  2351. }
  2352. goto Inf;
  2353. }
  2354. return 1; /* Results OK */
  2355. Inf:
  2356. return VpException(VP_EXCEPTION_INFINITY,"Computation results to 'Infinity'",0);
  2357. NaN:
  2358. return VpException(VP_EXCEPTION_NaN,"Computation results to 'NaN'",0);
  2359. }
  2360. /*
  2361. ----------------------------------------------------------------
  2362. */
  2363. /*
  2364. * returns number of chars needed to represent vp in specified format.
  2365. */
  2366. VP_EXPORT size_t
  2367. VpNumOfChars(Real *vp,const char *pszFmt)
  2368. {
  2369. SIGNED_VALUE ex;
  2370. size_t nc;
  2371. if(vp == NULL) return BASE_FIG*2+6;
  2372. if(!VpIsDef(vp)) return 32; /* not sure,may be OK */
  2373. switch(*pszFmt)
  2374. {
  2375. case 'F':
  2376. nc = BASE_FIG*(vp->Prec + 1)+2;
  2377. ex = vp->exponent;
  2378. if(ex < 0) {
  2379. nc += BASE_FIG*(size_t)(-ex);
  2380. }
  2381. else {
  2382. if((size_t)ex > vp->Prec) {
  2383. nc += BASE_FIG*((size_t)ex - vp->Prec);
  2384. }
  2385. }
  2386. break;
  2387. case 'E':
  2388. default:
  2389. nc = BASE_FIG*(vp->Prec + 2)+6; /* 3: sign + exponent chars */
  2390. }
  2391. return nc;
  2392. }
  2393. /*
  2394. * Initializer for Vp routines and constants used.
  2395. * [Input]
  2396. * BaseVal: Base value(assigned to BASE) for Vp calculation.
  2397. * It must be the form BaseVal=10**n.(n=1,2,3,...)
  2398. * If Base <= 0L,then the BASE will be calcurated so
  2399. * that BASE is as large as possible satisfying the
  2400. * relation MaxVal <= BASE*(BASE+1). Where the value
  2401. * MaxVal is the largest value which can be represented
  2402. * by one BDIGIT word in the computer used.
  2403. *
  2404. * [Returns]
  2405. * 1+DBL_DIG ... OK
  2406. */
  2407. VP_EXPORT size_t
  2408. VpInit(BDIGIT BaseVal)
  2409. {
  2410. /* Setup +/- Inf NaN -0 */
  2411. VpGetDoubleNaN();
  2412. VpGetDoublePosInf();
  2413. VpGetDoubleNegInf();
  2414. VpGetDoubleNegZero();
  2415. /* Allocates Vp constants. */
  2416. VpConstOne = VpAlloc(1UL, "1");
  2417. VpPt5 = VpAlloc(1UL, ".5");
  2418. #ifdef BIGDECIMAL_DEBUG
  2419. gnAlloc = 0;
  2420. #endif /* BIGDECIMAL_DEBUG */
  2421. #ifdef BIGDECIMAL_DEBUG
  2422. if(gfDebug) {
  2423. printf("VpInit: BaseVal = %lu\n", BaseVal);
  2424. printf(" BASE = %lu\n", BASE);
  2425. printf(" HALF_BASE = %lu\n", HALF_BASE);
  2426. printf(" BASE1 = %lu\n", BASE1);
  2427. printf(" BASE_FIG = %u\n", BASE_FIG);
  2428. printf(" DBLE_FIG = %d\n", DBLE_FIG);
  2429. }
  2430. #endif /* BIGDECIMAL_DEBUG */
  2431. return rmpd_double_figures();
  2432. }
  2433. VP_EXPORT Real *
  2434. VpOne(void)
  2435. {
  2436. return VpConstOne;
  2437. }
  2438. /* If exponent overflows,then raise exception or returns 0 */
  2439. static int
  2440. AddExponent(Real *a, SIGNED_VALUE n)
  2441. {
  2442. SIGNED_VALUE e = a->exponent;
  2443. SIGNED_VALUE m = e+n;
  2444. SIGNED_VALUE eb, mb;
  2445. if(e>0) {
  2446. if(n>0) {
  2447. mb = m*(SIGNED_VALUE)BASE_FIG;
  2448. eb = e*(SIGNED_VALUE)BASE_FIG;
  2449. if(mb<eb) goto overflow;
  2450. }
  2451. } else if(n<0) {
  2452. mb = m*(SIGNED_VALUE)BASE_FIG;
  2453. eb = e*(SIGNED_VALUE)BASE_FIG;
  2454. if(mb>eb) goto underflow;
  2455. }
  2456. a->exponent = m;
  2457. return 1;
  2458. /* Overflow/Underflow ==> Raise exception or returns 0 */
  2459. underflow:
  2460. VpSetZero(a,VpGetSign(a));
  2461. return VpException(VP_EXCEPTION_UNDERFLOW,"Exponent underflow",0);
  2462. overflow:
  2463. VpSetInf(a,VpGetSign(a));
  2464. return VpException(VP_EXCEPTION_OVERFLOW,"Exponent overflow",0);
  2465. }
  2466. /*
  2467. * Allocates variable.
  2468. * [Input]
  2469. * mx ... allocation unit, if zero then mx is determined by szVal.
  2470. * The mx is the number of effective digits can to be stored.
  2471. * szVal ... value assigned(char). If szVal==NULL,then zero is assumed.
  2472. * If szVal[0]=='#' then Max. Prec. will not be considered(1.1.7),
  2473. * full precision specified by szVal is allocated.
  2474. *
  2475. * [Returns]
  2476. * Pointer to the newly allocated variable, or
  2477. * NULL be returned if memory allocation is failed,or any error.
  2478. */
  2479. VP_EXPORT Real *
  2480. VpAlloc(size_t mx, const char *szVal)
  2481. {
  2482. size_t i, ni, ipn, ipf, nf, ipe, ne, nalloc;
  2483. char v,*psz;
  2484. int sign=1;
  2485. Real *vp = NULL;
  2486. size_t mf = VpGetPrecLimit();
  2487. VALUE buf;
  2488. mx = (mx + BASE_FIG - 1) / BASE_FIG + 1; /* Determine allocation unit. */
  2489. if (szVal) {
  2490. while (ISSPACE(*szVal)) szVal++;
  2491. if (*szVal != '#') {
  2492. if (mf) {
  2493. mf = (mf + BASE_FIG - 1) / BASE_FIG + 2; /* Needs 1 more for div */
  2494. if (mx > mf) {
  2495. mx = mf;
  2496. }
  2497. }
  2498. }
  2499. else {
  2500. ++szVal;
  2501. }
  2502. }
  2503. else {
  2504. /* necessary to be able to store */
  2505. /* at least mx digits. */
  2506. /* szVal==NULL ==> allocate zero value. */
  2507. vp = (Real *) VpMemAlloc(sizeof(Real) + mx * sizeof(BDIGIT));
  2508. /* xmalloc() alway returns(or throw interruption) */
  2509. vp->MaxPrec = mx; /* set max precision */
  2510. VpSetZero(vp,1); /* initialize vp to zero. */
  2511. return vp;
  2512. }
  2513. /* Skip all '_' after digit: 2006-6-30 */
  2514. ni = 0;
  2515. buf = rb_str_tmp_new(strlen(szVal)+1);
  2516. psz = RSTRING_PTR(buf);
  2517. i = 0;
  2518. ipn = 0;
  2519. while ((psz[i]=szVal[ipn]) != 0) {
  2520. if (ISDIGIT(psz[i])) ++ni;
  2521. if (psz[i] == '_') {
  2522. if (ni > 0) { ipn++; continue; }
  2523. psz[i] = 0;
  2524. break;
  2525. }
  2526. ++i;
  2527. ++ipn;
  2528. }
  2529. /* Skip trailing spaces */
  2530. while (--i > 0) {
  2531. if (ISSPACE(psz[i])) psz[i] = 0;
  2532. else break;
  2533. }
  2534. szVal = psz;
  2535. /* Check on Inf & NaN */
  2536. if (StrCmp(szVal, SZ_PINF) == 0 ||
  2537. StrCmp(szVal, SZ_INF) == 0 ) {
  2538. vp = (Real *) VpMemAlloc(sizeof(Real) + sizeof(BDIGIT));
  2539. vp->MaxPrec = 1; /* set max precision */
  2540. VpSetPosInf(vp);
  2541. return vp;
  2542. }
  2543. if (StrCmp(szVal, SZ_NINF) == 0) {
  2544. vp = (Real *) VpMemAlloc(sizeof(Real) + sizeof(BDIGIT));
  2545. vp->MaxPrec = 1; /* set max precision */
  2546. VpSetNegInf(vp);
  2547. return vp;
  2548. }
  2549. if (StrCmp(szVal, SZ_NaN) == 0) {
  2550. vp = (Real *) VpMemAlloc(sizeof(Real) + sizeof(BDIGIT));
  2551. vp->MaxPrec = 1; /* set max precision */
  2552. VpSetNaN(vp);
  2553. return vp;
  2554. }
  2555. /* check on number szVal[] */
  2556. ipn = i = 0;
  2557. if (szVal[i] == '-') { sign=-1; ++i; }
  2558. else if (szVal[i] == '+') ++i;
  2559. /* Skip digits */
  2560. ni = 0; /* digits in mantissa */
  2561. while ((v = szVal[i]) != 0) {
  2562. if (!ISDIGIT(v)) break;
  2563. ++i;
  2564. ++ni;
  2565. }
  2566. nf = 0;
  2567. ipf = 0;
  2568. ipe = 0;
  2569. ne = 0;
  2570. if (v) {
  2571. /* other than digit nor \0 */
  2572. if (szVal[i] == '.') { /* xxx. */
  2573. ++i;
  2574. ipf = i;
  2575. while ((v = szVal[i]) != 0) { /* get fraction part. */
  2576. if (!ISDIGIT(v)) break;
  2577. ++i;
  2578. ++nf;
  2579. }
  2580. }
  2581. ipe = 0; /* Exponent */
  2582. switch (szVal[i]) {
  2583. case '\0':
  2584. break;
  2585. case 'e': case 'E':
  2586. case 'd': case 'D':
  2587. ++i;
  2588. ipe = i;
  2589. v = szVal[i];
  2590. if ((v == '-') || (v == '+')) ++i;
  2591. while ((v=szVal[i]) != 0) {
  2592. if (!ISDIGIT(v)) break;
  2593. ++i;
  2594. ++ne;
  2595. }
  2596. break;
  2597. default:
  2598. break;
  2599. }
  2600. }
  2601. nalloc = (ni + nf + BASE_FIG - 1) / BASE_FIG + 1; /* set effective allocation */
  2602. /* units for szVal[] */
  2603. if (mx <= 0) mx = 1;
  2604. nalloc = Max(nalloc, mx);
  2605. mx = nalloc;
  2606. vp = (Real *) VpMemAlloc(sizeof(Real) + mx * sizeof(BDIGIT));
  2607. /* xmalloc() alway returns(or throw interruption) */
  2608. vp->MaxPrec = mx; /* set max precision */
  2609. VpSetZero(vp, sign);
  2610. VpCtoV(vp, &szVal[ipn], ni, &szVal[ipf], nf, &szVal[ipe], ne);
  2611. rb_str_resize(buf, 0);
  2612. return vp;
  2613. }
  2614. /*
  2615. * Assignment(c=a).
  2616. * [Input]
  2617. * a ... RHSV
  2618. * isw ... switch for assignment.
  2619. * c = a when isw > 0
  2620. * c = -a when isw < 0
  2621. * if c->MaxPrec < a->Prec,then round operation
  2622. * will be performed.
  2623. * [Output]
  2624. * c ... LHSV
  2625. */
  2626. VP_EXPORT size_t
  2627. VpAsgn(Real *c, Real *a, int isw)
  2628. {
  2629. size_t n;
  2630. if(VpIsNaN(a)) {
  2631. VpSetNaN(c);
  2632. return 0;
  2633. }
  2634. if(VpIsInf(a)) {
  2635. VpSetInf(c,isw*VpGetSign(a));
  2636. return 0;
  2637. }
  2638. /* check if the RHS is zero */
  2639. if(!VpIsZero(a)) {
  2640. c->exponent = a->exponent; /* store exponent */
  2641. VpSetSign(c,(isw*VpGetSign(a))); /* set sign */
  2642. n =(a->Prec < c->MaxPrec) ?(a->Prec) :(c->MaxPrec);
  2643. c->Prec = n;
  2644. memcpy(c->frac, a->frac, n * sizeof(BDIGIT));
  2645. /* Needs round ? */
  2646. if(isw!=10) {
  2647. /* Not in ActiveRound */
  2648. if(c->Prec < a->Prec) {
  2649. VpInternalRound(c,n,(n>0)?a->frac[n-1]:0,a->frac[n]);
  2650. } else {
  2651. VpLimitRound(c,0);
  2652. }
  2653. }
  2654. } else {
  2655. /* The value of 'a' is zero. */
  2656. VpSetZero(c,isw*VpGetSign(a));
  2657. return 1;
  2658. }
  2659. return c->Prec*BASE_FIG;
  2660. }
  2661. /*
  2662. * c = a + b when operation = 1 or 2
  2663. * = a - b when operation = -1 or -2.
  2664. * Returns number of significant digits of c
  2665. */
  2666. VP_EXPORT size_t
  2667. VpAddSub(Real *c, Real *a, Real *b, int operation)
  2668. {
  2669. short sw, isw;
  2670. Real *a_ptr, *b_ptr;
  2671. size_t n, na, nb, i;
  2672. BDIGIT mrv;
  2673. #ifdef BIGDECIMAL_DEBUG
  2674. if(gfDebug) {
  2675. VPrint(stdout, "VpAddSub(enter) a=% \n", a);
  2676. VPrint(stdout, " b=% \n", b);
  2677. printf(" operation=%d\n", operation);
  2678. }
  2679. #endif /* BIGDECIMAL_DEBUG */
  2680. if(!VpIsDefOP(c,a,b,(operation>0)?1:2)) return 0; /* No significant digits */
  2681. /* check if a or b is zero */
  2682. if(VpIsZero(a)) {
  2683. /* a is zero,then assign b to c */
  2684. if(!VpIsZero(b)) {
  2685. VpAsgn(c, b, operation);
  2686. } else {
  2687. /* Both a and b are zero. */
  2688. if(VpGetSign(a)<0 && operation*VpGetSign(b)<0) {
  2689. /* -0 -0 */
  2690. VpSetZero(c,-1);
  2691. } else {
  2692. VpSetZero(c,1);
  2693. }
  2694. return 1; /* 0: 1 significant digits */
  2695. }
  2696. return c->Prec*BASE_FIG;
  2697. }
  2698. if(VpIsZero(b)) {
  2699. /* b is zero,then assign a to c. */
  2700. VpAsgn(c, a, 1);
  2701. return c->Prec*BASE_FIG;
  2702. }
  2703. if(operation < 0) sw = -1;
  2704. else sw = 1;
  2705. /* compare absolute value. As a result,|a_ptr|>=|b_ptr| */
  2706. if(a->exponent > b->exponent) {
  2707. a_ptr = a;
  2708. b_ptr = b;
  2709. } /* |a|>|b| */
  2710. else if(a->exponent < b->exponent) {
  2711. a_ptr = b;
  2712. b_ptr = a;
  2713. } /* |a|<|b| */
  2714. else {
  2715. /* Exponent part of a and b is the same,then compare fraction */
  2716. /* part */
  2717. na = a->Prec;
  2718. nb = b->Prec;
  2719. n = Min(na, nb);
  2720. for(i=0;i < n; ++i) {
  2721. if(a->frac[i] > b->frac[i]) {
  2722. a_ptr = a;
  2723. b_ptr = b;
  2724. goto end_if;
  2725. } else if(a->frac[i] < b->frac[i]) {
  2726. a_ptr = b;
  2727. b_ptr = a;
  2728. goto end_if;
  2729. }
  2730. }
  2731. if(na > nb) {
  2732. a_ptr = a;
  2733. b_ptr = b;
  2734. goto end_if;
  2735. } else if(na < nb) {
  2736. a_ptr = b;
  2737. b_ptr = a;
  2738. goto end_if;
  2739. }
  2740. /* |a| == |b| */
  2741. if(VpGetSign(a) + sw *VpGetSign(b) == 0) {
  2742. VpSetZero(c,1); /* abs(a)=abs(b) and operation = '-' */
  2743. return c->Prec*BASE_FIG;
  2744. }
  2745. a_ptr = a;
  2746. b_ptr = b;
  2747. }
  2748. end_if:
  2749. isw = VpGetSign(a) + sw *VpGetSign(b);
  2750. /*
  2751. * isw = 0 ...( 1)+(-1),( 1)-( 1),(-1)+(1),(-1)-(-1)
  2752. * = 2 ...( 1)+( 1),( 1)-(-1)
  2753. * =-2 ...(-1)+(-1),(-1)-( 1)
  2754. * If isw==0, then c =(Sign a_ptr)(|a_ptr|-|b_ptr|)
  2755. * else c =(Sign ofisw)(|a_ptr|+|b_ptr|)
  2756. */
  2757. if(isw) { /* addition */
  2758. VpSetSign(c, 1);
  2759. mrv = VpAddAbs(a_ptr, b_ptr, c);
  2760. VpSetSign(c, isw / 2);
  2761. } else { /* subtraction */
  2762. VpSetSign(c, 1);
  2763. mrv = VpSubAbs(a_ptr, b_ptr, c);
  2764. if(a_ptr == a) {
  2765. VpSetSign(c,VpGetSign(a));
  2766. } else {
  2767. VpSetSign(c,VpGetSign(a_ptr) * sw);
  2768. }
  2769. }
  2770. VpInternalRound(c,0,(c->Prec>0)?c->frac[c->Prec-1]:0,mrv);
  2771. #ifdef BIGDECIMAL_DEBUG
  2772. if(gfDebug) {
  2773. VPrint(stdout, "VpAddSub(result) c=% \n", c);
  2774. VPrint(stdout, " a=% \n", a);
  2775. VPrint(stdout, " b=% \n", b);
  2776. printf(" operation=%d\n", operation);
  2777. }
  2778. #endif /* BIGDECIMAL_DEBUG */
  2779. return c->Prec*BASE_FIG;
  2780. }
  2781. /*
  2782. * Addition of two variable precisional variables
  2783. * a and b assuming abs(a)>abs(b).
  2784. * c = abs(a) + abs(b) ; where |a|>=|b|
  2785. */
  2786. static BDIGIT
  2787. VpAddAbs(Real *a, Real *b, Real *c)
  2788. {
  2789. size_t word_shift;
  2790. size_t ap;
  2791. size_t bp;
  2792. size_t cp;
  2793. size_t a_pos;
  2794. size_t b_pos, b_pos_with_word_shift;
  2795. size_t c_pos;
  2796. BDIGIT av, bv, carry, mrv;
  2797. #ifdef BIGDECIMAL_DEBUG
  2798. if(gfDebug) {
  2799. VPrint(stdout, "VpAddAbs called: a = %\n", a);
  2800. VPrint(stdout, " b = %\n", b);
  2801. }
  2802. #endif /* BIGDECIMAL_DEBUG */
  2803. word_shift = VpSetPTR(a, b, c, &ap, &bp, &cp, &av, &bv);
  2804. a_pos = ap;
  2805. b_pos = bp;
  2806. c_pos = cp;
  2807. if(word_shift==(size_t)-1L) return 0; /* Overflow */
  2808. if(b_pos == (size_t)-1L) goto Assign_a;
  2809. mrv = av + bv; /* Most right val. Used for round. */
  2810. /* Just assign the last few digits of b to c because a has no */
  2811. /* corresponding digits to be added. */
  2812. while(b_pos + word_shift > a_pos) {
  2813. --c_pos;
  2814. if(b_pos > 0) {
  2815. c->frac[c_pos] = b->frac[--b_pos];
  2816. } else {
  2817. --word_shift;
  2818. c->frac[c_pos] = 0;
  2819. }
  2820. }
  2821. /* Just assign the last few digits of a to c because b has no */
  2822. /* corresponding digits to be added. */
  2823. b_pos_with_word_shift = b_pos + word_shift;
  2824. while(a_pos > b_pos_with_word_shift) {
  2825. c->frac[--c_pos] = a->frac[--a_pos];
  2826. }
  2827. carry = 0; /* set first carry be zero */
  2828. /* Now perform addition until every digits of b will be */
  2829. /* exhausted. */
  2830. while(b_pos > 0) {
  2831. c->frac[--c_pos] = a->frac[--a_pos] + b->frac[--b_pos] + carry;
  2832. if(c->frac[c_pos] >= BASE) {
  2833. c->frac[c_pos] -= BASE;
  2834. carry = 1;
  2835. } else {
  2836. carry = 0;
  2837. }
  2838. }
  2839. /* Just assign the first few digits of a with considering */
  2840. /* the carry obtained so far because b has been exhausted. */
  2841. while(a_pos > 0) {
  2842. c->frac[--c_pos] = a->frac[--a_pos] + carry;
  2843. if(c->frac[c_pos] >= BASE) {
  2844. c->frac[c_pos] -= BASE;
  2845. carry = 1;
  2846. } else {
  2847. carry = 0;
  2848. }
  2849. }
  2850. if(c_pos) c->frac[c_pos - 1] += carry;
  2851. goto Exit;
  2852. Assign_a:
  2853. VpAsgn(c, a, 1);
  2854. mrv = 0;
  2855. Exit:
  2856. #ifdef BIGDECIMAL_DEBUG
  2857. if(gfDebug) {
  2858. VPrint(stdout, "VpAddAbs exit: c=% \n", c);
  2859. }
  2860. #endif /* BIGDECIMAL_DEBUG */
  2861. return mrv;
  2862. }
  2863. /*
  2864. * c = abs(a) - abs(b)
  2865. */
  2866. static BDIGIT
  2867. VpSubAbs(Real *a, Real *b, Real *c)
  2868. {
  2869. size_t word_shift;
  2870. size_t ap;
  2871. size_t bp;
  2872. size_t cp;
  2873. size_t a_pos;
  2874. size_t b_pos, b_pos_with_word_shift;
  2875. size_t c_pos;
  2876. BDIGIT av, bv, borrow, mrv;
  2877. #ifdef BIGDECIMAL_DEBUG
  2878. if(gfDebug) {
  2879. VPrint(stdout, "VpSubAbs called: a = %\n", a);
  2880. VPrint(stdout, " b = %\n", b);
  2881. }
  2882. #endif /* BIGDECIMAL_DEBUG */
  2883. word_shift = VpSetPTR(a, b, c, &ap, &bp, &cp, &av, &bv);
  2884. a_pos = ap;
  2885. b_pos = bp;
  2886. c_pos = cp;
  2887. if(word_shift==(size_t)-1L) return 0; /* Overflow */
  2888. if(b_pos == (size_t)-1L) goto Assign_a;
  2889. if(av >= bv) {
  2890. mrv = av - bv;
  2891. borrow = 0;
  2892. } else {
  2893. mrv = 0;
  2894. borrow = 1;
  2895. }
  2896. /* Just assign the values which are the BASE subtracted by */
  2897. /* each of the last few digits of the b because the a has no */
  2898. /* corresponding digits to be subtracted. */
  2899. if(b_pos + word_shift > a_pos) {
  2900. while(b_pos + word_shift > a_pos) {
  2901. --c_pos;
  2902. if(b_pos > 0) {
  2903. c->frac[c_pos] = BASE - b->frac[--b_pos] - borrow;
  2904. } else {
  2905. --word_shift;
  2906. c->frac[c_pos] = BASE - borrow;
  2907. }
  2908. borrow = 1;
  2909. }
  2910. }
  2911. /* Just assign the last few digits of a to c because b has no */
  2912. /* corresponding digits to subtract. */
  2913. b_pos_with_word_shift = b_pos + word_shift;
  2914. while(a_pos > b_pos_with_word_shift) {
  2915. c->frac[--c_pos] = a->frac[--a_pos];
  2916. }
  2917. /* Now perform subtraction until every digits of b will be */
  2918. /* exhausted. */
  2919. while(b_pos > 0) {
  2920. --c_pos;
  2921. if(a->frac[--a_pos] < b->frac[--b_pos] + borrow) {
  2922. c->frac[c_pos] = BASE + a->frac[a_pos] - b->frac[b_pos] - borrow;
  2923. borrow = 1;
  2924. } else {
  2925. c->frac[c_pos] = a->frac[a_pos] - b->frac[b_pos] - borrow;
  2926. borrow = 0;
  2927. }
  2928. }
  2929. /* Just assign the first few digits of a with considering */
  2930. /* the borrow obtained so far because b has been exhausted. */
  2931. while(a_pos > 0) {
  2932. --c_pos;
  2933. if(a->frac[--a_pos] < borrow) {
  2934. c->frac[c_pos] = BASE + a->frac[a_pos] - borrow;
  2935. borrow = 1;
  2936. } else {
  2937. c->frac[c_pos] = a->frac[a_pos] - borrow;
  2938. borrow = 0;
  2939. }
  2940. }
  2941. if(c_pos) c->frac[c_pos - 1] -= borrow;
  2942. goto Exit;
  2943. Assign_a:
  2944. VpAsgn(c, a, 1);
  2945. mrv = 0;
  2946. Exit:
  2947. #ifdef BIGDECIMAL_DEBUG
  2948. if(gfDebug) {
  2949. VPrint(stdout, "VpSubAbs exit: c=% \n", c);
  2950. }
  2951. #endif /* BIGDECIMAL_DEBUG */
  2952. return mrv;
  2953. }
  2954. /*
  2955. * Note: If(av+bv)>= HALF_BASE,then 1 will be added to the least significant
  2956. * digit of c(In case of addition).
  2957. * ------------------------- figure of output -----------------------------------
  2958. * a = xxxxxxxxxxx
  2959. * b = xxxxxxxxxx
  2960. * c =xxxxxxxxxxxxxxx
  2961. * word_shift = | |
  2962. * right_word = | | (Total digits in RHSV)
  2963. * left_word = | | (Total digits in LHSV)
  2964. * a_pos = |
  2965. * b_pos = |
  2966. * c_pos = |
  2967. */
  2968. static size_t
  2969. VpSetPTR(Real *a, Real *b, Real *c, size_t *a_pos, size_t *b_pos, size_t *c_pos, BDIGIT *av, BDIGIT *bv)
  2970. {
  2971. size_t left_word, right_word, word_shift;
  2972. c->frac[0] = 0;
  2973. *av = *bv = 0;
  2974. word_shift =((a->exponent) -(b->exponent));
  2975. left_word = b->Prec + word_shift;
  2976. right_word = Max((a->Prec),left_word);
  2977. left_word =(c->MaxPrec) - 1; /* -1 ... prepare for round up */
  2978. /*
  2979. * check if 'round' is needed.
  2980. */
  2981. if(right_word > left_word) { /* round ? */
  2982. /*---------------------------------
  2983. * Actual size of a = xxxxxxAxx
  2984. * Actual size of b = xxxBxxxxx
  2985. * Max. size of c = xxxxxx
  2986. * Round off = |-----|
  2987. * c_pos = |
  2988. * right_word = |
  2989. * a_pos = |
  2990. */
  2991. *c_pos = right_word = left_word + 1; /* Set resulting precision */
  2992. /* be equal to that of c */
  2993. if((a->Prec) >=(c->MaxPrec)) {
  2994. /*
  2995. * a = xxxxxxAxxx
  2996. * c = xxxxxx
  2997. * a_pos = |
  2998. */
  2999. *a_pos = left_word;
  3000. *av = a->frac[*a_pos]; /* av is 'A' shown in above. */
  3001. } else {
  3002. /*
  3003. * a = xxxxxxx
  3004. * c = xxxxxxxxxx
  3005. * a_pos = |
  3006. */
  3007. *a_pos = a->Prec;
  3008. }
  3009. if((b->Prec + word_shift) >= c->MaxPrec) {
  3010. /*
  3011. * a = xxxxxxxxx
  3012. * b = xxxxxxxBxxx
  3013. * c = xxxxxxxxxxx
  3014. * b_pos = |
  3015. */
  3016. if(c->MaxPrec >=(word_shift + 1)) {
  3017. *b_pos = c->MaxPrec - word_shift - 1;
  3018. *bv = b->frac[*b_pos];
  3019. } else {
  3020. *b_pos = -1L;
  3021. }
  3022. } else {
  3023. /*
  3024. * a = xxxxxxxxxxxxxxxx
  3025. * b = xxxxxx
  3026. * c = xxxxxxxxxxxxx
  3027. * b_pos = |
  3028. */
  3029. *b_pos = b->Prec;
  3030. }
  3031. } else { /* The MaxPrec of c - 1 > The Prec of a + b */
  3032. /*
  3033. * a = xxxxxxx
  3034. * b = xxxxxx
  3035. * c = xxxxxxxxxxx
  3036. * c_pos = |
  3037. */
  3038. *b_pos = b->Prec;
  3039. *a_pos = a->Prec;
  3040. *c_pos = right_word + 1;
  3041. }
  3042. c->Prec = *c_pos;
  3043. c->exponent = a->exponent;
  3044. if(!AddExponent(c,1)) return (size_t)-1L;
  3045. return word_shift;
  3046. }
  3047. /*
  3048. * Return number og significant digits
  3049. * c = a * b , Where a = a0a1a2 ... an
  3050. * b = b0b1b2 ... bm
  3051. * c = c0c1c2 ... cl
  3052. * a0 a1 ... an * bm
  3053. * a0 a1 ... an * bm-1
  3054. * . . .
  3055. * . . .
  3056. * a0 a1 .... an * b0
  3057. * +_____________________________
  3058. * c0 c1 c2 ...... cl
  3059. * nc <---|
  3060. * MaxAB |--------------------|
  3061. */
  3062. VP_EXPORT size_t
  3063. VpMult(Real *c, Real *a, Real *b)
  3064. {
  3065. size_t MxIndA, MxIndB, MxIndAB, MxIndC;
  3066. size_t ind_c, i, ii, nc;
  3067. size_t ind_as, ind_ae, ind_bs, ind_be;
  3068. BDIGIT carry;
  3069. BDIGIT_DBL s;
  3070. Real *w;
  3071. #ifdef BIGDECIMAL_DEBUG
  3072. if(gfDebug) {
  3073. VPrint(stdout, "VpMult(Enter): a=% \n", a);
  3074. VPrint(stdout, " b=% \n", b);
  3075. }
  3076. #endif /* BIGDECIMAL_DEBUG */
  3077. if(!VpIsDefOP(c,a,b,3)) return 0; /* No significant digit */
  3078. if(VpIsZero(a) || VpIsZero(b)) {
  3079. /* at least a or b is zero */
  3080. VpSetZero(c,VpGetSign(a)*VpGetSign(b));
  3081. return 1; /* 0: 1 significant digit */
  3082. }
  3083. if(VpIsOne(a)) {
  3084. VpAsgn(c, b, VpGetSign(a));
  3085. goto Exit;
  3086. }
  3087. if(VpIsOne(b)) {
  3088. VpAsgn(c, a, VpGetSign(b));
  3089. goto Exit;
  3090. }
  3091. if((b->Prec) >(a->Prec)) {
  3092. /* Adjust so that digits(a)>digits(b) */
  3093. w = a;
  3094. a = b;
  3095. b = w;
  3096. }
  3097. w = NULL;
  3098. MxIndA = a->Prec - 1;
  3099. MxIndB = b->Prec - 1;
  3100. MxIndC = c->MaxPrec - 1;
  3101. MxIndAB = a->Prec + b->Prec - 1;
  3102. if(MxIndC < MxIndAB) { /* The Max. prec. of c < Prec(a)+Prec(b) */
  3103. w = c;
  3104. c = VpAlloc((size_t)((MxIndAB + 1) * BASE_FIG), "#0");
  3105. MxIndC = MxIndAB;
  3106. }
  3107. /* set LHSV c info */
  3108. c->exponent = a->exponent; /* set exponent */
  3109. if(!AddExponent(c,b->exponent)) {
  3110. if(w) VpFree(c);
  3111. return 0;
  3112. }
  3113. VpSetSign(c,VpGetSign(a)*VpGetSign(b)); /* set sign */
  3114. carry = 0;
  3115. nc = ind_c = MxIndAB;
  3116. memset(c->frac, 0, (nc + 1) * sizeof(BDIGIT)); /* Initialize c */
  3117. c->Prec = nc + 1; /* set precision */
  3118. for(nc = 0; nc < MxIndAB; ++nc, --ind_c) {
  3119. if(nc < MxIndB) { /* The left triangle of the Fig. */
  3120. ind_as = MxIndA - nc;
  3121. ind_ae = MxIndA;
  3122. ind_bs = MxIndB;
  3123. ind_be = MxIndB - nc;
  3124. } else if(nc <= MxIndA) { /* The middle rectangular of the Fig. */
  3125. ind_as = MxIndA - nc;
  3126. ind_ae = MxIndA -(nc - MxIndB);
  3127. ind_bs = MxIndB;
  3128. ind_be = 0;
  3129. } else if(nc > MxIndA) { /* The right triangle of the Fig. */
  3130. ind_as = 0;
  3131. ind_ae = MxIndAB - nc - 1;
  3132. ind_bs = MxIndB -(nc - MxIndA);
  3133. ind_be = 0;
  3134. }
  3135. for(i = ind_as; i <= ind_ae; ++i) {
  3136. s = (BDIGIT_DBL)a->frac[i] * b->frac[ind_bs--];
  3137. carry = (BDIGIT)(s / BASE);
  3138. s -= (BDIGIT_DBL)carry * BASE;
  3139. c->frac[ind_c] += (BDIGIT)s;
  3140. if(c->frac[ind_c] >= BASE) {
  3141. s = c->frac[ind_c] / BASE;
  3142. carry += (BDIGIT)s;
  3143. c->frac[ind_c] -= (BDIGIT)(s * BASE);
  3144. }
  3145. if(carry) {
  3146. ii = ind_c;
  3147. while(ii-- > 0) {
  3148. c->frac[ii] += carry;
  3149. if(c->frac[ii] >= BASE) {
  3150. carry = c->frac[ii] / BASE;
  3151. c->frac[ii] -= (carry * BASE);
  3152. } else {
  3153. break;
  3154. }
  3155. }
  3156. }
  3157. }
  3158. }
  3159. if(w != NULL) { /* free work variable */
  3160. VpNmlz(c);
  3161. VpAsgn(w, c, 1);
  3162. VpFree(c);
  3163. c = w;
  3164. } else {
  3165. VpLimitRound(c,0);
  3166. }
  3167. Exit:
  3168. #ifdef BIGDECIMAL_DEBUG
  3169. if(gfDebug) {
  3170. VPrint(stdout, "VpMult(c=a*b): c=% \n", c);
  3171. VPrint(stdout, " a=% \n", a);
  3172. VPrint(stdout, " b=% \n", b);
  3173. }
  3174. #endif /*BIGDECIMAL_DEBUG */
  3175. return c->Prec*BASE_FIG;
  3176. }
  3177. /*
  3178. * c = a / b, remainder = r
  3179. */
  3180. VP_EXPORT size_t
  3181. VpDivd(Real *c, Real *r, Real *a, Real *b)
  3182. {
  3183. size_t word_a, word_b, word_c, word_r;
  3184. size_t i, n, ind_a, ind_b, ind_c, ind_r;
  3185. size_t nLoop;
  3186. BDIGIT_DBL q, b1, b1p1, b1b2, b1b2p1, r1r2;
  3187. BDIGIT borrow, borrow1, borrow2;
  3188. BDIGIT_DBL qb;
  3189. #ifdef BIGDECIMAL_DEBUG
  3190. if(gfDebug) {
  3191. VPrint(stdout, " VpDivd(c=a/b) a=% \n", a);
  3192. VPrint(stdout, " b=% \n", b);
  3193. }
  3194. #endif /*BIGDECIMAL_DEBUG */
  3195. VpSetNaN(r);
  3196. if(!VpIsDefOP(c,a,b,4)) goto Exit;
  3197. if(VpIsZero(a)&&VpIsZero(b)) {
  3198. VpSetNaN(c);
  3199. return VpException(VP_EXCEPTION_NaN,"(VpDivd) 0/0 not defined(NaN)",0);
  3200. }
  3201. if(VpIsZero(b)) {
  3202. VpSetInf(c,VpGetSign(a)*VpGetSign(b));
  3203. return VpException(VP_EXCEPTION_ZERODIVIDE,"(VpDivd) Divide by zero",0);
  3204. }
  3205. if(VpIsZero(a)) {
  3206. /* numerator a is zero */
  3207. VpSetZero(c,VpGetSign(a)*VpGetSign(b));
  3208. VpSetZero(r,VpGetSign(a)*VpGetSign(b));
  3209. goto Exit;
  3210. }
  3211. if(VpIsOne(b)) {
  3212. /* divide by one */
  3213. VpAsgn(c, a, VpGetSign(b));
  3214. VpSetZero(r,VpGetSign(a));
  3215. goto Exit;
  3216. }
  3217. word_a = a->Prec;
  3218. word_b = b->Prec;
  3219. word_c = c->MaxPrec;
  3220. word_r = r->MaxPrec;
  3221. ind_c = 0;
  3222. ind_r = 1;
  3223. if(word_a >= word_r) goto space_error;
  3224. r->frac[0] = 0;
  3225. while(ind_r <= word_a) {
  3226. r->frac[ind_r] = a->frac[ind_r - 1];
  3227. ++ind_r;
  3228. }
  3229. while(ind_r < word_r) r->frac[ind_r++] = 0;
  3230. while(ind_c < word_c) c->frac[ind_c++] = 0;
  3231. /* initial procedure */
  3232. b1 = b1p1 = b->frac[0];
  3233. if(b->Prec <= 1) {
  3234. b1b2p1 = b1b2 = b1p1 * BASE;
  3235. } else {
  3236. b1p1 = b1 + 1;
  3237. b1b2p1 = b1b2 = b1 * BASE + b->frac[1];
  3238. if(b->Prec > 2) ++b1b2p1;
  3239. }
  3240. /* */
  3241. /* loop start */
  3242. ind_c = word_r - 1;
  3243. nLoop = Min(word_c,ind_c);
  3244. ind_c = 1;
  3245. while(ind_c < nLoop) {
  3246. if(r->frac[ind_c] == 0) {
  3247. ++ind_c;
  3248. continue;
  3249. }
  3250. r1r2 = (BDIGIT_DBL)r->frac[ind_c] * BASE + r->frac[ind_c + 1];
  3251. if(r1r2 == b1b2) {
  3252. /* The first two word digits is the same */
  3253. ind_b = 2;
  3254. ind_a = ind_c + 2;
  3255. while(ind_b < word_b) {
  3256. if(r->frac[ind_a] < b->frac[ind_b]) goto div_b1p1;
  3257. if(r->frac[ind_a] > b->frac[ind_b]) break;
  3258. ++ind_a;
  3259. ++ind_b;
  3260. }
  3261. /* The first few word digits of r and b is the same and */
  3262. /* the first different word digit of w is greater than that */
  3263. /* of b, so quotinet is 1 and just subtract b from r. */
  3264. borrow = 0; /* quotient=1, then just r-b */
  3265. ind_b = b->Prec - 1;
  3266. ind_r = ind_c + ind_b;
  3267. if(ind_r >= word_r) goto space_error;
  3268. n = ind_b;
  3269. for(i = 0; i <= n; ++i) {
  3270. if(r->frac[ind_r] < b->frac[ind_b] + borrow) {
  3271. r->frac[ind_r] += (BASE - (b->frac[ind_b] + borrow));
  3272. borrow = 1;
  3273. } else {
  3274. r->frac[ind_r] = r->frac[ind_r] - b->frac[ind_b] - borrow;
  3275. borrow = 0;
  3276. }
  3277. --ind_r;
  3278. --ind_b;
  3279. }
  3280. ++c->frac[ind_c];
  3281. goto carry;
  3282. }
  3283. /* The first two word digits is not the same, */
  3284. /* then compare magnitude, and divide actually. */
  3285. if(r1r2 >= b1b2p1) {
  3286. q = r1r2 / b1b2p1; /* q == (BDIGIT)q */
  3287. c->frac[ind_c] += (BDIGIT)q;
  3288. ind_r = b->Prec + ind_c - 1;
  3289. goto sub_mult;
  3290. }
  3291. div_b1p1:
  3292. if(ind_c + 1 >= word_c) goto out_side;
  3293. q = r1r2 / b1p1; /* q == (BDIGIT)q */
  3294. c->frac[ind_c + 1] += (BDIGIT)q;
  3295. ind_r = b->Prec + ind_c;
  3296. sub_mult:
  3297. borrow1 = borrow2 = 0;
  3298. ind_b = word_b - 1;
  3299. if(ind_r >= word_r) goto space_error;
  3300. n = ind_b;
  3301. for(i = 0; i <= n; ++i) {
  3302. /* now, perform r = r - q * b */
  3303. qb = q * b->frac[ind_b];
  3304. if (qb < BASE) borrow1 = 0;
  3305. else {
  3306. borrow1 = (BDIGIT)(qb / BASE);
  3307. qb -= (BDIGIT_DBL)borrow1 * BASE; /* get qb < BASE */
  3308. }
  3309. if(r->frac[ind_r] < qb) {
  3310. r->frac[ind_r] += (BDIGIT)(BASE - qb);
  3311. borrow2 = borrow2 + borrow1 + 1;
  3312. } else {
  3313. r->frac[ind_r] -= (BDIGIT)qb;
  3314. borrow2 += borrow1;
  3315. }
  3316. if(borrow2) {
  3317. if(r->frac[ind_r - 1] < borrow2) {
  3318. r->frac[ind_r - 1] += (BASE - borrow2);
  3319. borrow2 = 1;
  3320. } else {
  3321. r->frac[ind_r - 1] -= borrow2;
  3322. borrow2 = 0;
  3323. }
  3324. }
  3325. --ind_r;
  3326. --ind_b;
  3327. }
  3328. r->frac[ind_r] -= borrow2;
  3329. carry:
  3330. ind_r = ind_c;
  3331. while(c->frac[ind_r] >= BASE) {
  3332. c->frac[ind_r] -= BASE;
  3333. --ind_r;
  3334. ++c->frac[ind_r];
  3335. }
  3336. }
  3337. /* End of operation, now final arrangement */
  3338. out_side:
  3339. c->Prec = word_c;
  3340. c->exponent = a->exponent;
  3341. if(!AddExponent(c,2)) return 0;
  3342. if(!AddExponent(c,-(b->exponent))) return 0;
  3343. VpSetSign(c,VpGetSign(a)*VpGetSign(b));
  3344. VpNmlz(c); /* normalize c */
  3345. r->Prec = word_r;
  3346. r->exponent = a->exponent;
  3347. if(!AddExponent(r,1)) return 0;
  3348. VpSetSign(r,VpGetSign(a));
  3349. VpNmlz(r); /* normalize r(remainder) */
  3350. goto Exit;
  3351. space_error:
  3352. #ifdef BIGDECIMAL_DEBUG
  3353. if(gfDebug) {
  3354. printf(" word_a=%lu\n", word_a);
  3355. printf(" word_b=%lu\n", word_b);
  3356. printf(" word_c=%lu\n", word_c);
  3357. printf(" word_r=%lu\n", word_r);
  3358. printf(" ind_r =%lu\n", ind_r);
  3359. }
  3360. #endif /* BIGDECIMAL_DEBUG */
  3361. rb_bug("ERROR(VpDivd): space for remainder too small.");
  3362. Exit:
  3363. #ifdef BIGDECIMAL_DEBUG
  3364. if(gfDebug) {
  3365. VPrint(stdout, " VpDivd(c=a/b), c=% \n", c);
  3366. VPrint(stdout, " r=% \n", r);
  3367. }
  3368. #endif /* BIGDECIMAL_DEBUG */
  3369. return c->Prec*BASE_FIG;
  3370. }
  3371. /*
  3372. * Input a = 00000xxxxxxxx En(5 preceeding zeros)
  3373. * Output a = xxxxxxxx En-5
  3374. */
  3375. static int
  3376. VpNmlz(Real *a)
  3377. {
  3378. size_t ind_a, i;
  3379. if (!VpIsDef(a)) goto NoVal;
  3380. if (VpIsZero(a)) goto NoVal;
  3381. ind_a = a->Prec;
  3382. while (ind_a--) {
  3383. if (a->frac[ind_a]) {
  3384. a->Prec = ind_a + 1;
  3385. i = 0;
  3386. while (a->frac[i] == 0) ++i; /* skip the first few zeros */
  3387. if (i) {
  3388. a->Prec -= i;
  3389. if (!AddExponent(a, -(SIGNED_VALUE)i)) return 0;
  3390. memmove(&a->frac[0], &a->frac[i], a->Prec*sizeof(BDIGIT));
  3391. }
  3392. return 1;
  3393. }
  3394. }
  3395. /* a is zero(no non-zero digit) */
  3396. VpSetZero(a, VpGetSign(a));
  3397. return 0;
  3398. NoVal:
  3399. a->frac[0] = 0;
  3400. a->Prec = 1;
  3401. return 0;
  3402. }
  3403. /*
  3404. * VpComp = 0 ... if a=b,
  3405. * Pos ... a>b,
  3406. * Neg ... a<b.
  3407. * 999 ... result undefined(NaN)
  3408. */
  3409. VP_EXPORT int
  3410. VpComp(Real *a, Real *b)
  3411. {
  3412. int val;
  3413. size_t mx, ind;
  3414. int e;
  3415. val = 0;
  3416. if(VpIsNaN(a)||VpIsNaN(b)) return 999;
  3417. if(!VpIsDef(a)) {
  3418. if(!VpIsDef(b)) e = a->sign - b->sign;
  3419. else e = a->sign;
  3420. if(e>0) return 1;
  3421. else if(e<0) return -1;
  3422. else return 0;
  3423. }
  3424. if(!VpIsDef(b)) {
  3425. e = -b->sign;
  3426. if(e>0) return 1;
  3427. else return -1;
  3428. }
  3429. /* Zero check */
  3430. if(VpIsZero(a)) {
  3431. if(VpIsZero(b)) return 0; /* both zero */
  3432. val = -VpGetSign(b);
  3433. goto Exit;
  3434. }
  3435. if(VpIsZero(b)) {
  3436. val = VpGetSign(a);
  3437. goto Exit;
  3438. }
  3439. /* compare sign */
  3440. if(VpGetSign(a) > VpGetSign(b)) {
  3441. val = 1; /* a>b */
  3442. goto Exit;
  3443. }
  3444. if(VpGetSign(a) < VpGetSign(b)) {
  3445. val = -1; /* a<b */
  3446. goto Exit;
  3447. }
  3448. /* a and b have same sign, && signe!=0,then compare exponent */
  3449. if((a->exponent) >(b->exponent)) {
  3450. val = VpGetSign(a);
  3451. goto Exit;
  3452. }
  3453. if((a->exponent) <(b->exponent)) {
  3454. val = -VpGetSign(b);
  3455. goto Exit;
  3456. }
  3457. /* a and b have same exponent, then compare significand. */
  3458. mx =((a->Prec) <(b->Prec)) ?(a->Prec) :(b->Prec);
  3459. ind = 0;
  3460. while(ind < mx) {
  3461. if((a->frac[ind]) >(b->frac[ind])) {
  3462. val = VpGetSign(a);
  3463. goto Exit;
  3464. }
  3465. if((a->frac[ind]) <(b->frac[ind])) {
  3466. val = -VpGetSign(b);
  3467. goto Exit;
  3468. }
  3469. ++ind;
  3470. }
  3471. if((a->Prec) >(b->Prec)) {
  3472. val = VpGetSign(a);
  3473. } else if((a->Prec) <(b->Prec)) {
  3474. val = -VpGetSign(b);
  3475. }
  3476. Exit:
  3477. if (val> 1) val = 1;
  3478. else if(val<-1) val = -1;
  3479. #ifdef BIGDECIMAL_DEBUG
  3480. if(gfDebug) {
  3481. VPrint(stdout, " VpComp a=%\n", a);
  3482. VPrint(stdout, " b=%\n", b);
  3483. printf(" ans=%d\n", val);
  3484. }
  3485. #endif /* BIGDECIMAL_DEBUG */
  3486. return (int)val;
  3487. }
  3488. /*
  3489. * cntl_chr ... ASCIIZ Character, print control characters
  3490. * Available control codes:
  3491. * % ... VP variable. To print '%', use '%%'.
  3492. * \n ... new line
  3493. * \b ... backspace
  3494. * ... tab
  3495. * Note: % must must not appear more than once
  3496. * a ... VP variable to be printed
  3497. */
  3498. VP_EXPORT int
  3499. VPrint(FILE *fp, const char *cntl_chr, Real *a)
  3500. {
  3501. size_t i, j, nc, nd, ZeroSup;
  3502. BDIGIT m, e, nn;
  3503. /* Check if NaN & Inf. */
  3504. if(VpIsNaN(a)) {
  3505. fprintf(fp,SZ_NaN);
  3506. return 8;
  3507. }
  3508. if(VpIsPosInf(a)) {
  3509. fprintf(fp,SZ_INF);
  3510. return 8;
  3511. }
  3512. if(VpIsNegInf(a)) {
  3513. fprintf(fp,SZ_NINF);
  3514. return 9;
  3515. }
  3516. if(VpIsZero(a)) {
  3517. fprintf(fp,"0.0");
  3518. return 3;
  3519. }
  3520. j = 0;
  3521. nd = nc = 0; /* nd : number of digits in fraction part(every 10 digits, */
  3522. /* nd<=10). */
  3523. /* nc : number of caracters printed */
  3524. ZeroSup = 1; /* Flag not to print the leading zeros as 0.00xxxxEnn */
  3525. while(*(cntl_chr + j)) {
  3526. if((*(cntl_chr + j) == '%') &&(*(cntl_chr + j + 1) != '%')) {
  3527. nc = 0;
  3528. if(!VpIsZero(a)) {
  3529. if(VpGetSign(a) < 0) {
  3530. fprintf(fp, "-");
  3531. ++nc;
  3532. }
  3533. nc += fprintf(fp, "0.");
  3534. for(i=0; i < a->Prec; ++i) {
  3535. m = BASE1;
  3536. e = a->frac[i];
  3537. while(m) {
  3538. nn = e / m;
  3539. if((!ZeroSup) || nn) {
  3540. nc += fprintf(fp, "%lu", (unsigned long)nn); /* The leading zero(s) */
  3541. /* as 0.00xx will not */
  3542. /* be printed. */
  3543. ++nd;
  3544. ZeroSup = 0; /* Set to print succeeding zeros */
  3545. }
  3546. if(nd >= 10) { /* print ' ' after every 10 digits */
  3547. nd = 0;
  3548. nc += fprintf(fp, " ");
  3549. }
  3550. e = e - nn * m;
  3551. m /= 10;
  3552. }
  3553. }
  3554. nc += fprintf(fp, "E%"PRIdVALUE, VpExponent10(a));
  3555. } else {
  3556. nc += fprintf(fp, "0.0");
  3557. }
  3558. } else {
  3559. ++nc;
  3560. if(*(cntl_chr + j) == '\\') {
  3561. switch(*(cntl_chr + j + 1)) {
  3562. case 'n':
  3563. fprintf(fp, "\n");
  3564. ++j;
  3565. break;
  3566. case 't':
  3567. fprintf(fp, "\t");
  3568. ++j;
  3569. break;
  3570. case 'b':
  3571. fprintf(fp, "\n");
  3572. ++j;
  3573. break;
  3574. default:
  3575. fprintf(fp, "%c", *(cntl_chr + j));
  3576. break;
  3577. }
  3578. } else {
  3579. fprintf(fp, "%c", *(cntl_chr + j));
  3580. if(*(cntl_chr + j) == '%') ++j;
  3581. }
  3582. }
  3583. j++;
  3584. }
  3585. return (int)nc;
  3586. }
  3587. static void
  3588. VpFormatSt(char *psz, size_t fFmt)
  3589. {
  3590. size_t ie, i, nf = 0;
  3591. char ch;
  3592. if(fFmt<=0) return;
  3593. ie = strlen(psz);
  3594. for(i = 0; i < ie; ++i) {
  3595. ch = psz[i];
  3596. if(!ch) break;
  3597. if(ISSPACE(ch) || ch=='-' || ch=='+') continue;
  3598. if(ch == '.') { nf = 0;continue;}
  3599. if(ch == 'E') break;
  3600. nf++;
  3601. if(nf > fFmt) {
  3602. memmove(psz + i + 1, psz + i, ie - i + 1);
  3603. ++ie;
  3604. nf = 0;
  3605. psz[i] = ' ';
  3606. }
  3607. }
  3608. }
  3609. VP_EXPORT ssize_t
  3610. VpExponent10(Real *a)
  3611. {
  3612. ssize_t ex;
  3613. size_t n;
  3614. if (!VpHasVal(a)) return 0;
  3615. ex = a->exponent * (ssize_t)BASE_FIG;
  3616. n = BASE1;
  3617. while ((a->frac[0] / n) == 0) {
  3618. --ex;
  3619. n /= 10;
  3620. }
  3621. return ex;
  3622. }
  3623. VP_EXPORT void
  3624. VpSzMantissa(Real *a,char *psz)
  3625. {
  3626. size_t i, n, ZeroSup;
  3627. BDIGIT_DBL m, e, nn;
  3628. if(VpIsNaN(a)) {
  3629. sprintf(psz,SZ_NaN);
  3630. return;
  3631. }
  3632. if(VpIsPosInf(a)) {
  3633. sprintf(psz,SZ_INF);
  3634. return;
  3635. }
  3636. if(VpIsNegInf(a)) {
  3637. sprintf(psz,SZ_NINF);
  3638. return;
  3639. }
  3640. ZeroSup = 1; /* Flag not to print the leading zeros as 0.00xxxxEnn */
  3641. if(!VpIsZero(a)) {
  3642. if(VpGetSign(a) < 0) *psz++ = '-';
  3643. n = a->Prec;
  3644. for (i=0; i < n; ++i) {
  3645. m = BASE1;
  3646. e = a->frac[i];
  3647. while (m) {
  3648. nn = e / m;
  3649. if((!ZeroSup) || nn) {
  3650. sprintf(psz, "%lu", (unsigned long)nn); /* The leading zero(s) */
  3651. psz += strlen(psz);
  3652. /* as 0.00xx will be ignored. */
  3653. ZeroSup = 0; /* Set to print succeeding zeros */
  3654. }
  3655. e = e - nn * m;
  3656. m /= 10;
  3657. }
  3658. }
  3659. *psz = 0;
  3660. while(psz[-1]=='0') *(--psz) = 0;
  3661. } else {
  3662. if(VpIsPosZero(a)) sprintf(psz, "0");
  3663. else sprintf(psz, "-0");
  3664. }
  3665. }
  3666. VP_EXPORT int
  3667. VpToSpecialString(Real *a,char *psz,int fPlus)
  3668. /* fPlus =0:default, =1: set ' ' before digits , =2: set '+' before digits. */
  3669. {
  3670. if(VpIsNaN(a)) {
  3671. sprintf(psz,SZ_NaN);
  3672. return 1;
  3673. }
  3674. if(VpIsPosInf(a)) {
  3675. if(fPlus==1) {
  3676. *psz++ = ' ';
  3677. } else if(fPlus==2) {
  3678. *psz++ = '+';
  3679. }
  3680. sprintf(psz,SZ_INF);
  3681. return 1;
  3682. }
  3683. if(VpIsNegInf(a)) {
  3684. sprintf(psz,SZ_NINF);
  3685. return 1;
  3686. }
  3687. if(VpIsZero(a)) {
  3688. if(VpIsPosZero(a)) {
  3689. if(fPlus==1) sprintf(psz, " 0.0");
  3690. else if(fPlus==2) sprintf(psz, "+0.0");
  3691. else sprintf(psz, "0.0");
  3692. } else sprintf(psz, "-0.0");
  3693. return 1;
  3694. }
  3695. return 0;
  3696. }
  3697. VP_EXPORT void
  3698. VpToString(Real *a, char *psz, size_t fFmt, int fPlus)
  3699. /* fPlus =0:default, =1: set ' ' before digits , =2:set '+' before digits. */
  3700. {
  3701. size_t i, n, ZeroSup;
  3702. BDIGIT shift, m, e, nn;
  3703. char *pszSav = psz;
  3704. ssize_t ex;
  3705. if (VpToSpecialString(a, psz, fPlus)) return;
  3706. ZeroSup = 1; /* Flag not to print the leading zeros as 0.00xxxxEnn */
  3707. if (VpGetSign(a) < 0) *psz++ = '-';
  3708. else if (fPlus == 1) *psz++ = ' ';
  3709. else if (fPlus == 2) *psz++ = '+';
  3710. *psz++ = '0';
  3711. *psz++ = '.';
  3712. n = a->Prec;
  3713. for(i=0;i < n;++i) {
  3714. m = BASE1;
  3715. e = a->frac[i];
  3716. while(m) {
  3717. nn = e / m;
  3718. if((!ZeroSup) || nn) {
  3719. sprintf(psz, "%lu", (unsigned long)nn); /* The reading zero(s) */
  3720. psz += strlen(psz);
  3721. /* as 0.00xx will be ignored. */
  3722. ZeroSup = 0; /* Set to print succeeding zeros */
  3723. }
  3724. e = e - nn * m;
  3725. m /= 10;
  3726. }
  3727. }
  3728. ex = a->exponent * (ssize_t)BASE_FIG;
  3729. shift = BASE1;
  3730. while(a->frac[0] / shift == 0) {
  3731. --ex;
  3732. shift /= 10;
  3733. }
  3734. while(psz[-1]=='0') *(--psz) = 0;
  3735. sprintf(psz, "E%"PRIdVALUE, ex);
  3736. if(fFmt) VpFormatSt(pszSav, fFmt);
  3737. }
  3738. VP_EXPORT void
  3739. VpToFString(Real *a, char *psz, size_t fFmt, int fPlus)
  3740. /* fPlus =0:default,=1: set ' ' before digits ,set '+' before digits. */
  3741. {
  3742. size_t i, n;
  3743. BDIGIT m, e, nn;
  3744. char *pszSav = psz;
  3745. ssize_t ex;
  3746. if(VpToSpecialString(a,psz,fPlus)) return;
  3747. if(VpGetSign(a) < 0) *psz++ = '-';
  3748. else if(fPlus==1) *psz++ = ' ';
  3749. else if(fPlus==2) *psz++ = '+';
  3750. n = a->Prec;
  3751. ex = a->exponent;
  3752. if(ex<=0) {
  3753. *psz++ = '0';*psz++ = '.';
  3754. while(ex<0) {
  3755. for(i=0;i<BASE_FIG;++i) *psz++ = '0';
  3756. ++ex;
  3757. }
  3758. ex = -1;
  3759. }
  3760. for(i=0;i < n;++i) {
  3761. --ex;
  3762. if(i==0 && ex >= 0) {
  3763. sprintf(psz, "%lu", (unsigned long)a->frac[i]);
  3764. psz += strlen(psz);
  3765. } else {
  3766. m = BASE1;
  3767. e = a->frac[i];
  3768. while(m) {
  3769. nn = e / m;
  3770. *psz++ = (char)(nn + '0');
  3771. e = e - nn * m;
  3772. m /= 10;
  3773. }
  3774. }
  3775. if(ex == 0) *psz++ = '.';
  3776. }
  3777. while(--ex>=0) {
  3778. m = BASE;
  3779. while(m/=10) *psz++ = '0';
  3780. if(ex == 0) *psz++ = '.';
  3781. }
  3782. *psz = 0;
  3783. while(psz[-1]=='0') *(--psz) = 0;
  3784. if(psz[-1]=='.') sprintf(psz, "0");
  3785. if(fFmt) VpFormatSt(pszSav, fFmt);
  3786. }
  3787. /*
  3788. * [Output]
  3789. * a[] ... variable to be assigned the value.
  3790. * [Input]
  3791. * int_chr[] ... integer part(may include '+/-').
  3792. * ni ... number of characters in int_chr[],not including '+/-'.
  3793. * frac[] ... fraction part.
  3794. * nf ... number of characters in frac[].
  3795. * exp_chr[] ... exponent part(including '+/-').
  3796. * ne ... number of characters in exp_chr[],not including '+/-'.
  3797. */
  3798. VP_EXPORT int
  3799. VpCtoV(Real *a, const char *int_chr, size_t ni, const char *frac, size_t nf, const char *exp_chr, size_t ne)
  3800. {
  3801. size_t i, j, ind_a, ma, mi, me;
  3802. size_t loc;
  3803. SIGNED_VALUE e, es, eb, ef;
  3804. int sign, signe, exponent_overflow;
  3805. /* get exponent part */
  3806. e = 0;
  3807. ma = a->MaxPrec;
  3808. mi = ni;
  3809. me = ne;
  3810. signe = 1;
  3811. exponent_overflow = 0;
  3812. memset(a->frac, 0, ma * sizeof(BDIGIT));
  3813. if (ne > 0) {
  3814. i = 0;
  3815. if (exp_chr[0] == '-') {
  3816. signe = -1;
  3817. ++i;
  3818. ++me;
  3819. }
  3820. else if (exp_chr[0] == '+') {
  3821. ++i;
  3822. ++me;
  3823. }
  3824. while (i < me) {
  3825. es = e * (SIGNED_VALUE)BASE_FIG;
  3826. e = e * 10 + exp_chr[i] - '0';
  3827. if (es > (SIGNED_VALUE)(e*BASE_FIG)) {
  3828. exponent_overflow = 1;
  3829. e = es; /* keep sign */
  3830. break;
  3831. }
  3832. ++i;
  3833. }
  3834. }
  3835. /* get integer part */
  3836. i = 0;
  3837. sign = 1;
  3838. if(1 /*ni >= 0*/) {
  3839. if(int_chr[0] == '-') {
  3840. sign = -1;
  3841. ++i;
  3842. ++mi;
  3843. } else if(int_chr[0] == '+') {
  3844. ++i;
  3845. ++mi;
  3846. }
  3847. }
  3848. e = signe * e; /* e: The value of exponent part. */
  3849. e = e + ni; /* set actual exponent size. */
  3850. if (e > 0) signe = 1;
  3851. else signe = -1;
  3852. /* Adjust the exponent so that it is the multiple of BASE_FIG. */
  3853. j = 0;
  3854. ef = 1;
  3855. while (ef) {
  3856. if (e >= 0) eb = e;
  3857. else eb = -e;
  3858. ef = eb / (SIGNED_VALUE)BASE_FIG;
  3859. ef = eb - ef * (SIGNED_VALUE)BASE_FIG;
  3860. if (ef) {
  3861. ++j; /* Means to add one more preceeding zero */
  3862. ++e;
  3863. }
  3864. }
  3865. eb = e / (SIGNED_VALUE)BASE_FIG;
  3866. if (exponent_overflow) {
  3867. int zero = 1;
  3868. for ( ; i < mi && zero; i++) zero = int_chr[i] == '0';
  3869. for (i = 0; i < nf && zero; i++) zero = frac[i] == '0';
  3870. if (!zero && signe > 0) {
  3871. VpSetInf(a, sign);
  3872. VpException(VP_EXCEPTION_INFINITY, "exponent overflow",0);
  3873. }
  3874. else VpSetZero(a, sign);
  3875. return 1;
  3876. }
  3877. ind_a = 0;
  3878. while (i < mi) {
  3879. a->frac[ind_a] = 0;
  3880. while ((j < BASE_FIG) && (i < mi)) {
  3881. a->frac[ind_a] = a->frac[ind_a] * 10 + int_chr[i] - '0';
  3882. ++j;
  3883. ++i;
  3884. }
  3885. if (i < mi) {
  3886. ++ind_a;
  3887. if (ind_a >= ma) goto over_flow;
  3888. j = 0;
  3889. }
  3890. }
  3891. loc = 1;
  3892. /* get fraction part */
  3893. i = 0;
  3894. while(i < nf) {
  3895. while((j < BASE_FIG) && (i < nf)) {
  3896. a->frac[ind_a] = a->frac[ind_a] * 10 + frac[i] - '0';
  3897. ++j;
  3898. ++i;
  3899. }
  3900. if(i < nf) {
  3901. ++ind_a;
  3902. if(ind_a >= ma) goto over_flow;
  3903. j = 0;
  3904. }
  3905. }
  3906. goto Final;
  3907. over_flow:
  3908. rb_warn("Conversion from String to BigDecimal overflow (last few digits discarded).");
  3909. Final:
  3910. if (ind_a >= ma) ind_a = ma - 1;
  3911. while (j < BASE_FIG) {
  3912. a->frac[ind_a] = a->frac[ind_a] * 10;
  3913. ++j;
  3914. }
  3915. a->Prec = ind_a + 1;
  3916. a->exponent = eb;
  3917. VpSetSign(a,sign);
  3918. VpNmlz(a);
  3919. return 1;
  3920. }
  3921. /*
  3922. * [Input]
  3923. * *m ... Real
  3924. * [Output]
  3925. * *d ... fraction part of m(d = 0.xxxxxxx). where # of 'x's is fig.
  3926. * *e ... exponent of m.
  3927. * DBLE_FIG ... Number of digits in a double variable.
  3928. *
  3929. * m -> d*10**e, 0<d<BASE
  3930. * [Returns]
  3931. * 0 ... Zero
  3932. * 1 ... Normal
  3933. * 2 ... Infinity
  3934. * -1 ... NaN
  3935. */
  3936. VP_EXPORT int
  3937. VpVtoD(double *d, SIGNED_VALUE *e, Real *m)
  3938. {
  3939. size_t ind_m, mm, fig;
  3940. double div;
  3941. int f = 1;
  3942. if(VpIsNaN(m)) {
  3943. *d = VpGetDoubleNaN();
  3944. *e = 0;
  3945. f = -1; /* NaN */
  3946. goto Exit;
  3947. } else
  3948. if(VpIsPosZero(m)) {
  3949. *d = 0.0;
  3950. *e = 0;
  3951. f = 0;
  3952. goto Exit;
  3953. } else
  3954. if(VpIsNegZero(m)) {
  3955. *d = VpGetDoubleNegZero();
  3956. *e = 0;
  3957. f = 0;
  3958. goto Exit;
  3959. } else
  3960. if(VpIsPosInf(m)) {
  3961. *d = VpGetDoublePosInf();
  3962. *e = 0;
  3963. f = 2;
  3964. goto Exit;
  3965. } else
  3966. if(VpIsNegInf(m)) {
  3967. *d = VpGetDoubleNegInf();
  3968. *e = 0;
  3969. f = 2;
  3970. goto Exit;
  3971. }
  3972. /* Normal number */
  3973. fig =(DBLE_FIG + BASE_FIG - 1) / BASE_FIG;
  3974. ind_m = 0;
  3975. mm = Min(fig,(m->Prec));
  3976. *d = 0.0;
  3977. div = 1.;
  3978. while(ind_m < mm) {
  3979. div /= (double)BASE;
  3980. *d = *d + (double)m->frac[ind_m++] * div;
  3981. }
  3982. *e = m->exponent * (SIGNED_VALUE)BASE_FIG;
  3983. *d *= VpGetSign(m);
  3984. Exit:
  3985. #ifdef BIGDECIMAL_DEBUG
  3986. if(gfDebug) {
  3987. VPrint(stdout, " VpVtoD: m=%\n", m);
  3988. printf(" d=%e * 10 **%ld\n", *d, *e);
  3989. printf(" DBLE_FIG = %d\n", DBLE_FIG);
  3990. }
  3991. #endif /*BIGDECIMAL_DEBUG */
  3992. return f;
  3993. }
  3994. /*
  3995. * m <- d
  3996. */
  3997. VP_EXPORT void
  3998. VpDtoV(Real *m, double d)
  3999. {
  4000. size_t ind_m, mm;
  4001. SIGNED_VALUE ne;
  4002. BDIGIT i;
  4003. double val, val2;
  4004. if(isnan(d)) {
  4005. VpSetNaN(m);
  4006. goto Exit;
  4007. }
  4008. if(isinf(d)) {
  4009. if(d>0.0) VpSetPosInf(m);
  4010. else VpSetNegInf(m);
  4011. goto Exit;
  4012. }
  4013. if(d == 0.0) {
  4014. VpSetZero(m,1);
  4015. goto Exit;
  4016. }
  4017. val =(d > 0.) ? d :(-d);
  4018. ne = 0;
  4019. if(val >= 1.0) {
  4020. while(val >= 1.0) {
  4021. val /= (double)BASE;
  4022. ++ne;
  4023. }
  4024. } else {
  4025. val2 = 1.0 /(double)BASE;
  4026. while(val < val2) {
  4027. val *= (double)BASE;
  4028. --ne;
  4029. }
  4030. }
  4031. /* Now val = 0.xxxxx*BASE**ne */
  4032. mm = m->MaxPrec;
  4033. memset(m->frac, 0, mm * sizeof(BDIGIT));
  4034. for(ind_m = 0;val > 0.0 && ind_m < mm;ind_m++) {
  4035. val *= (double)BASE;
  4036. i = (BDIGIT)val;
  4037. val -= (double)i;
  4038. m->frac[ind_m] = i;
  4039. }
  4040. if(ind_m >= mm) ind_m = mm - 1;
  4041. VpSetSign(m, (d > 0.0) ? 1 : -1);
  4042. m->Prec = ind_m + 1;
  4043. m->exponent = ne;
  4044. VpInternalRound(m, 0, (m->Prec > 0) ? m->frac[m->Prec-1] : 0,
  4045. (BDIGIT)(val*(double)BASE));
  4046. Exit:
  4047. #ifdef BIGDECIMAL_DEBUG
  4048. if(gfDebug) {
  4049. printf("VpDtoV d=%30.30e\n", d);
  4050. VPrint(stdout, " m=%\n", m);
  4051. }
  4052. #endif /* BIGDECIMAL_DEBUG */
  4053. return;
  4054. }
  4055. /*
  4056. * m <- ival
  4057. */
  4058. #if 0 /* unused */
  4059. VP_EXPORT void
  4060. VpItoV(Real *m, SIGNED_VALUE ival)
  4061. {
  4062. size_t mm, ind_m;
  4063. size_t val, v1, v2, v;
  4064. int isign;
  4065. SIGNED_VALUE ne;
  4066. if(ival == 0) {
  4067. VpSetZero(m,1);
  4068. goto Exit;
  4069. }
  4070. isign = 1;
  4071. val = ival;
  4072. if(ival < 0) {
  4073. isign = -1;
  4074. val =(size_t)(-ival);
  4075. }
  4076. ne = 0;
  4077. ind_m = 0;
  4078. mm = m->MaxPrec;
  4079. while(ind_m < mm) {
  4080. m->frac[ind_m] = 0;
  4081. ++ind_m;
  4082. }
  4083. ind_m = 0;
  4084. while(val > 0) {
  4085. if(val) {
  4086. v1 = val;
  4087. v2 = 1;
  4088. while(v1 >= BASE) {
  4089. v1 /= BASE;
  4090. v2 *= BASE;
  4091. }
  4092. val = val - v2 * v1;
  4093. v = v1;
  4094. } else {
  4095. v = 0;
  4096. }
  4097. m->frac[ind_m] = v;
  4098. ++ind_m;
  4099. ++ne;
  4100. }
  4101. m->Prec = ind_m - 1;
  4102. m->exponent = ne;
  4103. VpSetSign(m,isign);
  4104. VpNmlz(m);
  4105. Exit:
  4106. #ifdef BIGDECIMAL_DEBUG
  4107. if(gfDebug) {
  4108. printf(" VpItoV i=%d\n", ival);
  4109. VPrint(stdout, " m=%\n", m);
  4110. }
  4111. #endif /* BIGDECIMAL_DEBUG */
  4112. return;
  4113. }
  4114. #endif
  4115. /*
  4116. * y = SQRT(x), y*y - x =>0
  4117. */
  4118. VP_EXPORT int
  4119. VpSqrt(Real *y, Real *x)
  4120. {
  4121. Real *f = NULL;
  4122. Real *r = NULL;
  4123. size_t y_prec, f_prec;
  4124. SIGNED_VALUE n, e;
  4125. SIGNED_VALUE prec;
  4126. ssize_t nr;
  4127. double val;
  4128. /* Zero, NaN or Infinity ? */
  4129. if(!VpHasVal(x)) {
  4130. if(VpIsZero(x)||VpGetSign(x)>0) {
  4131. VpAsgn(y,x,1);
  4132. goto Exit;
  4133. }
  4134. VpSetNaN(y);
  4135. return VpException(VP_EXCEPTION_OP,"(VpSqrt) SQRT(NaN or negative value)",0);
  4136. goto Exit;
  4137. }
  4138. /* Negative ? */
  4139. if(VpGetSign(x) < 0) {
  4140. VpSetNaN(y);
  4141. return VpException(VP_EXCEPTION_OP,"(VpSqrt) SQRT(negative value)",0);
  4142. }
  4143. /* One ? */
  4144. if(VpIsOne(x)) {
  4145. VpSetOne(y);
  4146. goto Exit;
  4147. }
  4148. n = (SIGNED_VALUE)y->MaxPrec;
  4149. if (x->MaxPrec > (size_t)n) n = (ssize_t)x->MaxPrec;
  4150. /* allocate temporally variables */
  4151. f = VpAlloc(y->MaxPrec * (BASE_FIG + 2), "#1");
  4152. r = VpAlloc((n + n) * (BASE_FIG + 2), "#1");
  4153. nr = 0;
  4154. y_prec = y->MaxPrec;
  4155. f_prec = f->MaxPrec;
  4156. prec = x->exponent - (ssize_t)y_prec;
  4157. if (x->exponent > 0)
  4158. ++prec;
  4159. else
  4160. --prec;
  4161. VpVtoD(&val, &e, x); /* val <- x */
  4162. e /= (SIGNED_VALUE)BASE_FIG;
  4163. n = e / 2;
  4164. if (e - n * 2 != 0) {
  4165. val /= BASE;
  4166. n = (e + 1) / 2;
  4167. }
  4168. VpDtoV(y, sqrt(val)); /* y <- sqrt(val) */
  4169. y->exponent += n;
  4170. n = (SIGNED_VALUE)((DBLE_FIG + BASE_FIG - 1) / BASE_FIG);
  4171. y->MaxPrec = Min((size_t)n , y_prec);
  4172. f->MaxPrec = y->MaxPrec + 1;
  4173. n = (SIGNED_VALUE)(y_prec * BASE_FIG);
  4174. if (n < (SIGNED_VALUE)maxnr) n = (SIGNED_VALUE)maxnr;
  4175. do {
  4176. y->MaxPrec *= 2;
  4177. if (y->MaxPrec > y_prec) y->MaxPrec = y_prec;
  4178. f->MaxPrec = y->MaxPrec;
  4179. VpDivd(f, r, x, y); /* f = x/y */
  4180. VpAddSub(r, f, y, -1); /* r = f - y */
  4181. VpMult(f, VpPt5, r); /* f = 0.5*r */
  4182. if(VpIsZero(f)) goto converge;
  4183. VpAddSub(r, f, y, 1); /* r = y + f */
  4184. VpAsgn(y, r, 1); /* y = r */
  4185. if(f->exponent <= prec) goto converge;
  4186. } while(++nr < n);
  4187. /* */
  4188. #ifdef BIGDECIMAL_DEBUG
  4189. if(gfDebug) {
  4190. printf("ERROR(VpSqrt): did not converge within %ld iterations.\n",
  4191. nr);
  4192. }
  4193. #endif /* BIGDECIMAL_DEBUG */
  4194. y->MaxPrec = y_prec;
  4195. converge:
  4196. VpChangeSign(y, 1);
  4197. #ifdef BIGDECIMAL_DEBUG
  4198. if(gfDebug) {
  4199. VpMult(r, y, y);
  4200. VpAddSub(f, x, r, -1);
  4201. printf("VpSqrt: iterations = %"PRIdSIZE"\n", nr);
  4202. VPrint(stdout, " y =% \n", y);
  4203. VPrint(stdout, " x =% \n", x);
  4204. VPrint(stdout, " x-y*y = % \n", f);
  4205. }
  4206. #endif /* BIGDECIMAL_DEBUG */
  4207. y->MaxPrec = y_prec;
  4208. Exit:
  4209. VpFree(f);
  4210. VpFree(r);
  4211. return 1;
  4212. }
  4213. /*
  4214. *
  4215. * nf: digit position for operation.
  4216. *
  4217. */
  4218. VP_EXPORT int
  4219. VpMidRound(Real *y, unsigned short f, ssize_t nf)
  4220. /*
  4221. * Round reletively from the decimal point.
  4222. * f: rounding mode
  4223. * nf: digit location to round from the the decimal point.
  4224. */
  4225. {
  4226. /* fracf: any positive digit under rounding position? */
  4227. /* fracf_1further: any positive digits under one further than the rounding position? */
  4228. /* exptoadd: number of digits needed to compensate negative nf */
  4229. int fracf, fracf_1further;
  4230. ssize_t n,i,ix,ioffset, exptoadd;
  4231. BDIGIT v, shifter;
  4232. BDIGIT div;
  4233. nf += y->exponent * (ssize_t)BASE_FIG;
  4234. exptoadd=0;
  4235. if (nf < 0) {
  4236. /* rounding position too left(large). */
  4237. if((f!=VP_ROUND_CEIL) && (f!=VP_ROUND_FLOOR)) {
  4238. VpSetZero(y,VpGetSign(y)); /* truncate everything */
  4239. return 0;
  4240. }
  4241. exptoadd = -nf;
  4242. nf = 0;
  4243. }
  4244. ix = nf / (ssize_t)BASE_FIG;
  4245. if ((size_t)ix >= y->Prec) return 0; /* rounding position too right(small). */
  4246. v = y->frac[ix];
  4247. ioffset = nf - ix*(ssize_t)BASE_FIG;
  4248. n = (ssize_t)BASE_FIG - ioffset - 1;
  4249. for (shifter=1,i=0; i<n; ++i) shifter *= 10;
  4250. /* so the representation used (in y->frac) is an array of BDIGIT, where
  4251. each BDIGIT contains a value between 0 and BASE-1, consisting of BASE_FIG
  4252. decimal places.
  4253. (that numbers of decimal places are typed as ssize_t is somewhat confusing)
  4254. nf is now position (in decimal places) of the digit from the start of
  4255. the array.
  4256. ix is the position (in BDIGITS) of the BDIGIT containing the decimal digit,
  4257. from the start of the array.
  4258. v is the value of this BDIGIT
  4259. ioffset is the number of extra decimal places along of this decimal digit
  4260. within v.
  4261. n is the number of decimal digits remaining within v after this decimal digit
  4262. shifter is 10**n,
  4263. v % shifter are the remaining digits within v
  4264. v % (shifter * 10) are the digit together with the remaining digits within v
  4265. v / shifter are the digit's predecessors together with the digit
  4266. div = v / shifter / 10 is just the digit's precessors
  4267. (v / shifter) - div*10 is just the digit, which is what v ends up being reassigned to.
  4268. */
  4269. fracf = (v % (shifter * 10) > 0);
  4270. fracf_1further = ((v % shifter) > 0);
  4271. v /= shifter;
  4272. div = v / 10;
  4273. v = v - div*10;
  4274. /* now v is just the digit required.
  4275. now fracf is whether the digit or any of the remaining digits within v are non-zero
  4276. now fracf_1further is whether any of the remaining digits within v are non-zero
  4277. */
  4278. /* now check all the remaining BDIGITS for zero-ness a whole BDIGIT at a time.
  4279. if we spot any non-zeroness, that means that we foudn a positive digit under
  4280. rounding position, and we also found a positive digit under one further than
  4281. the rounding position, so both searches (to see if any such non-zero digit exists)
  4282. can stop */
  4283. for (i=ix+1; (size_t)i < y->Prec; i++) {
  4284. if (y->frac[i] % BASE) {
  4285. fracf = fracf_1further = 1;
  4286. break;
  4287. }
  4288. }
  4289. /* now fracf = does any positive digit exist under the rounding position?
  4290. now fracf_1further = does any positive digit exist under one further than the
  4291. rounding position?
  4292. now v = the first digit under the rounding position */
  4293. /* drop digits after pointed digit */
  4294. memset(y->frac+ix+1, 0, (y->Prec - (ix+1)) * sizeof(BDIGIT));
  4295. switch(f) {
  4296. case VP_ROUND_DOWN: /* Truncate */
  4297. break;
  4298. case VP_ROUND_UP: /* Roundup */
  4299. if (fracf) ++div;
  4300. break;
  4301. case VP_ROUND_HALF_UP:
  4302. if (v>=5) ++div;
  4303. break;
  4304. case VP_ROUND_HALF_DOWN:
  4305. if (v > 5 || (v == 5 && fracf_1further)) ++div;
  4306. break;
  4307. case VP_ROUND_CEIL:
  4308. if (fracf && (VpGetSign(y)>0)) ++div;
  4309. break;
  4310. case VP_ROUND_FLOOR:
  4311. if (fracf && (VpGetSign(y)<0)) ++div;
  4312. break;
  4313. case VP_ROUND_HALF_EVEN: /* Banker's rounding */
  4314. if (v > 5) ++div;
  4315. else if (v == 5) {
  4316. if (fracf_1further) {
  4317. ++div;
  4318. }
  4319. else {
  4320. if (ioffset == 0) {
  4321. /* v is the first decimal digit of its BDIGIT;
  4322. need to grab the previous BDIGIT if present
  4323. to check for evenness of the previous decimal
  4324. digit (which is same as that of the BDIGIT since
  4325. base 10 has a factor of 2) */
  4326. if (ix && (y->frac[ix-1] % 2)) ++div;
  4327. }
  4328. else {
  4329. if (div % 2) ++div;
  4330. }
  4331. }
  4332. }
  4333. break;
  4334. }
  4335. for (i=0; i<=n; ++i) div *= 10;
  4336. if (div>=BASE) {
  4337. if(ix) {
  4338. y->frac[ix] = 0;
  4339. VpRdup(y,ix);
  4340. } else {
  4341. short s = VpGetSign(y);
  4342. SIGNED_VALUE e = y->exponent;
  4343. VpSetOne(y);
  4344. VpSetSign(y, s);
  4345. y->exponent = e+1;
  4346. }
  4347. } else {
  4348. y->frac[ix] = div;
  4349. VpNmlz(y);
  4350. }
  4351. if (exptoadd > 0) {
  4352. y->exponent += (SIGNED_VALUE)(exptoadd/BASE_FIG);
  4353. exptoadd %= (ssize_t)BASE_FIG;
  4354. for(i=0;i<exptoadd;i++) {
  4355. y->frac[0] *= 10;
  4356. if (y->frac[0] >= BASE) {
  4357. y->frac[0] /= BASE;
  4358. y->exponent++;
  4359. }
  4360. }
  4361. }
  4362. return 1;
  4363. }
  4364. VP_EXPORT int
  4365. VpLeftRound(Real *y, unsigned short f, ssize_t nf)
  4366. /*
  4367. * Round from the left hand side of the digits.
  4368. */
  4369. {
  4370. BDIGIT v;
  4371. if (!VpHasVal(y)) return 0; /* Unable to round */
  4372. v = y->frac[0];
  4373. nf -= VpExponent(y)*(ssize_t)BASE_FIG;
  4374. while ((v /= 10) != 0) nf--;
  4375. nf += (ssize_t)BASE_FIG-1;
  4376. return VpMidRound(y,f,nf);
  4377. }
  4378. VP_EXPORT int
  4379. VpActiveRound(Real *y, Real *x, unsigned short f, ssize_t nf)
  4380. {
  4381. /* First,assign whole value in truncation mode */
  4382. if (VpAsgn(y, x, 10) <= 1) return 0; /* Zero,NaN,or Infinity */
  4383. return VpMidRound(y,f,nf);
  4384. }
  4385. static int
  4386. VpLimitRound(Real *c, size_t ixDigit)
  4387. {
  4388. size_t ix = VpGetPrecLimit();
  4389. if(!VpNmlz(c)) return -1;
  4390. if(!ix) return 0;
  4391. if(!ixDigit) ixDigit = c->Prec-1;
  4392. if((ix+BASE_FIG-1)/BASE_FIG > ixDigit+1) return 0;
  4393. return VpLeftRound(c, (int)VpGetRoundMode(), (ssize_t)ix);
  4394. }
  4395. /* If I understand correctly, this is only ever used to round off the final decimal
  4396. digit of precision */
  4397. static void
  4398. VpInternalRound(Real *c, size_t ixDigit, BDIGIT vPrev, BDIGIT v)
  4399. {
  4400. int f = 0;
  4401. unsigned short const rounding_mode = VpGetRoundMode();
  4402. if (VpLimitRound(c, ixDigit)) return;
  4403. if (!v) return;
  4404. v /= BASE1;
  4405. switch (rounding_mode) {
  4406. case VP_ROUND_DOWN:
  4407. break;
  4408. case VP_ROUND_UP:
  4409. if (v) f = 1;
  4410. break;
  4411. case VP_ROUND_HALF_UP:
  4412. if (v >= 5) f = 1;
  4413. break;
  4414. case VP_ROUND_HALF_DOWN:
  4415. /* this is ok - because this is the last digit of precision,
  4416. the case where v == 5 and some further digits are nonzero
  4417. will never occur */
  4418. if (v >= 6) f = 1;
  4419. break;
  4420. case VP_ROUND_CEIL:
  4421. if (v && (VpGetSign(c) > 0)) f = 1;
  4422. break;
  4423. case VP_ROUND_FLOOR:
  4424. if (v && (VpGetSign(c) < 0)) f = 1;
  4425. break;
  4426. case VP_ROUND_HALF_EVEN: /* Banker's rounding */
  4427. /* as per VP_ROUND_HALF_DOWN, because this is the last digit of precision,
  4428. there is no case to worry about where v == 5 and some further digits are nonzero */
  4429. if (v > 5) f = 1;
  4430. else if (v == 5 && vPrev % 2) f = 1;
  4431. break;
  4432. }
  4433. if (f) {
  4434. VpRdup(c, ixDigit);
  4435. VpNmlz(c);
  4436. }
  4437. }
  4438. /*
  4439. * Rounds up m(plus one to final digit of m).
  4440. */
  4441. static int
  4442. VpRdup(Real *m, size_t ind_m)
  4443. {
  4444. BDIGIT carry;
  4445. if (!ind_m) ind_m = m->Prec;
  4446. carry = 1;
  4447. while (carry > 0 && (ind_m--)) {
  4448. m->frac[ind_m] += carry;
  4449. if (m->frac[ind_m] >= BASE) m->frac[ind_m] -= BASE;
  4450. else carry = 0;
  4451. }
  4452. if(carry > 0) { /* Overflow,count exponent and set fraction part be 1 */
  4453. if (!AddExponent(m, 1)) return 0;
  4454. m->Prec = m->frac[0] = 1;
  4455. } else {
  4456. VpNmlz(m);
  4457. }
  4458. return 1;
  4459. }
  4460. /*
  4461. * y = x - fix(x)
  4462. */
  4463. VP_EXPORT void
  4464. VpFrac(Real *y, Real *x)
  4465. {
  4466. size_t my, ind_y, ind_x;
  4467. if(!VpHasVal(x)) {
  4468. VpAsgn(y,x,1);
  4469. goto Exit;
  4470. }
  4471. if (x->exponent > 0 && (size_t)x->exponent >= x->Prec) {
  4472. VpSetZero(y,VpGetSign(x));
  4473. goto Exit;
  4474. }
  4475. else if(x->exponent <= 0) {
  4476. VpAsgn(y, x, 1);
  4477. goto Exit;
  4478. }
  4479. /* satisfy: x->exponent > 0 */
  4480. y->Prec = x->Prec - (size_t)x->exponent;
  4481. y->Prec = Min(y->Prec, y->MaxPrec);
  4482. y->exponent = 0;
  4483. VpSetSign(y,VpGetSign(x));
  4484. ind_y = 0;
  4485. my = y->Prec;
  4486. ind_x = x->exponent;
  4487. while(ind_y < my) {
  4488. y->frac[ind_y] = x->frac[ind_x];
  4489. ++ind_y;
  4490. ++ind_x;
  4491. }
  4492. VpNmlz(y);
  4493. Exit:
  4494. #ifdef BIGDECIMAL_DEBUG
  4495. if(gfDebug) {
  4496. VPrint(stdout, "VpFrac y=%\n", y);
  4497. VPrint(stdout, " x=%\n", x);
  4498. }
  4499. #endif /* BIGDECIMAL_DEBUG */
  4500. return;
  4501. }
  4502. /*
  4503. * y = x ** n
  4504. */
  4505. VP_EXPORT int
  4506. VpPower(Real *y, Real *x, SIGNED_VALUE n)
  4507. {
  4508. size_t s, ss;
  4509. ssize_t sign;
  4510. Real *w1 = NULL;
  4511. Real *w2 = NULL;
  4512. if(VpIsZero(x)) {
  4513. if(n==0) {
  4514. VpSetOne(y);
  4515. goto Exit;
  4516. }
  4517. sign = VpGetSign(x);
  4518. if(n<0) {
  4519. n = -n;
  4520. if(sign<0) sign = (n%2)?(-1):(1);
  4521. VpSetInf (y,sign);
  4522. } else {
  4523. if(sign<0) sign = (n%2)?(-1):(1);
  4524. VpSetZero(y,sign);
  4525. }
  4526. goto Exit;
  4527. }
  4528. if(VpIsNaN(x)) {
  4529. VpSetNaN(y);
  4530. goto Exit;
  4531. }
  4532. if(VpIsInf(x)) {
  4533. if(n==0) {
  4534. VpSetOne(y);
  4535. goto Exit;
  4536. }
  4537. if(n>0) {
  4538. VpSetInf(y, (n%2==0 || VpIsPosInf(x)) ? 1 : -1);
  4539. goto Exit;
  4540. }
  4541. VpSetZero(y, (n%2==0 || VpIsPosInf(x)) ? 1 : -1);
  4542. goto Exit;
  4543. }
  4544. if((x->exponent == 1) &&(x->Prec == 1) &&(x->frac[0] == 1)) {
  4545. /* abs(x) = 1 */
  4546. VpSetOne(y);
  4547. if(VpGetSign(x) > 0) goto Exit;
  4548. if((n % 2) == 0) goto Exit;
  4549. VpSetSign(y, -1);
  4550. goto Exit;
  4551. }
  4552. if(n > 0) sign = 1;
  4553. else if(n < 0) {
  4554. sign = -1;
  4555. n = -n;
  4556. } else {
  4557. VpSetOne(y);
  4558. goto Exit;
  4559. }
  4560. /* Allocate working variables */
  4561. w1 = VpAlloc((y->MaxPrec + 2) * BASE_FIG, "#0");
  4562. w2 = VpAlloc((w1->MaxPrec * 2 + 1) * BASE_FIG, "#0");
  4563. /* calculation start */
  4564. VpAsgn(y, x, 1);
  4565. --n;
  4566. while(n > 0) {
  4567. VpAsgn(w1, x, 1);
  4568. s = 1;
  4569. while (ss = s, (s += s) <= (size_t)n) {
  4570. VpMult(w2, w1, w1);
  4571. VpAsgn(w1, w2, 1);
  4572. }
  4573. n -= (SIGNED_VALUE)ss;
  4574. VpMult(w2, y, w1);
  4575. VpAsgn(y, w2, 1);
  4576. }
  4577. if(sign < 0) {
  4578. VpDivd(w1, w2, VpConstOne, y);
  4579. VpAsgn(y, w1, 1);
  4580. }
  4581. Exit:
  4582. #ifdef BIGDECIMAL_DEBUG
  4583. if(gfDebug) {
  4584. VPrint(stdout, "VpPower y=%\n", y);
  4585. VPrint(stdout, "VpPower x=%\n", x);
  4586. printf(" n=%d\n", n);
  4587. }
  4588. #endif /* BIGDECIMAL_DEBUG */
  4589. VpFree(w2);
  4590. VpFree(w1);
  4591. return 1;
  4592. }
  4593. #ifdef BIGDECIMAL_DEBUG
  4594. int
  4595. VpVarCheck(Real * v)
  4596. /*
  4597. * Checks the validity of the Real variable v.
  4598. * [Input]
  4599. * v ... Real *, variable to be checked.
  4600. * [Returns]
  4601. * 0 ... correct v.
  4602. * other ... error
  4603. */
  4604. {
  4605. size_t i;
  4606. if(v->MaxPrec <= 0) {
  4607. printf("ERROR(VpVarCheck): Illegal Max. Precision(=%"PRIuSIZE")\n",
  4608. v->MaxPrec);
  4609. return 1;
  4610. }
  4611. if((v->Prec <= 0) ||((v->Prec) >(v->MaxPrec))) {
  4612. printf("ERROR(VpVarCheck): Illegal Precision(=%"PRIuSIZE")\n", v->Prec);
  4613. printf(" Max. Prec.=%"PRIuSIZE"\n", v->MaxPrec);
  4614. return 2;
  4615. }
  4616. for(i = 0; i < v->Prec; ++i) {
  4617. if((v->frac[i] >= BASE)) {
  4618. printf("ERROR(VpVarCheck): Illegal fraction\n");
  4619. printf(" Frac[%"PRIuSIZE"]=%lu\n", i, v->frac[i]);
  4620. printf(" Prec. =%"PRIuSIZE"\n", v->Prec);
  4621. printf(" Exp. =%"PRIdVALUE"\n", v->exponent);
  4622. printf(" BASE =%lu\n", BASE);
  4623. return 3;
  4624. }
  4625. }
  4626. return 0;
  4627. }
  4628. #endif /* BIGDECIMAL_DEBUG */