~ubuntu-branches/ubuntu/natty/bc/natty

« back to all changes in this revision

Viewing changes to lib/number.c

  • Committer: Bazaar Package Importer
  • Author(s): John G. Hasler
  • Date: 2009-10-06 13:57:38 UTC
  • mto: (3.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20091006135738-81b5mvwsfxfa96bv
Tags: upstream-1.06.95
ImportĀ upstreamĀ versionĀ 1.06.95

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
#define bc_rt_error rt_error
48
48
#define bc_out_of_memory out_of_memory
49
49
 
50
 
_PROTOTYPE(void rt_warn, (char *mesg ,...));
51
 
_PROTOTYPE(void rt_error, (char *mesg ,...));
 
50
_PROTOTYPE(void rt_warn, (const char *mesg ,...));
 
51
_PROTOTYPE(void rt_error, (const char *mesg ,...));
52
52
_PROTOTYPE(void out_of_memory, (void));
53
53
 
54
54
/* Storage used for special numbers. */
661
661
}
662
662
 
663
663
static void
664
 
_bc_simp_mul (bc_num n1, int n1len, bc_num n2, int n2len, bc_num *prod,
665
 
              int full_scale)
 
664
_bc_simp_mul (bc_num n1, int n1len, bc_num n2, int n2len, bc_num *prod)
666
665
{
667
666
  char *n1ptr, *n2ptr, *pvptr;
668
667
  char *n1end, *n2end;          /* To the end of n1 and n2. */
762
761
   B is the base of storage, number of digits in u1,u0 close to equal.
763
762
*/
764
763
static void
765
 
_bc_rec_mul (bc_num u, int ulen, bc_num v, int vlen, bc_num *prod,
766
 
             int full_scale)
 
764
_bc_rec_mul (bc_num u, int ulen, bc_num v, int vlen, bc_num *prod)
767
765
768
766
  bc_num u0, u1, v0, v1;
769
767
  int u0len, v0len;
775
773
  if ((ulen+vlen) < mul_base_digits
776
774
      || ulen < MUL_SMALL_DIGITS
777
775
      || vlen < MUL_SMALL_DIGITS ) {
778
 
    _bc_simp_mul (u, ulen, v, vlen, prod, full_scale);
 
776
    _bc_simp_mul (u, ulen, v, vlen, prod);
779
777
    return;
780
778
  }
781
779
 
820
818
  if (m1zero)
821
819
    m1 = bc_copy_num (_zero_);
822
820
  else
823
 
    _bc_rec_mul (u1, u1->n_len, v1, v1->n_len, &m1, 0);
 
821
    _bc_rec_mul (u1, u1->n_len, v1, v1->n_len, &m1);
824
822
 
825
823
  if (bc_is_zero(d1) || bc_is_zero(d2))
826
824
    m2 = bc_copy_num (_zero_);
827
825
  else
828
 
    _bc_rec_mul (d1, d1len, d2, d2len, &m2, 0);
 
826
    _bc_rec_mul (d1, d1len, d2, d2len, &m2);
829
827
 
830
828
  if (bc_is_zero(u0) || bc_is_zero(v0))
831
829
    m3 = bc_copy_num (_zero_);
832
830
  else
833
 
    _bc_rec_mul (u0, u0->n_len, v0, v0->n_len, &m3, 0);
 
831
    _bc_rec_mul (u0, u0->n_len, v0, v0->n_len, &m3);
834
832
 
835
833
  /* Initialize product */
836
834
  prodlen = ulen+vlen+1;
876
874
  prod_scale = MIN(full_scale,MAX(scale,MAX(n1->n_scale,n2->n_scale)));
877
875
 
878
876
  /* Do the multiply */
879
 
  _bc_rec_mul (n1, len1, n2, len2, &pval, full_scale);
 
877
  _bc_rec_mul (n1, len1, n2, len2, &pval);
880
878
 
881
879
  /* Assign to prod and clean up the number. */
882
880
  pval->n_sign = ( n1->n_sign == n2->n_sign ? PLUS : MINUS );
1471
1469
     int leading_zero;
1472
1470
{
1473
1471
  char *nptr;
1474
 
  int  index, fdigit, pre_space;
 
1472
  int  ix, fdigit, pre_space;
1475
1473
  stk_rec *digits, *temp;
1476
1474
  bc_num int_part, frac_part, base, cur_dig, t_num, max_o_digit;
1477
1475
 
1487
1485
        /* The number is in base 10, do it the fast way. */
1488
1486
        nptr = num->n_value;
1489
1487
        if (num->n_len > 1 || *nptr != 0)
1490
 
          for (index=num->n_len; index>0; index--)
 
1488
          for (ix=num->n_len; ix>0; ix--)
1491
1489
            (*out_char) (BCD_CHAR(*nptr++));
1492
1490
        else
1493
1491
          nptr++;
1499
1497
        if (num->n_scale > 0)
1500
1498
          {
1501
1499
            (*out_char) ('.');
1502
 
            for (index=0; index<num->n_scale; index++)
 
1500
            for (ix=0; ix<num->n_scale; ix++)
1503
1501
              (*out_char) (BCD_CHAR(*nptr++));
1504
1502
          }
1505
1503
      }
1594
1592
{
1595
1593
  long val;
1596
1594
  char *nptr;
1597
 
  int  index;
 
1595
  int  i;
1598
1596
 
1599
1597
  /* Extract the int value, ignore the fraction. */
1600
1598
  val = 0;
1601
1599
  nptr = num->n_value;
1602
 
  for (index=num->n_len; (index>0) && (val<=(LONG_MAX/BASE)); index--)
 
1600
  for (i=num->n_len; (i>0) && (val<=(LONG_MAX/BASE)); i--)
1603
1601
    val = val*BASE + *nptr++;
1604
1602
 
1605
1603
  /* Check for overflow.  If overflow, return zero. */
1606
 
  if (index>0) val = 0;
 
1604
  if (i>0) val = 0;
1607
1605
  if (val < 0) val = 0;
1608
1606
 
1609
1607
  /* Return the value. */
1665
1663
{
1666
1664
  char *str, *sptr;
1667
1665
  char *nptr;
1668
 
  int  index, signch;
 
1666
  int  i, signch;
1669
1667
 
1670
1668
  /* Allocate the string memory. */
1671
1669
  signch = ( num->n_sign == PLUS ? 0 : 1 );  /* Number of sign chars. */
1681
1679
 
1682
1680
  /* Load the whole number. */
1683
1681
  nptr = num->n_value;
1684
 
  for (index=num->n_len; index>0; index--)
 
1682
  for (i=num->n_len; i>0; i--)
1685
1683
    *sptr++ = BCD_CHAR(*nptr++);
1686
1684
 
1687
1685
  /* Now the fraction. */
1688
1686
  if (num->n_scale > 0)
1689
1687
    {
1690
1688
      *sptr++ = '.';
1691
 
      for (index=0; index<num->n_scale; index++)
 
1689
      for (i=0; i<num->n_scale; i++)
1692
1690
        *sptr++ = BCD_CHAR(*nptr++);
1693
1691
    }
1694
1692