~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mystrings/decimal.cc

Merge Trond

Show diffs side-by-side

added added

removed removed

Lines of Context:
270
270
}
271
271
 
272
272
 
273
 
static dec1 *remove_leading_zeroes(decimal_t *from, int *intg_result)
 
273
static dec1 *remove_leading_zeroes(const decimal_t *from, int *intg_result)
274
274
{
275
275
  int intg= from->intg, i;
276
276
  dec1 *buf0= from->buf;
346
346
    E_DEC_OK/E_DEC_TRUNCATED/E_DEC_OVERFLOW
347
347
*/
348
348
 
349
 
int decimal2string(decimal_t *from, char *to, int *to_len,
 
349
int decimal2string(const decimal_t *from, char *to, int *to_len,
350
350
                   int fixed_precision, int fixed_decimals,
351
351
                   char filler)
352
352
{
964
964
    E_DEC_OK/E_DEC_OVERFLOW/E_DEC_TRUNCATED
965
965
*/
966
966
 
967
 
int decimal2double(decimal_t *from, double *to)
 
967
int decimal2double(const decimal_t *from, double *to)
968
968
{
969
969
  char strbuf[FLOATING_POINT_BUFFER], *end;
970
970
  int len= sizeof(strbuf);
990
990
    E_DEC_OK/E_DEC_OVERFLOW/E_DEC_TRUNCATED
991
991
*/
992
992
 
993
 
int double2decimal(double from, decimal_t *to)
 
993
int double2decimal(const double from, decimal_t *to)
994
994
{
995
995
  char buff[FLOATING_POINT_BUFFER], *end;
996
996
  int res;
1026
1026
  return error;
1027
1027
}
1028
1028
 
1029
 
int uint64_t2decimal(uint64_t from, decimal_t *to)
 
1029
int uint64_t2decimal(const uint64_t from, decimal_t *to)
1030
1030
{
1031
1031
  to->sign=0;
1032
1032
  return ull2dec(from, to);
1033
1033
}
1034
1034
 
1035
 
int int64_t2decimal(int64_t from, decimal_t *to)
 
1035
int int64_t2decimal(const int64_t from, decimal_t *to)
1036
1036
{
1037
1037
  if ((to->sign= from < 0))
1038
1038
    return ull2dec(-from, to);
1039
1039
  return ull2dec(from, to);
1040
1040
}
1041
1041
 
1042
 
int decimal2uint64_t(decimal_t *from, uint64_t *to)
 
1042
int decimal2uint64_t(const decimal_t *from, uint64_t *to)
1043
1043
{
1044
1044
  dec1 *buf=from->buf;
1045
1045
  uint64_t x=0;
1068
1068
  return E_DEC_OK;
1069
1069
}
1070
1070
 
1071
 
int decimal2int64_t(decimal_t *from, int64_t *to)
 
1071
int decimal2int64_t(const decimal_t *from, int64_t *to)
1072
1072
{
1073
1073
  dec1 *buf=from->buf;
1074
1074
  int64_t x=0;
1187
1187
 
1188
1188
                7E F2 04 37 2D FB 2D
1189
1189
*/
1190
 
int decimal2bin(decimal_t *from, unsigned char *to, int precision, int frac)
 
1190
int decimal2bin(const decimal_t *from, unsigned char *to, int precision, int frac)
1191
1191
{
1192
1192
  dec1 mask=from->sign ? -1 : 0, *buf1=from->buf, *stop1;
1193
1193
  int error=E_DEC_OK, intg=precision-frac,
1461
1461
*/
1462
1462
 
1463
1463
int
1464
 
decimal_round(decimal_t *from, decimal_t *to, int scale,
 
1464
decimal_round(const decimal_t *from, decimal_t *to, int scale,
1465
1465
              decimal_round_mode mode)
1466
1466
{
1467
1467
  int frac0=scale>0 ? ROUND_UP(scale) : scale/DIG_PER_DEC1,
1653
1653
  return error;
1654
1654
}
1655
1655
 
1656
 
static int do_add(decimal_t *from1, decimal_t *from2, decimal_t *to)
 
1656
static int do_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1657
1657
{
1658
1658
  int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
1659
1659
      frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
1735
1735
 
1736
1736
/* to=from1-from2.
1737
1737
   if to==0, return -1/0/+1 - the result of the comparison */
1738
 
static int do_sub(decimal_t *from1, decimal_t *from2, decimal_t *to)
 
1738
static int do_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1739
1739
{
1740
1740
  int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
1741
1741
      frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac);
1804
1804
  /* ensure that always from1 > from2 (and intg1 >= intg2) */
1805
1805
  if (carry)
1806
1806
  {
1807
 
    swap_variables(decimal_t *,from1,from1);
 
1807
    swap_variables(const decimal_t *,from1, from2);
1808
1808
    swap_variables(dec1 *,start1, start2);
1809
1809
    swap_variables(int,intg1,intg2);
1810
1810
    swap_variables(int,frac1,frac2);
1870
1870
  return error;
1871
1871
}
1872
1872
 
1873
 
int decimal_intg(decimal_t *from)
 
1873
int decimal_intg(const decimal_t *from)
1874
1874
{
1875
1875
  int res;
1876
1876
  dec1 *tmp_res;
1878
1878
  return res;
1879
1879
}
1880
1880
 
1881
 
int decimal_add(decimal_t *from1, decimal_t *from2, decimal_t *to)
 
1881
int decimal_add(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1882
1882
{
1883
1883
  if (likely(from1->sign == from2->sign))
1884
1884
    return do_add(from1, from2, to);
1885
1885
  return do_sub(from1, from2, to);
1886
1886
}
1887
1887
 
1888
 
int decimal_sub(decimal_t *from1, decimal_t *from2, decimal_t *to)
 
1888
int decimal_sub(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1889
1889
{
1890
1890
  if (likely(from1->sign == from2->sign))
1891
1891
    return do_sub(from1, from2, to);
1892
1892
  return do_add(from1, from2, to);
1893
1893
}
1894
1894
 
1895
 
int decimal_cmp(decimal_t *from1, decimal_t *from2)
 
1895
int decimal_cmp(const decimal_t *from1, const decimal_t *from2)
1896
1896
{
1897
1897
  if (likely(from1->sign == from2->sign))
1898
1898
    return do_sub(from1, from2, 0);
1899
1899
  return from1->sign > from2->sign ? -1 : 1;
1900
1900
}
1901
1901
 
1902
 
int decimal_is_zero(decimal_t *from)
 
1902
int decimal_is_zero(const decimal_t *from)
1903
1903
{
1904
1904
  dec1 *buf1=from->buf,
1905
1905
       *end=buf1+ROUND_UP(from->intg)+ROUND_UP(from->frac);
1930
1930
    XXX if this library is to be used with huge numbers of thousands of
1931
1931
    digits, fast multiplication must be implemented.
1932
1932
*/
1933
 
int decimal_mul(decimal_t *from1, decimal_t *from2, decimal_t *to)
 
1933
int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
1934
1934
{
1935
1935
  int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg),
1936
1936
      frac1=ROUND_UP(from1->frac), frac2=ROUND_UP(from2->frac),
2046
2046
  changed to malloc (or at least fallback to malloc if alloca() fails)
2047
2047
  but then, decimal_mul() should be rewritten too :(
2048
2048
*/
2049
 
static int do_div_mod(decimal_t *from1, decimal_t *from2,
 
2049
static int do_div_mod(const decimal_t *from1, const decimal_t *from2,
2050
2050
                       decimal_t *to, decimal_t *mod, int scale_incr)
2051
2051
{
2052
2052
  int frac1=ROUND_UP(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1,
2305
2305
*/
2306
2306
 
2307
2307
int
2308
 
decimal_div(decimal_t *from1, decimal_t *from2, decimal_t *to, int scale_incr)
 
2308
decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to, int scale_incr)
2309
2309
{
2310
2310
  return do_div_mod(from1, from2, to, 0, scale_incr);
2311
2311
}
2337
2337
   thus, there's no requirement for M or N to be integers
2338
2338
*/
2339
2339
 
2340
 
int decimal_mod(decimal_t *from1, decimal_t *from2, decimal_t *to)
 
2340
int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
2341
2341
{
2342
2342
  return do_div_mod(from1, from2, 0, to, 0);
2343
2343
}