~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to strings/decimal.c

  • Committer: Brian Aker
  • Date: 2008-07-13 22:45:08 UTC
  • Revision ID: brian@tangent.org-20080713224508-hb20z4okblotb39a
longlong replacement

Show diffs side-by-side

added added

removed removed

Lines of Context:
122
122
        not in bytes
123
123
*/
124
124
typedef decimal_digit_t dec1;
125
 
typedef longlong      dec2;
 
125
typedef int64_t      dec2;
126
126
 
127
127
#define DIG_PER_DEC1 9
128
128
#define DIG_MASK     100000000
897
897
  if (endp+1 < end_of_string && (*endp == 'e' || *endp == 'E'))
898
898
  {
899
899
    int str_error;
900
 
    longlong exponent= my_strtoll10(endp+1, (char**) &end_of_string,
 
900
    int64_t exponent= my_strtoll10(endp+1, (char**) &end_of_string,
901
901
                                    &str_error);
902
902
 
903
903
    if (end_of_string != endp +1)               /* If at least one digit */
1016
1016
  return ull2dec(from, to);
1017
1017
}
1018
1018
 
1019
 
int longlong2decimal(longlong from, decimal_t *to)
 
1019
int int64_t2decimal(int64_t from, decimal_t *to)
1020
1020
{
1021
1021
  if ((to->sign= from < 0))
1022
1022
    return ull2dec(-from, to);
1052
1052
  return E_DEC_OK;
1053
1053
}
1054
1054
 
1055
 
int decimal2longlong(decimal_t *from, longlong *to)
 
1055
int decimal2int64_t(decimal_t *from, int64_t *to)
1056
1056
{
1057
1057
  dec1 *buf=from->buf;
1058
 
  longlong x=0;
 
1058
  int64_t x=0;
1059
1059
  int intg, frac;
1060
1060
 
1061
1061
  for (intg=from->intg; intg > 0; intg-=DIG_PER_DEC1)
1062
1062
  {
1063
 
    longlong y=x;
 
1063
    int64_t y=x;
1064
1064
    /*
1065
1065
      Attention: trick!
1066
1066
      we're calculating -|from| instead of |from| here
2528
2528
  int res;
2529
2529
 
2530
2530
  res=uint64_t2decimal(from, &a);
2531
 
  longlong10_to_str(from,s,10);
 
2531
  int64_t10_to_str(from,s,10);
2532
2532
  printf("%-40s => res=%d    ", s, res);
2533
2533
  print_decimal(&a, orig, res, ex);
2534
2534
  printf("\n");
2535
2535
}
2536
2536
 
2537
 
void test_ll2d(longlong from, const char *orig, int ex)
 
2537
void test_ll2d(int64_t from, const char *orig, int ex)
2538
2538
{
2539
2539
  char s[100];
2540
2540
  int res;
2541
2541
 
2542
 
  res=longlong2decimal(from, &a);
2543
 
  longlong10_to_str(from,s,-10);
 
2542
  res=int64_t2decimal(from, &a);
 
2543
  int64_t10_to_str(from,s,-10);
2544
2544
  printf("%-40s => res=%d    ", s, res);
2545
2545
  print_decimal(&a, orig, res, ex);
2546
2546
  printf("\n");
2556
2556
  string2decimal(s, &a, &end);
2557
2557
  res=decimal2uint64_t(&a, &x);
2558
2558
  if (full) dump_decimal(&a);
2559
 
  longlong10_to_str(x,s1,10);
 
2559
  int64_t10_to_str(x,s1,10);
2560
2560
  printf("%-40s => res=%d    %s\n", s, res, s1);
2561
2561
  check_result_code(res, ex);
2562
2562
  if (orig && strcmp(orig, s1))
2569
2569
void test_d2ll(const char *s, const char *orig, int ex)
2570
2570
{
2571
2571
  char s1[100], *end;
2572
 
  longlong x;
 
2572
  int64_t x;
2573
2573
  int res;
2574
2574
 
2575
2575
  end= strend(s);
2576
2576
  string2decimal(s, &a, &end);
2577
 
  res=decimal2longlong(&a, &x);
 
2577
  res=decimal2int64_t(&a, &x);
2578
2578
  if (full) dump_decimal(&a);
2579
 
  longlong10_to_str(x,s1,-10);
 
2579
  int64_t10_to_str(x,s1,-10);
2580
2580
  printf("%-40s => res=%d    %s\n", s, res, s1);
2581
2581
  check_result_code(res, ex);
2582
2582
  if (orig && strcmp(orig, s1))
2823
2823
  test_d2ull("1.23", "1", 1);
2824
2824
  test_d2ull("9999999999999999999999999.000", "9999999999999999", 2);
2825
2825
 
2826
 
  printf("==== longlong2decimal ====\n");
 
2826
  printf("==== int64_t2decimal ====\n");
2827
2827
  test_ll2d(12345LL, "-12345", 0);
2828
2828
  test_ll2d(1LL, "-1", 0);
2829
2829
  test_ll2d(9223372036854775807LL, "-9223372036854775807", 0);
2830
2830
  test_ll2d(9223372036854775808ULL, "-9223372036854775808", 0);
2831
2831
 
2832
 
  printf("==== decimal2longlong ====\n");
 
2832
  printf("==== decimal2int64_t ====\n");
2833
2833
  test_d2ll("18446744073709551615", "18446744073", 2);
2834
2834
  test_d2ll("-1", "-1", 0);
2835
2835
  test_d2ll("-1.23", "-1", 1);