~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/decimal.cc

  • Committer: Monty Taylor
  • Date: 2009-10-13 06:22:10 UTC
  • mfrom: (1182 staging)
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20091013062210-iwnwwcdamjdvlx1m
Merged up with build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1425
1425
}
1426
1426
 
1427
1427
/*
1428
 
  Returns the size of array to hold a decimal with given precision and scale
1429
 
 
1430
 
  RETURN VALUE
1431
 
    size in dec1
1432
 
    (multiply by sizeof(dec1) to get the size if bytes)
1433
 
*/
1434
 
 
1435
 
int decimal_size(int precision, int scale)
1436
 
{
1437
 
  assert(scale >= 0 && precision > 0 && scale <= precision);
1438
 
  return ROUND_UP(precision-scale)+ROUND_UP(scale);
1439
 
}
1440
 
 
1441
 
/*
1442
1428
  Returns the size of array to hold a binary representation of a decimal
1443
1429
 
1444
1430
  RETURN VALUE
1667
1653
  return error;
1668
1654
}
1669
1655
 
1670
 
/*
1671
 
  Returns the size of the result of the operation
1672
 
 
1673
 
  SYNOPSIS
1674
 
    decimal_result_size()
1675
 
      from1   - operand of the unary operation or first operand of the
1676
 
                binary operation
1677
 
      from2   - second operand of the binary operation
1678
 
      op      - operation. one char '+', '-', '*', '/' are allowed
1679
 
                others may be added later
1680
 
      param   - extra param to the operation. unused for '+', '-', '*'
1681
 
                scale increment for '/'
1682
 
 
1683
 
  NOTE
1684
 
    returned valued may be larger than the actual buffer requred
1685
 
    in the operation, as decimal_result_size, by design, operates on
1686
 
    precision/scale values only and not on the actual decimal number
1687
 
 
1688
 
  RETURN VALUE
1689
 
    size of to->buf array in dec1 elements. to get size in bytes
1690
 
    multiply by sizeof(dec1)
1691
 
*/
1692
 
 
1693
 
int decimal_result_size(decimal_t *from1, decimal_t *from2, char op, int param)
1694
 
{
1695
 
  switch (op) {
1696
 
  case '-':
1697
 
    return ROUND_UP(max(from1->intg, from2->intg)) +
1698
 
           ROUND_UP(max(from1->frac, from2->frac));
1699
 
  case '+':
1700
 
    return ROUND_UP(max(from1->intg, from2->intg)+1) +
1701
 
           ROUND_UP(max(from1->frac, from2->frac));
1702
 
  case '*':
1703
 
    return ROUND_UP(from1->intg+from2->intg)+
1704
 
           ROUND_UP(from1->frac)+ROUND_UP(from2->frac);
1705
 
  case '/':
1706
 
    return ROUND_UP(from1->intg+from2->intg+1+from1->frac+from2->frac+param);
1707
 
  default: assert(0);
1708
 
  }
1709
 
  return -1; /* shut up the warning */
1710
 
}
1711
 
 
1712
1656
static int do_add(decimal_t *from1, decimal_t *from2, decimal_t *to)
1713
1657
{
1714
1658
  int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),