109#include <drizzled/internal/m_string.h>
110#include <drizzled/charset.h>
111#include <drizzled/type/decimal.h>
113#include <plugin/myisam/myisampack.h>
114#include <drizzled/util/test.h>
120#include <drizzled/current_session.h>
121#include <drizzled/error.h>
122#include <drizzled/field.h>
123#include <drizzled/internal/my_sys.h>
146 case E_DEC_TRUNCATED:
147 push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
148 ER_WARN_DATA_TRUNCATED, ER(ER_WARN_DATA_TRUNCATED),
152 push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
153 ER_TRUNCATED_WRONG_VALUE,
154 ER(ER_TRUNCATED_WRONG_VALUE),
158 my_error(ER_DIVISION_BY_ZERO, MYF(0));
161 push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
162 ER_TRUNCATED_WRONG_VALUE_FOR_FIELD,
163 ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
164 "decimal",
"",
"", (
long)-1);
167 my_error(ER_OUT_OF_RESOURCES, MYF(0));
196 uint32_t fixed_dec,
String *str)
198 uint32_t mask= E_DEC_FATAL_ERROR;
212 ? (uint32_t)(((0 == fixed_dec) ? 1 : 0) + 1)
213 : (uint32_t)d->string_length());
218 &length, (
int)0, fixed_dec,
221 return check_result(mask, result);
246int Decimal::val_binary(uint32_t mask,
unsigned char *bin,
int prec,
int scale)
const
248 int err1= E_DEC_OK, err2;
249 type::Decimal rounded;
250 class_decimal2decimal(
this, &rounded);
252 if (scale < rounded.frac)
254 err1= E_DEC_TRUNCATED;
261 return check_result(mask, err2);
285 char *end, *from_end;
287 char buff[STRING_BUFFER_USUAL_SIZE];
288 String tmp(buff,
sizeof(buff), &my_charset_bin);
289 if (charset->mbminlen > 1)
291 tmp.copy(from, length, &my_charset_utf8_general_ci);
293 length= tmp.length();
294 charset= &my_charset_bin;
296 from_end= end= (
char*) from+length;
297 err= string2decimal((
char *)from, (
decimal_t*)
this, &end);
298 if (end != from_end && !err)
301 for ( ; end < from_end; end++)
303 if (not my_charset_utf8_general_ci.isspace(*end))
305 err= E_DEC_TRUNCATED;
310 check_result_and_overflow(mask, err);
314void type::Decimal::convert(
double &result)
const
322 date = (ltime->year*100L + ltime->month)*100L + ltime->day;
323 if (ltime->time_type > type::DRIZZLE_TIMESTAMP_DATE)
324 date= ((date*100L + ltime->hour)*100L+ ltime->minute)*100L + ltime->second;
326 if (int2_class_decimal(E_DEC_FATAL_ERROR, date,
false, dec))
329 if (ltime->second_part)
331 dec->buf[(dec->intg-1) / 9 + 1]= ltime->second_part * 1000;
339void class_decimal_trim(uint32_t *precision, uint32_t *scale)
341 if (!(*precision) && !(*scale))
365typedef decimal_digit_t dec1;
368#define DIG_PER_DEC1 9
369#define DIG_MASK 100000000
370#define DIG_BASE 1000000000
371#define DIG_MAX (DIG_BASE-1)
374inline static T round_up(
const T &x)
376 return (x+DIG_PER_DEC1-1)/DIG_PER_DEC1;
379static const dec1 powers10[DIG_PER_DEC1+1]={
380 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
381static const int dig2bytes[DIG_PER_DEC1+1]={0, 1, 1, 2, 2, 3, 3, 4, 4, 4};
382static const dec1 frac_max[DIG_PER_DEC1-1]={
383 900000000, 990000000, 999000000,
384 999900000, 999990000, 999999000,
385 999999900, 999999990 };
388#define sanity(d) assert((d)->len > 0)
390#define sanity(d) assert((d)->len >0 && ((d)->buf[0] | \
391 (d)->buf[(d)->len-1] | 1))
394inline static void fix_intg_frac_error(
const int len,
int &intg1,
int &frac1,
int &error)
396 if (DRIZZLE_UNLIKELY(intg1+frac1 > len))
398 if (DRIZZLE_UNLIKELY(intg1 > len))
402 error=E_DEC_OVERFLOW;
407 error=E_DEC_TRUNCATED;
415inline static void add(dec1 &to,
const dec1 &from1,
const dec1& from2, dec1 &carry)
417 dec1 a=from1+from2+carry;
419 if ((carry= (a >= DIG_BASE)))
424inline static void add2(dec1 &to,
const dec1 &from1,
const dec1 &from2, dec1 &carry)
426 dec2 a=dec2(from1)+from2+carry;
427 if ((carry= (a >= DIG_BASE)))
429 if (DRIZZLE_UNLIKELY(a >= DIG_BASE))
438inline static void sub(dec1 &to,
const dec1 &from1,
const dec1 &from2, dec1 &carry)
440 dec1 a=from1-from2-carry;
441 if ((carry= (a < 0)))
447inline static void sub2(dec1 &to,
const dec1 &from1,
const dec1 &from2, dec1 &carry)
449 dec1 a=from1-from2-carry;
450 if ((carry= (a < 0)))
452 if (DRIZZLE_UNLIKELY(a < 0))
472 assert(precision && precision >= frac);
475 if ((intpart= to->intg= (precision - frac)))
477 const int firstdigits= intpart % DIG_PER_DEC1;
479 *buf++= powers10[firstdigits] - 1;
480 for(intpart/= DIG_PER_DEC1; intpart; intpart--)
484 if ((to->frac= frac))
486 const int lastdigits= frac % DIG_PER_DEC1;
487 for(frac/= DIG_PER_DEC1; frac; frac--)
490 *buf= frac_max[lastdigits - 1];
495static dec1 *remove_leading_zeroes(
const decimal_t *from,
int *intg_result)
497 int intg= from->intg, i;
498 dec1 *buf0= from->buf;
499 i= ((intg - 1) % DIG_PER_DEC1) + 1;
500 while (intg > 0 && *buf0 == 0)
508 for (i= (intg - 1) % DIG_PER_DEC1; *buf0 < powers10[i--]; intg--) ;
526 int frac= from->frac, i;
527 dec1 *buf0= from->buf + round_up(from->intg) + round_up(frac) - 1;
532 i= ((frac - 1) % DIG_PER_DEC1 + 1);
533 while (frac > 0 && *buf0 == 0)
541 for (i= DIG_PER_DEC1 - ((frac - 1) % DIG_PER_DEC1); *buf0 % powers10[i++] == 0; frac--) {};
569 int fixed_precision,
int fixed_decimals,
572 int len, intg, frac= from->frac, i, intg_len, frac_len, fill;
574 int fixed_intg= (fixed_precision ?
575 (fixed_precision - fixed_decimals) : 0);
578 dec1 *buf, *buf0=from->buf, tmp;
580 assert(*to_len >= 2+from->sign);
583 buf0= remove_leading_zeroes(from, &intg);
584 if (DRIZZLE_UNLIKELY(intg+frac==0))
591 if (!(intg_len= fixed_precision ? fixed_intg : intg))
593 frac_len= fixed_precision ? fixed_decimals : frac;
594 len= from->sign + intg_len + test(frac) + frac_len;
597 if (frac > fixed_decimals)
599 error= E_DEC_TRUNCATED;
600 frac= fixed_decimals;
602 if (intg > fixed_intg)
604 error= E_DEC_OVERFLOW;
608 else if (DRIZZLE_UNLIKELY(len > --*to_len))
611 error= (frac && j <= frac + 1) ? E_DEC_TRUNCATED : E_DEC_OVERFLOW;
612 if (frac && j >= frac + 1) j--;
620 len= from->sign + intg_len + test(frac) + frac_len;
630 char *s1= s + intg_len;
631 fill= frac_len - frac;
632 buf=buf0+round_up(intg);
634 for (; frac>0; frac-=DIG_PER_DEC1)
637 for (i=min(frac, DIG_PER_DEC1); i; i--)
640 *s1++=
'0'+(
unsigned char)y;
649 fill= intg_len - intg;
657 for (buf=buf0+round_up(intg); intg>0; intg-=DIG_PER_DEC1)
660 for (i=min(intg, DIG_PER_DEC1); i; i--)
663 *--s=
'0'+(
unsigned char)(x-y*10);
686 dec1 *buf_beg= from->buf;
687 dec1 *end= from->buf + round_up(from->intg) + round_up(from->frac);
688 dec1 *buf_end= end - 1;
691 while (buf_beg < end && *buf_beg == 0)
697 *start_result= *end_result= 0;
702 if (buf_beg == from->buf && from->intg)
704 start= DIG_PER_DEC1 - (i= ((from->intg-1) % DIG_PER_DEC1 + 1));
710 start= (int) ((buf_beg - from->buf) * DIG_PER_DEC1);
713 for (; *buf_beg < powers10[i--]; start++) ;
714 *start_result= start;
717 while (buf_end > buf_beg && *buf_end == 0)
720 if (buf_end == end - 1 && from->frac)
722 stop= (int) (((buf_end - from->buf) * DIG_PER_DEC1 +
723 (i= ((from->frac - 1) % DIG_PER_DEC1 + 1))));
724 i= DIG_PER_DEC1 - i + 1;
728 stop= (int) ((buf_end - from->buf + 1) * DIG_PER_DEC1);
731 for (; *buf_end % powers10[i++] == 0; stop--) {};
753 dec1 *from= dec->buf + round_up(beg + 1) - 1;
754 dec1 *end= dec->buf + round_up(last) - 1;
755 int c_shift= DIG_PER_DEC1 - shift;
756 assert(from >= dec->buf);
757 assert(end < dec->buf + dec->len);
758 if (beg % DIG_PER_DEC1 < shift)
759 *(from - 1)= (*from) / powers10[c_shift];
760 for(; from < end; from++)
761 *from= ((*from % powers10[c_shift]) * powers10[shift] +
762 (*(from + 1)) / powers10[c_shift]);
763 *from= (*from % powers10[c_shift]) * powers10[shift];
781 dec1 *from= dec->buf + round_up(last) - 1;
782 dec1 *end= dec->buf + round_up(beg + 1) - 1;
783 int c_shift= DIG_PER_DEC1 - shift;
784 assert(from < dec->buf + dec->len);
785 assert(end >= dec->buf);
786 if (DIG_PER_DEC1 - ((last - 1) % DIG_PER_DEC1 + 1) < shift)
787 *(from + 1)= (*from % powers10[shift]) * powers10[c_shift];
788 for(; from > end; from--)
789 *from= (*from / powers10[shift] +
790 (*(from - 1) % powers10[shift]) * powers10[c_shift]);
791 *from= *from / powers10[shift];
818 int point= round_up(dec->intg) * DIG_PER_DEC1;
820 int new_point= point + shift;
822 int digits_int, digits_frac;
824 int new_len, new_frac_len;
840 digits_int= new_point - beg;
841 set_if_bigger(digits_int, 0);
842 digits_frac= end - new_point;
843 set_if_bigger(digits_frac, 0);
845 if ((new_len= round_up(digits_int) + (new_frac_len= round_up(digits_frac))) >
848 int lack= new_len - dec->len;
851 if (new_frac_len < lack)
852 return E_DEC_OVERFLOW;
855 err= E_DEC_TRUNCATED;
857 diff= digits_frac - (new_frac_len * DIG_PER_DEC1);
861 digits_frac= new_frac_len * DIG_PER_DEC1;
871 return E_DEC_TRUNCATED;
875 if (shift % DIG_PER_DEC1)
877 int l_mini_shift, r_mini_shift, mini_shift;
885 l_mini_shift= shift % DIG_PER_DEC1;
886 r_mini_shift= DIG_PER_DEC1 - l_mini_shift;
892 do_left= l_mini_shift <= beg;
893 assert(do_left || (dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
897 r_mini_shift= (-shift) % DIG_PER_DEC1;
898 l_mini_shift= DIG_PER_DEC1 - r_mini_shift;
900 do_left= !((dec->len * DIG_PER_DEC1 - end) >= r_mini_shift);
901 assert(!do_left || l_mini_shift <= beg);
906 mini_shift= (-l_mini_shift);
911 mini_shift= r_mini_shift;
913 new_point+= mini_shift;
918 if (!(shift+= mini_shift) && (new_point - digits_int) < DIG_PER_DEC1)
920 dec->intg= digits_int;
921 dec->frac= digits_frac;
929 if ((new_front= (new_point - digits_int)) >= DIG_PER_DEC1 ||
938 d_shift= new_front / DIG_PER_DEC1;
939 to= dec->buf + (round_up(beg + 1) - 1 - d_shift);
940 barier= dec->buf + (round_up(end) - 1 - d_shift);
941 assert(to >= dec->buf);
942 assert(barier + d_shift < dec->buf + dec->len);
943 for(; to <= barier; to++)
944 *to= *(to + d_shift);
945 for(barier+= d_shift; to <= barier; to++)
952 d_shift= (1 - new_front) / DIG_PER_DEC1;
953 to= dec->buf + round_up(end) - 1 + d_shift;
954 barier= dec->buf + round_up(beg + 1) - 1 + d_shift;
955 assert(to < dec->buf + dec->len);
956 assert(barier - d_shift >= dec->buf);
957 for(; to >= barier; to--)
958 *to= *(to - d_shift);
959 for(barier-= d_shift; to >= barier; to--)
962 d_shift*= DIG_PER_DEC1;
973 beg= round_up(beg + 1) - 1;
974 end= round_up(end) - 1;
975 assert(new_point >= 0);
979 new_point= round_up(new_point) - 1;
985 dec->buf[new_point]=0;
986 }
while (--new_point > end);
990 for (; new_point < beg; new_point++)
991 dec->buf[new_point]= 0;
993 dec->intg= digits_int;
994 dec->frac= digits_frac;
1022 char *end_of_string = *end;
1024 int i, intg, frac, error, intg1, frac1;
1028 error= E_DEC_BAD_NUM;
1029 while (s < end_of_string && my_charset_utf8_general_ci.isspace(*s))
1031 if (s == end_of_string)
1034 if ((to->sign= (*s ==
'-')))
1040 while (s < end_of_string && my_charset_utf8_general_ci.isdigit(*s))
1043 if (s < end_of_string && *s==
'.')
1046 while (endp < end_of_string && my_charset_utf8_general_ci.isdigit(*endp))
1048 frac= (int) (endp - s - 1);
1064 if (frac > to->frac)
1066 error=E_DEC_TRUNCATED;
1069 if (intg > to->intg)
1071 error=E_DEC_OVERFLOW;
1074 intg1=round_up(intg);
1075 frac1=round_up(frac);
1076 if (intg1+frac1 > to->len)
1084 intg1=round_up(intg);
1085 frac1=round_up(frac);
1086 fix_intg_frac_error(to->len, intg1, frac1, error);
1087 if (DRIZZLE_UNLIKELY(error))
1089 frac=frac1*DIG_PER_DEC1;
1090 if (error == E_DEC_OVERFLOW)
1091 intg=intg1*DIG_PER_DEC1;
1101 for (x=0, i=0; intg; intg--)
1103 x+= (*--s -
'0')*powers10[i];
1105 if (DRIZZLE_UNLIKELY(++i == DIG_PER_DEC1))
1116 for (x=0, i=0; frac; frac--)
1118 x= (*++s1 -
'0') + x*10;
1120 if (DRIZZLE_UNLIKELY(++i == DIG_PER_DEC1))
1128 *buf=x*powers10[DIG_PER_DEC1-i];
1131 if (endp+1 < end_of_string && (*endp ==
'e' || *endp ==
'E'))
1134 const int64_t exponent= internal::my_strtoll10(endp+1, (
char**) &end_of_string,
1137 if (end_of_string != endp +1)
1139 *end= (
char*) end_of_string;
1142 error= E_DEC_BAD_NUM;
1145 if (exponent > INT_MAX/2 || (str_error == 0 && exponent < 0))
1147 error= E_DEC_OVERFLOW;
1150 if (exponent < INT_MIN/2 && error != E_DEC_OVERFLOW)
1152 error= E_DEC_TRUNCATED;
1155 if (error != E_DEC_OVERFLOW)
1179 char strbuf[FLOATING_POINT_BUFFER];
1180 int len=
sizeof(strbuf);
1182 char* end= strbuf + len;
1184 *to= internal::my_strtod(strbuf, &end, &error);
1185 return rc != E_DEC_OK ? rc : (error ? E_DEC_OVERFLOW : E_DEC_OK);
1200 char buff[FLOATING_POINT_BUFFER], *end;
1202 end= buff + internal::my_gcvt(from,
1203 internal::MY_GCVT_ARG_DOUBLE,
1204 sizeof(buff) - 1, buff, NULL);
1205 res= string2decimal(buff, to, &end);
1210static int ull2dec(uint64_t from, decimal_t *to)
1212 int intg1, error=E_DEC_OK;
1218 for (intg1=1; from >= DIG_BASE; intg1++, from/=DIG_BASE) {};
1219 if (DRIZZLE_UNLIKELY(intg1 > to->len))
1222 error=E_DEC_OVERFLOW;
1225 to->intg=intg1*DIG_PER_DEC1;
1227 for (buf=to->buf+intg1; intg1; intg1--)
1229 uint64_t y=x/DIG_BASE;
1230 *--buf=(dec1)(x-y*DIG_BASE);
1236int uint64_t2decimal(
const uint64_t from, decimal_t *to)
1239 return ull2dec(from, to);
1242int int64_t2decimal(
const int64_t from, decimal_t *to)
1244 if ((to->sign= from < 0))
1245 return ull2dec(-from, to);
1246 return ull2dec(from, to);
1249int decimal2uint64_t(
const decimal_t *from, uint64_t *to)
1251 dec1 *buf=from->buf;
1258 return E_DEC_OVERFLOW;
1261 for (intg=from->intg; intg > 0; intg-=DIG_PER_DEC1)
1264 x=x*DIG_BASE + *buf++;
1265 if (DRIZZLE_UNLIKELY(y > ((uint64_t) UINT64_MAX/DIG_BASE) || x < y))
1268 return E_DEC_OVERFLOW;
1272 for (frac=from->frac; DRIZZLE_UNLIKELY(frac > 0); frac-=DIG_PER_DEC1)
1274 return E_DEC_TRUNCATED;
1278int decimal2int64_t(
const decimal_t *from, int64_t *to)
1280 dec1 *buf=from->buf;
1284 for (intg=from->intg; intg > 0; intg-=DIG_PER_DEC1)
1293 x=x*DIG_BASE - *buf++;
1294 if (DRIZZLE_UNLIKELY(y < (INT64_MIN/DIG_BASE) || x > y))
1300 *to= from->sign ? INT64_MIN : INT64_MAX;
1301 return E_DEC_OVERFLOW;
1305 if (DRIZZLE_UNLIKELY(from->sign==0 && x == INT64_MIN))
1308 return E_DEC_OVERFLOW;
1311 *to=from->sign ? x : -x;
1312 for (frac=from->frac; DRIZZLE_UNLIKELY(frac > 0); frac-=DIG_PER_DEC1)
1314 return E_DEC_TRUNCATED;
1399 dec1 mask=from->sign ? -1 : 0, *buf1=from->buf, *stop1;
1400 int error=E_DEC_OK, intg=precision-frac,
1401 isize1, intg1, intg1x, from_intg,
1402 intg0=intg/DIG_PER_DEC1,
1403 frac0=frac/DIG_PER_DEC1,
1404 intg0x=intg-intg0*DIG_PER_DEC1,
1405 frac0x=frac-frac0*DIG_PER_DEC1,
1406 frac1=from->frac/DIG_PER_DEC1,
1407 frac1x=from->frac-frac1*DIG_PER_DEC1,
1408 isize0=intg0*
sizeof(dec1)+dig2bytes[intg0x],
1409 fsize0=frac0*
sizeof(dec1)+dig2bytes[frac0x],
1410 fsize1=frac1*
sizeof(dec1)+dig2bytes[frac1x];
1411 const int orig_isize0= isize0;
1412 const int orig_fsize0= fsize0;
1413 unsigned char *orig_to= to;
1415 buf1= remove_leading_zeroes(from, &from_intg);
1417 if (DRIZZLE_UNLIKELY(from_intg+fsize1==0))
1424 intg1=from_intg/DIG_PER_DEC1;
1425 intg1x=from_intg-intg1*DIG_PER_DEC1;
1426 isize1=intg1*
sizeof(dec1)+dig2bytes[intg1x];
1428 if (intg < from_intg)
1430 buf1+=intg1-intg0+(intg1x>0)-(intg0x>0);
1431 intg1=intg0; intg1x=intg0x;
1432 error=E_DEC_OVERFLOW;
1434 else if (isize0 > isize1)
1436 while (isize0-- > isize1)
1439 if (fsize0 < fsize1)
1441 frac1=frac0; frac1x=frac0x;
1442 error=E_DEC_TRUNCATED;
1444 else if (fsize0 > fsize1 && frac1x)
1461 int i=dig2bytes[intg1x];
1462 dec1 x=(*buf1++ % powers10[intg1x]) ^ mask;
1465 case 1: mi_int1store(to, x);
break;
1466 case 2: mi_int2store(to, x);
break;
1467 case 3: mi_int3store(to, x);
break;
1468 case 4: mi_int4store(to, x);
break;
1475 for (stop1=buf1+intg1+frac1; buf1 < stop1; to+=
sizeof(dec1))
1477 dec1 x=*buf1++ ^ mask;
1478 assert(
sizeof(dec1) == 4);
1479 mi_int4store(to, x);
1486 int i=dig2bytes[frac1x],
1487 lim=(frac1 < frac0 ? DIG_PER_DEC1 : frac0x);
1488 while (frac1x < lim && dig2bytes[frac1x] == i)
1490 x=(*buf1 / powers10[DIG_PER_DEC1 - frac1x]) ^ mask;
1493 case 1: mi_int1store(to, x);
break;
1494 case 2: mi_int2store(to, x);
break;
1495 case 3: mi_int3store(to, x);
break;
1496 case 4: mi_int4store(to, x);
break;
1501 if (fsize0 > fsize1)
1503 unsigned char *to_end= orig_to + orig_fsize0 + orig_isize0;
1505 while (fsize0-- > fsize1 && to < to_end)
1506 *to++= (
unsigned char)mask;
1511 assert(to == orig_to + orig_fsize0 + orig_isize0);
1532 int error=E_DEC_OK, intg=precision-scale,
1533 intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1,
1534 intg0x=intg-intg0*DIG_PER_DEC1, frac0x=scale-frac0*DIG_PER_DEC1,
1535 intg1=intg0+(intg0x>0), frac1=frac0+(frac0x>0);
1536 dec1 *buf=to->buf, mask=(*from & 0x80) ? 0 : -1;
1537 const unsigned char *stop;
1538 unsigned char *d_copy;
1542 d_copy= (
unsigned char*) alloca(bin_size);
1543 memcpy(d_copy, from, bin_size);
1547 fix_intg_frac_error(to->len, intg1, frac1, error);
1548 if (DRIZZLE_UNLIKELY(error))
1550 if (intg1 < intg0+(intg0x>0))
1552 from+=dig2bytes[intg0x]+
sizeof(dec1)*(intg0-intg1);
1553 frac0=frac0x=intg0x=0;
1563 to->sign=(mask != 0);
1564 to->intg=intg0*DIG_PER_DEC1+intg0x;
1565 to->frac=frac0*DIG_PER_DEC1+frac0x;
1569 int i=dig2bytes[intg0x];
1573 case 1: x=mi_sint1korr(from);
break;
1574 case 2: x=mi_sint2korr(from);
break;
1575 case 3: x=mi_sint3korr(from);
break;
1576 case 4: x=mi_sint4korr(from);
break;
1581 if (((uint64_t)*buf) >= (uint64_t) powers10[intg0x+1])
1583 if (buf > to->buf || *buf != 0)
1588 for (stop=from+intg0*
sizeof(dec1); from < stop; from+=
sizeof(dec1))
1590 assert(
sizeof(dec1) == 4);
1591 *buf=mi_sint4korr(from) ^ mask;
1592 if (((uint32_t)*buf) > DIG_MAX)
1594 if (buf > to->buf || *buf != 0)
1597 to->intg-=DIG_PER_DEC1;
1599 assert(to->intg >=0);
1600 for (stop=from+frac0*
sizeof(dec1); from < stop; from+=
sizeof(dec1))
1602 assert(
sizeof(dec1) == 4);
1603 *buf=mi_sint4korr(from) ^ mask;
1604 if (((uint32_t)*buf) > DIG_MAX)
1610 int i=dig2bytes[frac0x];
1614 case 1: x=mi_sint1korr(from);
break;
1615 case 2: x=mi_sint2korr(from);
break;
1616 case 3: x=mi_sint3korr(from);
break;
1617 case 4: x=mi_sint4korr(from);
break;
1620 *buf=(x ^ mask) * powers10[DIG_PER_DEC1 - frac0x];
1621 if (((uint32_t)*buf) > DIG_MAX)
1629 return(E_DEC_BAD_NUM);
1639 int intg=precision-scale,
1640 intg0=intg/DIG_PER_DEC1, frac0=scale/DIG_PER_DEC1,
1641 intg0x=intg-intg0*DIG_PER_DEC1, frac0x=scale-frac0*DIG_PER_DEC1;
1643 assert(scale >= 0 && precision > 0 && scale <= precision);
1644 return intg0*
sizeof(dec1)+dig2bytes[intg0x]+
1645 frac0*
sizeof(dec1)+dig2bytes[frac0x];
1665 decimal_round_mode mode)
1667 int frac0=scale>0 ? round_up(scale) : scale/DIG_PER_DEC1,
1668 frac1=round_up(from->frac), round_digit= 0,
1669 intg0=round_up(from->intg), error=E_DEC_OK, len=to->len,
1670 intg1=round_up(from->intg +
1671 (((intg0 + frac0)>0) && (from->buf[0] == DIG_MAX)));
1672 dec1 *buf0=from->buf, *buf1=to->buf, x, y, carry=0;
1679 case HALF_EVEN: round_digit=5;
break;
1680 case CEILING: round_digit= from->sign ? 10 : 0;
break;
1681 case FLOOR: round_digit= from->sign ? 0 : 10;
break;
1682 case TRUNCATE: round_digit=10;
break;
1686 if (DRIZZLE_UNLIKELY(frac0+intg0 > len))
1689 scale=frac0*DIG_PER_DEC1;
1690 error=E_DEC_TRUNCATED;
1693 if (scale+from->intg < 0)
1699 if (to != from || intg1>intg0)
1701 dec1 *p0= buf0+intg0+max(frac1, frac0);
1702 dec1 *p1= buf1+intg1+max(frac1, frac0);
1706 if (DRIZZLE_UNLIKELY(intg1 > intg0))
1712 to->sign=from->sign;
1713 to->intg=min(intg0, len)*DIG_PER_DEC1;
1719 while (frac0-- > frac1)
1724 if (scale >= from->frac)
1727 buf0+=intg0+frac0-1;
1728 buf1+=intg0+frac0-1;
1729 if (scale == frac0*DIG_PER_DEC1)
1732 assert(frac0+intg0 >= 0);
1733 switch (round_digit) {
1736 dec1 *p0= buf0 + (frac1-frac0);
1737 for (; p0 > buf0; p0--)
1749 x= buf0[1]/DIG_MASK;
1750 do_inc= (x>5) || ((x == 5) &&
1751 (mode == HALF_UP || (frac0+intg0 > 0 && *buf0 & 1)));
1764 else if (frac0+intg0==0)
1773 int pos=frac0*DIG_PER_DEC1-scale-1;
1774 assert(frac0+intg0 > 0);
1775 x=*buf1 / powers10[pos];
1777 if (y > round_digit ||
1778 (round_digit == 5 && y == 5 && (mode == HALF_UP || (x/10) & 1)))
1780 *buf1=powers10[pos]*(x-y);
1794 dec1 *buf= to->buf + ((scale == 0 && intg0 == 0) ? 1 : intg0 + frac0);
1795 dec1 *end= to->buf + len;
1800 if (*buf1 >= DIG_BASE)
1804 while (carry && --buf1 >= to->buf)
1805 add(*buf1, *buf1, 0, carry);
1806 if (DRIZZLE_UNLIKELY(carry))
1809 if (frac0+intg0 >= len)
1812 scale=frac0*DIG_PER_DEC1;
1813 error=E_DEC_TRUNCATED;
1815 for (buf1=to->buf+intg0+max(frac0,0); buf1 > to->buf; buf1--)
1827 if (DRIZZLE_LIKELY(*buf1))
1829 if (buf1-- == to->buf)
1832 dec1 *p0= to->buf + frac0 + 1;
1834 to->frac= max(scale, 0);
1836 for (buf1= to->buf; buf1<p0; buf1++)
1844 first_dig= to->intg % DIG_PER_DEC1;
1845 if (first_dig && (*buf1 >= powers10[first_dig]))
1856static int do_add(
const decimal_t *from1,
const decimal_t *from2, decimal_t *to)
1858 int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
1859 frac1=round_up(from1->frac), frac2=round_up(from2->frac),
1860 frac0=max(frac1, frac2), intg0=max(intg1, intg2), error;
1861 dec1 *buf1, *buf2, *buf0, *stop, *stop2, x, carry;
1866 x=intg1 > intg2 ? from1->buf[0] :
1867 intg2 > intg1 ? from2->buf[0] :
1868 from1->buf[0] + from2->buf[0] ;
1869 if (DRIZZLE_UNLIKELY(x > DIG_MAX-1))
1875 fix_intg_frac_error(to->len, intg0, frac0, error);
1876 if (DRIZZLE_UNLIKELY(error == E_DEC_OVERFLOW))
1882 buf0=to->buf+intg0+frac0;
1884 to->sign=from1->sign;
1885 to->frac=max(from1->frac, from2->frac);
1886 to->intg=intg0*DIG_PER_DEC1;
1887 if (DRIZZLE_UNLIKELY(error))
1889 set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
1890 set_if_smaller(frac1, frac0);
1891 set_if_smaller(frac2, frac0);
1892 set_if_smaller(intg1, intg0);
1893 set_if_smaller(intg2, intg0);
1899 buf1=from1->buf+intg1+frac1;
1900 stop=from1->buf+intg1+frac2;
1901 buf2=from2->buf+intg2+frac2;
1902 stop2=from1->buf+(intg1 > intg2 ? intg1-intg2 : 0);
1906 buf1=from2->buf+intg2+frac2;
1907 stop=from2->buf+intg2+frac1;
1908 buf2=from1->buf+intg1+frac1;
1909 stop2=from2->buf+(intg2 > intg1 ? intg2-intg1 : 0);
1916 while (buf1 > stop2)
1918 add(*--buf0, *--buf1, *--buf2, carry);
1922 buf1= intg1 > intg2 ? ((stop=from1->buf)+intg1-intg2) :
1923 ((stop=from2->buf)+intg2-intg1) ;
1926 add(*--buf0, *--buf1, 0, carry);
1929 if (DRIZZLE_UNLIKELY(carry))
1931 assert(buf0 == to->buf || buf0 == to->buf+1);
1938static int do_sub(
const decimal_t *from1,
const decimal_t *from2, decimal_t *to)
1940 int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
1941 frac1=round_up(from1->frac), frac2=round_up(from2->frac);
1942 int frac0=max(frac1, frac2), error;
1943 dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry=0;
1946 start1=buf1=from1->buf; stop1=buf1+intg1;
1947 start2=buf2=from2->buf; stop2=buf2+intg2;
1948 if (DRIZZLE_UNLIKELY(*buf1 == 0))
1950 while (buf1 < stop1 && *buf1 == 0)
1953 intg1= (int) (stop1-buf1);
1955 if (DRIZZLE_UNLIKELY(*buf2 == 0))
1957 while (buf2 < stop2 && *buf2 == 0)
1960 intg2= (int) (stop2-buf2);
1964 else if (intg2 == intg1)
1966 dec1 *end1= stop1 + (frac1 - 1);
1967 dec1 *end2= stop2 + (frac2 - 1);
1968 while (DRIZZLE_UNLIKELY((buf1 <= end1) && (*end1 == 0)))
1970 while (DRIZZLE_UNLIKELY((buf2 <= end2) && (*end2 == 0)))
1972 frac1= (int) (end1 - stop1) + 1;
1973 frac2= (int) (end2 - stop2) + 1;
1974 while (buf1 <=end1 && buf2 <= end2 && *buf1 == *buf2)
1979 carry= *buf2 > *buf1;
2000 return carry == from1->sign ? 1 : -1;
2004 to->sign=from1->sign;
2010 swap(start1, start2);
2013 to->sign= 1 - to->sign;
2016 fix_intg_frac_error(to->len, intg1, frac0, error);
2017 buf0=to->buf+intg1+frac0;
2019 to->frac=max(from1->frac, from2->frac);
2020 to->intg=intg1*DIG_PER_DEC1;
2021 if (DRIZZLE_UNLIKELY(error))
2023 set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
2024 set_if_smaller(frac1, frac0);
2025 set_if_smaller(frac2, frac0);
2026 set_if_smaller(intg2, intg1);
2033 buf1=start1+intg1+frac1;
2034 stop1=start1+intg1+frac2;
2035 buf2=start2+intg2+frac2;
2036 while (frac0-- > frac1)
2038 while (buf1 > stop1)
2043 buf1=start1+intg1+frac1;
2044 buf2=start2+intg2+frac2;
2045 stop2=start2+intg2+frac1;
2046 while (frac0-- > frac2)
2048 while (buf2 > stop2)
2050 sub(*--buf0, 0, *--buf2, carry);
2055 while (buf2 > start2)
2057 sub(*--buf0, *--buf1, *--buf2, carry);
2061 while (carry && buf1 > start1)
2063 sub(*--buf0, *--buf1, 0, carry);
2066 while (buf1 > start1)
2069 while (buf0 > to->buf)
2075int decimal_intg(
const decimal_t *from)
2078 remove_leading_zeroes(from, &res);
2082int decimal_add(
const decimal_t *from1,
const decimal_t *from2, decimal_t *to)
2084 if (DRIZZLE_LIKELY(from1->sign == from2->sign))
2085 return do_add(from1, from2, to);
2086 return do_sub(from1, from2, to);
2089int decimal_sub(
const decimal_t *from1,
const decimal_t *from2, decimal_t *to)
2091 if (DRIZZLE_LIKELY(from1->sign == from2->sign))
2092 return do_sub(from1, from2, to);
2093 return do_add(from1, from2, to);
2096int decimal_cmp(
const decimal_t *from1,
const decimal_t *from2)
2098 if (DRIZZLE_LIKELY(from1->sign == from2->sign))
2099 return do_sub(from1, from2, 0);
2100 return from1->sign > from2->sign ? -1 : 1;
2103int decimal_t::isZero()
const
2106 *end= buf1 +round_up(intg) +round_up(frac);
2141 int intg1=round_up(from1->intg), intg2=round_up(from2->intg),
2142 frac1=round_up(from1->frac), frac2=round_up(from2->frac),
2143 intg0=round_up(from1->intg+from2->intg),
2144 frac0=frac1+frac2, error, i, j, d_to_move;
2145 dec1 *buf1=from1->buf+intg1, *buf2=from2->buf+intg2, *buf0,
2146 *start2, *stop2, *stop1, *start0, carry;
2152 fix_intg_frac_error(to->len, intg0, frac0, error);
2153 to->sign=from1->sign != from2->sign;
2154 to->frac=from1->frac+from2->frac;
2155 to->intg=intg0*DIG_PER_DEC1;
2157 if (DRIZZLE_UNLIKELY(error))
2159 set_if_smaller(to->frac, frac0*DIG_PER_DEC1);
2160 set_if_smaller(to->intg, intg0*DIG_PER_DEC1);
2161 if (DRIZZLE_UNLIKELY(i > intg0))
2177 start0=to->buf+intg0+frac0-1;
2178 start2=buf2+frac2-1;
2182 memset(to->buf, 0, (intg0+frac0)*
sizeof(dec1));
2184 for (buf1+=frac1-1; buf1 >= stop1; buf1--, start0--)
2187 for (buf0=start0, buf2=start2; buf2 >= stop2; buf2--, buf0--)
2190 dec2 p= ((dec2)*buf1) * ((dec2)*buf2);
2191 hi=(dec1)(p/DIG_BASE);
2192 lo=(dec1)(p-((dec2)hi)*DIG_BASE);
2193 add2(*buf0, *buf0, lo, carry);
2199 return E_DEC_OVERFLOW;
2200 add2(*buf0, *buf0, 0, carry);
2202 for (buf0--; carry; buf0--)
2205 return E_DEC_OVERFLOW;
2206 add(*buf0, *buf0, 0, carry);
2214 dec1 *end= to->buf + intg0 + frac0;
2229 d_to_move= intg0 + round_up(to->frac);
2230 while (!*buf1 && (to->intg > DIG_PER_DEC1))
2233 to->intg-= DIG_PER_DEC1;
2238 dec1 *cur_d= to->buf;
2239 for (; d_to_move--; cur_d++, buf1++)
2259 int frac1=round_up(from1->frac)*DIG_PER_DEC1, prec1=from1->intg+frac1,
2260 frac2=round_up(from2->frac)*DIG_PER_DEC1, prec2=from2->intg+frac2,
2261 error= 0, i, intg0, frac0, len1, len2, dintg, div_mod=(!mod);
2262 dec1 *buf0, *buf1=from1->buf, *buf2=from2->buf, *tmp1,
2263 *start2, *stop2, *stop1, *stop0, norm2, carry, *start1, dcarry;
2264 dec2 norm_factor, x, guess, y;
2272 i= ((prec2 - 1) % DIG_PER_DEC1) + 1;
2273 while (prec2 > 0 && *buf2 == 0)
2280 return E_DEC_DIV_ZERO;
2281 for (i= (prec2 - 1) % DIG_PER_DEC1; *buf2 < powers10[i--]; prec2--) ;
2284 i=((prec1-1) % DIG_PER_DEC1)+1;
2285 while (prec1 > 0 && *buf1 == 0)
2296 for (i=(prec1-1) % DIG_PER_DEC1; *buf1 < powers10[i--]; prec1--) ;
2300 if ((scale_incr-= frac1 - from1->frac + frac2 - from2->frac) < 0)
2303 dintg=(prec1-frac1)-(prec2-frac2)+(*buf1 >= *buf2);
2306 dintg/=DIG_PER_DEC1;
2310 intg0=round_up(dintg);
2318 to->sign=from1->sign;
2319 to->frac=max(from1->frac, from2->frac);
2334 frac0=round_up(frac1+frac2+scale_incr);
2335 fix_intg_frac_error(to->len, intg0, frac0, error);
2336 to->sign=from1->sign != from2->sign;
2337 to->intg=intg0*DIG_PER_DEC1;
2338 to->frac=frac0*DIG_PER_DEC1;
2341 stop0=buf0+intg0+frac0;
2342 if (DRIZZLE_LIKELY(div_mod))
2346 len1=(i=round_up(prec1))+round_up(2*frac2+scale_incr+1) + 1;
2347 set_if_bigger(len1, 3);
2348 if (!(tmp1=(dec1 *)alloca(len1*
sizeof(dec1))))
2350 memcpy(tmp1, buf1, i*
sizeof(dec1));
2351 memset(tmp1+i, 0, (len1-i)*
sizeof(dec1));
2356 stop2=buf2+round_up(prec2)-1;
2359 while (*stop2 == 0 && stop2 >= start2)
2361 len2= (int) (stop2++ - start2);
2371 norm_factor=DIG_BASE/(*start2+1);
2372 norm2=(dec1)(norm_factor*start2[0]);
2373 if (DRIZZLE_LIKELY(len2>0))
2374 norm2+=(dec1)(norm_factor*start2[1]/DIG_BASE);
2376 if (*start1 < *start2)
2382 for (; buf0 < stop0; buf0++)
2385 if (DRIZZLE_UNLIKELY(dcarry == 0 && *start1 < *start2))
2390 x=start1[0]+((dec2)dcarry)*DIG_BASE;
2392 guess=(norm_factor*x+norm_factor*y/DIG_BASE)/norm2;
2393 if (DRIZZLE_UNLIKELY(guess >= DIG_BASE))
2395 if (DRIZZLE_LIKELY(len2>0))
2398 if (start2[1]*guess > (x-guess*start2[0])*DIG_BASE+y)
2400 if (DRIZZLE_UNLIKELY(start2[1]*guess > (x-guess*start2[0])*DIG_BASE+y))
2402 assert(start2[1]*guess <= (x-guess*start2[0])*DIG_BASE+y);
2408 assert(buf1 < stop1);
2409 for (carry=0; buf2 > start2; buf1--)
2412 x=guess * (*--buf2);
2413 hi=(dec1)(x/DIG_BASE);
2414 lo=(dec1)(x-((dec2)hi)*DIG_BASE);
2415 sub2(*buf1, *buf1, lo, carry);
2418 carry= dcarry < carry;
2421 if (DRIZZLE_UNLIKELY(carry))
2427 for (carry=0; buf2 > start2; buf1--)
2429 add(*buf1, *buf1, *--buf2, carry);
2433 if (DRIZZLE_LIKELY(div_mod))
2448 intg0=(int) (round_up(prec1-frac1)-(start1-tmp1));
2449 frac0=round_up(to->frac);
2451 if (DRIZZLE_UNLIKELY(frac0==0 && intg0==0))
2458 if (DRIZZLE_UNLIKELY(-intg0 >= to->len))
2461 error=E_DEC_TRUNCATED;
2472 if (DRIZZLE_UNLIKELY(intg0 > to->len))
2476 error=E_DEC_OVERFLOW;
2479 assert(intg0 <= round_up(from2->intg));
2480 stop1=start1+frac0+intg0;
2481 to->intg=min(intg0*DIG_PER_DEC1, from2->intg);
2483 if (DRIZZLE_UNLIKELY(intg0+frac0 > to->len))
2485 stop1-=frac0+intg0-to->len;
2486 frac0=to->len-intg0;
2487 to->frac=frac0*DIG_PER_DEC1;
2488 error=E_DEC_TRUNCATED;
2490 assert(buf0 + (stop1 - start1) <= to->buf + to->len);
2491 while (start1 < stop1)
2514 return do_div_mod(from1, from2, to, 0, scale_incr);
2547std::ostream& operator<<(std::ostream& output,
const type::Decimal &dec)
2553 output <<
"type::Decimal:(";
2554 output << str.c_ptr();
2566char buf1[100], buf2[100], buf3[100];
2571 printf(
"/* intg=%d, frac=%d, sign=%d, buf[]={", d->intg, d->frac, d->sign);
2572 for (i=0; i < round_up(d->frac)+round_up(d->intg)-1; i++)
2573 printf(
"%09d, ", d->buf[i]);
2574 printf(
"%09d} */ ", d->buf[i]);
2578void check_result_code(
int actual,
int want)
2582 printf(
"\n^^^^^^^^^^^^^ must return %d\n", want);
2588void print_decimal(
decimal_t *d,
const char *orig,
int actual,
int want)
2593 if (full) dump_decimal(d);
2596 check_result_code(actual, want);
2597 if (orig && strcmp(orig, s))
2599 printf(
"\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
2610 printf(
"==== decimal2string ====\n");
2611 a.buf[0]=12345; a.intg=5; a.frac=0; a.sign=0;
2614 dump_decimal(&a); printf(
" --> res=%d str='%s' len=%d\n", res, s, slen);
2616 a.buf[1]=987000000; a.frac=3;
2619 dump_decimal(&a); printf(
" --> res=%d str='%s' len=%d\n", res, s, slen);
2624 dump_decimal(&a); printf(
" --> res=%d str='%s' len=%d\n", res, s, slen);
2628 dump_decimal(&a); printf(
" --> res=%d str='%s' len=%d\n", res, s, slen);
2632 dump_decimal(&a); printf(
" --> res=%d str='%s' len=%d\n", res, s, slen);
2634 a.buf[0]=987000000; a.frac=3; a.intg=0;
2637 dump_decimal(&a); printf(
" --> res=%d str='%s' len=%d\n", res, s, slen);
2640void test_s2d(
const char *s,
const char *orig,
int ex)
2644 snprintf(s1,
sizeof(s1),
"'%s'", s);
2646 printf(
"len=%2d %-30s => res=%d ", a.len, s1,
2647 (res= string2decimal(s, &a, &end)));
2648 print_decimal(&a, orig, res, ex);
2652void test_d2f(
const char *s,
int ex)
2658 snprintf(s1,
sizeof(s1),
"'%s'", s);
2660 string2decimal(s, &a, &end);
2662 if (full) dump_decimal(&a);
2663 printf(
"%-40s => res=%d %.*g\n", s1, res, a.intg+a.frac, x);
2664 check_result_code(res, ex);
2667void test_d2b2d(
const char *str,
int p,
int s,
const char *orig,
int ex)
2669 char s1[100], buf[100], *end;
2672 snprintf(s1,
sizeof(s1),
"'%s'", str);
2674 string2decimal(str, &a, &end);
2676 printf(
"%-31s {%2d, %2d} => res=%d size=%-2d ", s1, p, s, res, size);
2680 for (
int i= 0; i < size; i++)
2681 printf(
"%02x", ((
unsigned char *)buf)[i]);
2684 printf(
" => res=%d ", res);
2685 print_decimal(&a, orig, res, ex);
2689void test_f2d(
double from,
int ex)
2694 printf(
"%-40.*f => res=%d ", DBL_DIG-2, from, res);
2695 print_decimal(&a, 0, res, ex);
2699void test_ull2d(uint64_t from,
const char *orig,
int ex)
2704 res=uint64_t2decimal(from, &a);
2705 internal::int64_t10_to_str(from,s,10);
2706 printf(
"%-40s => res=%d ", s, res);
2707 print_decimal(&a, orig, res, ex);
2711void test_ll2d(int64_t from,
const char *orig,
int ex)
2716 res=int64_t2decimal(from, &a);
2717 internal::int64_t10_to_str(from,s,-10);
2718 printf(
"%-40s => res=%d ", s, res);
2719 print_decimal(&a, orig, res, ex);
2723void test_d2ull(
const char *s,
const char *orig,
int ex)
2730 string2decimal(s, &a, &end);
2731 res=decimal2uint64_t(&a, &x);
2732 if (full) dump_decimal(&a);
2733 internal::int64_t10_to_str(x,s1,10);
2734 printf(
"%-40s => res=%d %s\n", s, res, s1);
2735 check_result_code(res, ex);
2736 if (orig && strcmp(orig, s1))
2738 printf(
"\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
2743void test_d2ll(
const char *s,
const char *orig,
int ex)
2750 string2decimal(s, &a, &end);
2751 res=decimal2int64_t(&a, &x);
2752 if (full) dump_decimal(&a);
2753 internal::int64_t10_to_str(x,s1,-10);
2754 printf(
"%-40s => res=%d %s\n", s, res, s1);
2755 check_result_code(res, ex);
2756 if (orig && strcmp(orig, s1))
2758 printf(
"\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
2763void test_da(
const char *s1,
const char *s2,
const char *orig,
int ex)
2767 snprintf(s,
sizeof(s),
"'%s' + '%s'", s1, s2);
2769 string2decimal(s1, &a, &end);
2771 string2decimal(s2, &b, &end);
2772 res=decimal_add(&a, &b, &c);
2773 printf(
"%-40s => res=%d ", s, res);
2774 print_decimal(&c, orig, res, ex);
2778void test_ds(
const char *s1,
const char *s2,
const char *orig,
int ex)
2782 snprintf(s,
sizeof(s),
"'%s' - '%s'", s1, s2);
2784 string2decimal(s1, &a, &end);
2786 string2decimal(s2, &b, &end);
2787 res=decimal_sub(&a, &b, &c);
2788 printf(
"%-40s => res=%d ", s, res);
2789 print_decimal(&c, orig, res, ex);
2793void test_dc(
const char *s1,
const char *s2,
int orig)
2797 snprintf(s,
sizeof(s),
"'%s' <=> '%s'", s1, s2);
2799 string2decimal(s1, &a, &end);
2801 string2decimal(s2, &b, &end);
2802 res=decimal_cmp(&a, &b);
2803 printf(
"%-40s => res=%d\n", s, res);
2806 printf(
"\n^^^^^^^^^^^^^ must've been %d\n", orig);
2811void test_dm(
const char *s1,
const char *s2,
const char *orig,
int ex)
2815 snprintf(s,
sizeof(s),
"'%s' * '%s'", s1, s2);
2817 string2decimal(s1, &a, &end);
2819 string2decimal(s2, &b, &end);
2821 printf(
"%-40s => res=%d ", s, res);
2822 print_decimal(&c, orig, res, ex);
2826void test_dv(
const char *s1,
const char *s2,
const char *orig,
int ex)
2830 snprintf(s,
sizeof(s),
"'%s' / '%s'", s1, s2);
2832 string2decimal(s1, &a, &end);
2834 string2decimal(s2, &b, &end);
2836 printf(
"%-40s => res=%d ", s, res);
2837 check_result_code(res, ex);
2838 if (res == E_DEC_DIV_ZERO)
2839 printf(
"E_DEC_DIV_ZERO");
2841 print_decimal(&c, orig, res, ex);
2845void test_md(
const char *s1,
const char *s2,
const char *orig,
int ex)
2849 snprintf(s,
sizeof(s),
"'%s' %% '%s'", s1, s2);
2851 string2decimal(s1, &a, &end);
2853 string2decimal(s2, &b, &end);
2855 printf(
"%-40s => res=%d ", s, res);
2856 check_result_code(res, ex);
2857 if (res == E_DEC_DIV_ZERO)
2858 printf(
"E_DEC_DIV_ZERO");
2860 print_decimal(&c, orig, res, ex);
2864const char *round_mode[]=
2865{
"TRUNCATE",
"HALF_EVEN",
"HALF_UP",
"CEILING",
"FLOOR"};
2867void test_ro(
const char *s1,
int n, decimal_round_mode mode,
const char *orig,
2872 snprintf(s,
sizeof(s),
"'%s', %d, %s", s1, n, round_mode[mode]);
2874 string2decimal(s1, &a, &end);
2876 printf(
"%-40s => res=%d ", s, res);
2877 print_decimal(&b, orig, res, ex);
2882void test_mx(
int precision,
int frac,
const char *orig)
2885 snprintf(s,
sizeof(s),
"%d, %d", precision, frac);
2887 printf(
"%-40s => ", s);
2888 print_decimal(&a, orig, 0, 0);
2893void test_pr(
const char *s1,
int prec,
int dec,
char filler,
const char *orig,
2898 int slen=
sizeof(s2);
2901 snprintf(s,
sizeof(s), filler ?
"'%s', %d, %d, '%c'" :
"'%s', %d, %d, '\\0'",
2902 s1, prec, dec, filler);
2904 string2decimal(s1, &a, &end);
2906 printf(
"%-40s => res=%d '%s'", s, res, s2);
2907 check_result_code(res, ex);
2908 if (orig && strcmp(orig, s2))
2910 printf(
"\n^^^^^^^^^^^^^ must've been '%s'\n", orig);
2917void test_sh(
const char *s1,
int shift,
const char *orig,
int ex)
2921 snprintf(s,
sizeof(s),
"'%s' %s %d", s1, ((shift < 0) ?
">>" :
"<<"), abs(shift));
2923 string2decimal(s1, &a, &end);
2925 printf(
"%-40s => res=%d ", s, res);
2926 print_decimal(&a, orig, res, ex);
2931void test_fr(
const char *s1,
const char *orig)
2934 snprintf(s,
sizeof(s),
"'%s'", s1);
2935 printf(
"%-40s => ", s);
2937 string2decimal(s1, &a, &end);
2939 print_decimal(&a, orig, 0, 0);
2947 a.len=
sizeof(buf1)/
sizeof(dec1);
2949 b.len=
sizeof(buf2)/
sizeof(dec1);
2951 c.len=
sizeof(buf3)/
sizeof(dec1);
2956 printf(
"==== string2decimal ====\n");
2957 test_s2d(
"12345",
"12345", 0);
2958 test_s2d(
"12345.",
"12345", 0);
2959 test_s2d(
"123.45",
"123.45", 0);
2960 test_s2d(
"-123.45",
"-123.45", 0);
2961 test_s2d(
".00012345000098765",
"0.00012345000098765", 0);
2962 test_s2d(
".12345000098765",
"0.12345000098765", 0);
2963 test_s2d(
"-.000000012345000098765",
"-0.000000012345000098765", 0);
2964 test_s2d(
"1234500009876.5",
"1234500009876.5", 0);
2966 test_s2d(
"123450000098765",
"98765", 2);
2967 test_s2d(
"123450.000098765",
"123450", 1);
2968 a.len=
sizeof(buf1)/
sizeof(dec1);
2969 test_s2d(
"123E5",
"12300000", 0);
2970 test_s2d(
"123E-2",
"1.23", 0);
2972 printf(
"==== decimal2double ====\n");
2973 test_d2f(
"12345", 0);
2974 test_d2f(
"123.45", 0);
2975 test_d2f(
"-123.45", 0);
2976 test_d2f(
"0.00012345000098765", 0);
2977 test_d2f(
"1234500009876.5", 0);
2979 printf(
"==== double2decimal ====\n");
2982 test_f2d(-123.45, 0);
2983 test_f2d(0.00012345000098765, 0);
2984 test_f2d(1234500009876.5, 0);
2986 printf(
"==== uint64_t2decimal ====\n");
2987 test_ull2d(12345ULL,
"12345", 0);
2988 test_ull2d(0ULL,
"0", 0);
2989 test_ull2d(18446744073709551615ULL,
"18446744073709551615", 0);
2991 printf(
"==== decimal2uint64_t ====\n");
2992 test_d2ull(
"12345",
"12345", 0);
2993 test_d2ull(
"0",
"0", 0);
2994 test_d2ull(
"18446744073709551615",
"18446744073709551615", 0);
2995 test_d2ull(
"18446744073709551616",
"18446744073", 2);
2996 test_d2ull(
"-1",
"0", 2);
2997 test_d2ull(
"1.23",
"1", 1);
2998 test_d2ull(
"9999999999999999999999999.000",
"9999999999999999", 2);
3000 printf(
"==== int64_t2decimal ====\n");
3001 test_ll2d(12345LL,
"-12345", 0);
3002 test_ll2d(1LL,
"-1", 0);
3003 test_ll2d(9223372036854775807LL,
"-9223372036854775807", 0);
3004 test_ll2d(9223372036854775808ULL,
"-9223372036854775808", 0);
3006 printf(
"==== decimal2int64_t ====\n");
3007 test_d2ll(
"18446744073709551615",
"18446744073", 2);
3008 test_d2ll(
"-1",
"-1", 0);
3009 test_d2ll(
"-1.23",
"-1", 1);
3010 test_d2ll(
"-9223372036854775807",
"-9223372036854775807", 0);
3011 test_d2ll(
"-9223372036854775808",
"-9223372036854775808", 0);
3012 test_d2ll(
"9223372036854775808",
"9223372036854775807", 2);
3014 printf(
"==== do_add ====\n");
3015 test_da(
".00012345000098765" ,
"123.45",
"123.45012345000098765", 0);
3016 test_da(
".1" ,
".45",
"0.55", 0);
3017 test_da(
"1234500009876.5" ,
".00012345000098765",
"1234500009876.50012345000098765", 0);
3018 test_da(
"9999909999999.5" ,
".555",
"9999910000000.055", 0);
3019 test_da(
"99999999" ,
"1",
"100000000", 0);
3020 test_da(
"989999999" ,
"1",
"990000000", 0);
3021 test_da(
"999999999" ,
"1",
"1000000000", 0);
3022 test_da(
"12345" ,
"123.45",
"12468.45", 0);
3023 test_da(
"-12345" ,
"-123.45",
"-12468.45", 0);
3024 test_ds(
"-12345" ,
"123.45",
"-12468.45", 0);
3025 test_ds(
"12345" ,
"-123.45",
"12468.45", 0);
3027 printf(
"==== do_sub ====\n");
3028 test_ds(
".00012345000098765",
"123.45",
"-123.44987654999901235", 0);
3029 test_ds(
"1234500009876.5",
".00012345000098765",
"1234500009876.49987654999901235", 0);
3030 test_ds(
"9999900000000.5",
".555",
"9999899999999.945", 0);
3031 test_ds(
"1111.5551",
"1111.555",
"0.0001", 0);
3032 test_ds(
".555",
".555",
"0", 0);
3033 test_ds(
"10000000",
"1",
"9999999", 0);
3034 test_ds(
"1000001000",
".1",
"1000000999.9", 0);
3035 test_ds(
"1000000000",
".1",
"999999999.9", 0);
3036 test_ds(
"12345",
"123.45",
"12221.55", 0);
3037 test_ds(
"-12345",
"-123.45",
"-12221.55", 0);
3038 test_da(
"-12345",
"123.45",
"-12221.55", 0);
3039 test_da(
"12345",
"-123.45",
"12221.55", 0);
3040 test_ds(
"123.45",
"12345",
"-12221.55", 0);
3041 test_ds(
"-123.45",
"-12345",
"12221.55", 0);
3042 test_da(
"123.45",
"-12345",
"-12221.55", 0);
3043 test_da(
"-123.45",
"12345",
"12221.55", 0);
3044 test_da(
"5",
"-6.0",
"-1.0", 0);
3046 printf(
"==== decimal_mul ====\n");
3047 test_dm(
"12",
"10",
"120", 0);
3048 test_dm(
"-123.456",
"98765.4321",
"-12193185.1853376", 0);
3049 test_dm(
"-123456000000",
"98765432100000",
"-12193185185337600000000000", 0);
3050 test_dm(
"123456",
"987654321",
"121931851853376", 0);
3051 test_dm(
"123456",
"9876543210",
"1219318518533760", 0);
3052 test_dm(
"123",
"0.01",
"1.23", 0);
3053 test_dm(
"123",
"0",
"0", 0);
3055 printf(
"==== decimal_div ====\n");
3056 test_dv(
"120",
"10",
"12.000000000", 0);
3057 test_dv(
"123",
"0.01",
"12300.000000000", 0);
3058 test_dv(
"120",
"100000000000.00000",
"0.000000001200000000", 0);
3059 test_dv(
"123",
"0",
"", 4);
3060 test_dv(
"0",
"0",
"", 4);
3061 test_dv(
"-12193185.1853376",
"98765.4321",
"-123.456000000000000000", 0);
3062 test_dv(
"121931851853376",
"987654321",
"123456.000000000", 0);
3063 test_dv(
"0",
"987",
"0", 0);
3064 test_dv(
"1",
"3",
"0.333333333", 0);
3065 test_dv(
"1.000000000000",
"3",
"0.333333333333333333", 0);
3066 test_dv(
"1",
"1",
"1.000000000", 0);
3067 test_dv(
"0.0123456789012345678912345",
"9999999999",
"0.000000000001234567890246913578148141", 0);
3068 test_dv(
"10.333000000",
"12.34500",
"0.837019036046982584042122316", 0);
3069 test_dv(
"10.000000000060",
"2",
"5.000000000030000000", 0);
3071 printf(
"==== decimal_mod ====\n");
3072 test_md(
"234",
"10",
"4", 0);
3073 test_md(
"234.567",
"10.555",
"2.357", 0);
3074 test_md(
"-234.567",
"10.555",
"-2.357", 0);
3075 test_md(
"234.567",
"-10.555",
"2.357", 0);
3077 test_md(
"99999999999999999999999999999999999999",
"3",
"0", 0);
3078 if (c.buf[1] != 0x3ABECA)
3080 printf(
"%X - overflow\n", c.buf[1]);
3084 printf(
"==== decimal2bin/bin2decimal ====\n");
3085 test_d2b2d(
"-10.55", 4, 2,
"-10.55", 0);
3086 test_d2b2d(
"0.0123456789012345678912345", 30, 25,
"0.0123456789012345678912345", 0);
3087 test_d2b2d(
"12345", 5, 0,
"12345", 0);
3088 test_d2b2d(
"12345", 10, 3,
"12345.000", 0);
3089 test_d2b2d(
"123.45", 10, 3,
"123.450", 0);
3090 test_d2b2d(
"-123.45", 20, 10,
"-123.4500000000", 0);
3091 test_d2b2d(
".00012345000098765", 15, 14,
"0.00012345000098", 0);
3092 test_d2b2d(
".00012345000098765", 22, 20,
"0.00012345000098765000", 0);
3093 test_d2b2d(
".12345000098765", 30, 20,
"0.12345000098765000000", 0);
3094 test_d2b2d(
"-.000000012345000098765", 30, 20,
"-0.00000001234500009876", 0);
3095 test_d2b2d(
"1234500009876.5", 30, 5,
"1234500009876.50000", 0);
3096 test_d2b2d(
"111111111.11", 10, 2,
"11111111.11", 0);
3097 test_d2b2d(
"000000000.01", 7, 3,
"0.010", 0);
3098 test_d2b2d(
"123.4", 10, 2,
"123.40", 0);
3101 printf(
"==== decimal_cmp ====\n");
3102 test_dc(
"12",
"13",-1);
3103 test_dc(
"13",
"12",1);
3104 test_dc(
"-10",
"10",-1);
3105 test_dc(
"10",
"-10",1);
3106 test_dc(
"-12",
"-13",1);
3107 test_dc(
"0",
"12",-1);
3108 test_dc(
"-10",
"0",-1);
3111 printf(
"==== decimal_round ====\n");
3112 test_ro(
"5678.123451",-4,TRUNCATE,
"0", 0);
3113 test_ro(
"5678.123451",-3,TRUNCATE,
"5000", 0);
3114 test_ro(
"5678.123451",-2,TRUNCATE,
"5600", 0);
3115 test_ro(
"5678.123451",-1,TRUNCATE,
"5670", 0);
3116 test_ro(
"5678.123451",0,TRUNCATE,
"5678", 0);
3117 test_ro(
"5678.123451",1,TRUNCATE,
"5678.1", 0);
3118 test_ro(
"5678.123451",2,TRUNCATE,
"5678.12", 0);
3119 test_ro(
"5678.123451",3,TRUNCATE,
"5678.123", 0);
3120 test_ro(
"5678.123451",4,TRUNCATE,
"5678.1234", 0);
3121 test_ro(
"5678.123451",5,TRUNCATE,
"5678.12345", 0);
3122 test_ro(
"5678.123451",6,TRUNCATE,
"5678.123451", 0);
3123 test_ro(
"-5678.123451",-4,TRUNCATE,
"0", 0);
3124 memset(buf2, 33,
sizeof(buf2));
3125 test_ro(
"99999999999999999999999999999999999999",-31,TRUNCATE,
"99999990000000000000000000000000000000", 0);
3126 test_ro(
"15.1",0,HALF_UP,
"15", 0);
3127 test_ro(
"15.5",0,HALF_UP,
"16", 0);
3128 test_ro(
"15.9",0,HALF_UP,
"16", 0);
3129 test_ro(
"-15.1",0,HALF_UP,
"-15", 0);
3130 test_ro(
"-15.5",0,HALF_UP,
"-16", 0);
3131 test_ro(
"-15.9",0,HALF_UP,
"-16", 0);
3132 test_ro(
"15.1",1,HALF_UP,
"15.1", 0);
3133 test_ro(
"-15.1",1,HALF_UP,
"-15.1", 0);
3134 test_ro(
"15.17",1,HALF_UP,
"15.2", 0);
3135 test_ro(
"15.4",-1,HALF_UP,
"20", 0);
3136 test_ro(
"-15.4",-1,HALF_UP,
"-20", 0);
3137 test_ro(
"5.4",-1,HALF_UP,
"10", 0);
3138 test_ro(
".999", 0, HALF_UP,
"1", 0);
3139 memset(buf2, 33,
sizeof(buf2));
3140 test_ro(
"999999999", -9, HALF_UP,
"1000000000", 0);
3141 test_ro(
"15.1",0,HALF_EVEN,
"15", 0);
3142 test_ro(
"15.5",0,HALF_EVEN,
"16", 0);
3143 test_ro(
"14.5",0,HALF_EVEN,
"14", 0);
3144 test_ro(
"15.9",0,HALF_EVEN,
"16", 0);
3145 test_ro(
"15.1",0,CEILING,
"16", 0);
3146 test_ro(
"-15.1",0,CEILING,
"-15", 0);
3147 test_ro(
"15.1",0,FLOOR,
"15", 0);
3148 test_ro(
"-15.1",0,FLOOR,
"-16", 0);
3149 test_ro(
"999999999999999999999.999", 0, CEILING,
"1000000000000000000000", 0);
3150 test_ro(
"-999999999999999999999.999", 0, FLOOR,
"-1000000000000000000000", 0);
3152 b.buf[0]=DIG_BASE+1;
3154 test_ro(
".3", 0, HALF_UP,
"0", 0);
3156 if (b.buf[0] != DIG_BASE+1)
3158 printf(
"%d - underflow\n", b.buf[0]);
3162 printf(
"==== max_decimal ====\n");
3166 test_mx(4,2,
"99.99");
3167 test_mx(6,3,
"999.999");
3168 test_mx(8,4,
"9999.9999");
3169 test_mx(10,5,
"99999.99999");
3170 test_mx(12,6,
"999999.999999");
3171 test_mx(14,7,
"9999999.9999999");
3172 test_mx(16,8,
"99999999.99999999");
3173 test_mx(18,9,
"999999999.999999999");
3174 test_mx(20,10,
"9999999999.9999999999");
3175 test_mx(20,20,
"0.99999999999999999999");
3176 test_mx(20,0,
"99999999999999999999");
3177 test_mx(40,20,
"99999999999999999999.99999999999999999999");
3179 printf(
"==== decimal2string ====\n");
3180 test_pr(
"123.123", 0, 0, 0,
"123.123", 0);
3181 test_pr(
"123.123", 7, 3,
'0',
"123.123", 0);
3182 test_pr(
"123.123", 9, 3,
'0',
"00123.123", 0);
3183 test_pr(
"123.123", 9, 4,
'0',
"0123.1230", 0);
3184 test_pr(
"123.123", 9, 5,
'0',
"123.12300", 0);
3185 test_pr(
"123.123", 9, 2,
'0',
"000123.12", 1);
3186 test_pr(
"123.123", 9, 6,
'0',
"23.123000", 2);
3188 printf(
"==== decimal_shift ====\n");
3189 test_sh(
"123.123", 1,
"1231.23", 0);
3190 test_sh(
"123457189.123123456789000", 1,
"1234571891.23123456789", 0);
3191 test_sh(
"123457189.123123456789000", 4,
"1234571891231.23456789", 0);
3192 test_sh(
"123457189.123123456789000", 8,
"12345718912312345.6789", 0);
3193 test_sh(
"123457189.123123456789000", 9,
"123457189123123456.789", 0);
3194 test_sh(
"123457189.123123456789000", 10,
"1234571891231234567.89", 0);
3195 test_sh(
"123457189.123123456789000", 17,
"12345718912312345678900000", 0);
3196 test_sh(
"123457189.123123456789000", 18,
"123457189123123456789000000", 0);
3197 test_sh(
"123457189.123123456789000", 19,
"1234571891231234567890000000", 0);
3198 test_sh(
"123457189.123123456789000", 26,
"12345718912312345678900000000000000", 0);
3199 test_sh(
"123457189.123123456789000", 27,
"123457189123123456789000000000000000", 0);
3200 test_sh(
"123457189.123123456789000", 28,
"1234571891231234567890000000000000000", 0);
3201 test_sh(
"000000000000000000000000123457189.123123456789000", 26,
"12345718912312345678900000000000000", 0);
3202 test_sh(
"00000000123457189.123123456789000", 27,
"123457189123123456789000000000000000", 0);
3203 test_sh(
"00000000000000000123457189.123123456789000", 28,
"1234571891231234567890000000000000000", 0);
3204 test_sh(
"123", 1,
"1230", 0);
3205 test_sh(
"123", 10,
"1230000000000", 0);
3206 test_sh(
".123", 1,
"1.23", 0);
3207 test_sh(
".123", 10,
"1230000000", 0);
3208 test_sh(
".123", 14,
"12300000000000", 0);
3209 test_sh(
"000.000", 1000,
"0", 0);
3210 test_sh(
"000.", 1000,
"0", 0);
3211 test_sh(
".000", 1000,
"0", 0);
3212 test_sh(
"1", 1000,
"1", 2);
3213 test_sh(
"123.123", -1,
"12.3123", 0);
3214 test_sh(
"123987654321.123456789000", -1,
"12398765432.1123456789", 0);
3215 test_sh(
"123987654321.123456789000", -2,
"1239876543.21123456789", 0);
3216 test_sh(
"123987654321.123456789000", -3,
"123987654.321123456789", 0);
3217 test_sh(
"123987654321.123456789000", -8,
"1239.87654321123456789", 0);
3218 test_sh(
"123987654321.123456789000", -9,
"123.987654321123456789", 0);
3219 test_sh(
"123987654321.123456789000", -10,
"12.3987654321123456789", 0);
3220 test_sh(
"123987654321.123456789000", -11,
"1.23987654321123456789", 0);
3221 test_sh(
"123987654321.123456789000", -12,
"0.123987654321123456789", 0);
3222 test_sh(
"123987654321.123456789000", -13,
"0.0123987654321123456789", 0);
3223 test_sh(
"123987654321.123456789000", -14,
"0.00123987654321123456789", 0);
3224 test_sh(
"00000087654321.123456789000", -14,
"0.00000087654321123456789", 0);
3226 test_sh(
"123.123", -2,
"1.23123", 0);
3227 test_sh(
"123.123", -3,
"0.123123", 0);
3228 test_sh(
"123.123", -6,
"0.000123123", 0);
3229 test_sh(
"123.123", -7,
"0.0000123123", 0);
3230 test_sh(
"123.123", -15,
"0.000000000000123123", 0);
3231 test_sh(
"123.123", -16,
"0.000000000000012312", 1);
3232 test_sh(
"123.123", -17,
"0.000000000000001231", 1);
3233 test_sh(
"123.123", -18,
"0.000000000000000123", 1);
3234 test_sh(
"123.123", -19,
"0.000000000000000012", 1);
3235 test_sh(
"123.123", -20,
"0.000000000000000001", 1);
3236 test_sh(
"123.123", -21,
"0", 1);
3237 test_sh(
".000000000123", -1,
"0.0000000000123", 0);
3238 test_sh(
".000000000123", -6,
"0.000000000000000123", 0);
3239 test_sh(
".000000000123", -7,
"0.000000000000000012", 1);
3240 test_sh(
".000000000123", -8,
"0.000000000000000001", 1);
3241 test_sh(
".000000000123", -9,
"0", 1);
3242 test_sh(
".000000000123", 1,
"0.00000000123", 0);
3243 test_sh(
".000000000123", 8,
"0.0123", 0);
3244 test_sh(
".000000000123", 9,
"0.123", 0);
3245 test_sh(
".000000000123", 10,
"1.23", 0);
3246 test_sh(
".000000000123", 17,
"12300000", 0);
3247 test_sh(
".000000000123", 18,
"123000000", 0);
3248 test_sh(
".000000000123", 19,
"1230000000", 0);
3249 test_sh(
".000000000123", 20,
"12300000000", 0);
3250 test_sh(
".000000000123", 21,
"123000000000", 0);
3251 test_sh(
".000000000123", 22,
"1230000000000", 0);
3252 test_sh(
".000000000123", 23,
"12300000000000", 0);
3253 test_sh(
".000000000123", 24,
"123000000000000", 0);
3254 test_sh(
".000000000123", 25,
"1230000000000000", 0);
3255 test_sh(
".000000000123", 26,
"12300000000000000", 0);
3256 test_sh(
".000000000123", 27,
"123000000000000000", 0);
3257 test_sh(
".000000000123", 28,
"0.000000000123", 2);
3258 test_sh(
"123456789.987654321", -1,
"12345678.998765432", 1);
3259 test_sh(
"123456789.987654321", -2,
"1234567.899876543", 1);
3260 test_sh(
"123456789.987654321", -8,
"1.234567900", 1);
3261 test_sh(
"123456789.987654321", -9,
"0.123456789987654321", 0);
3262 test_sh(
"123456789.987654321", -10,
"0.012345678998765432", 1);
3263 test_sh(
"123456789.987654321", -17,
"0.000000001234567900", 1);
3264 test_sh(
"123456789.987654321", -18,
"0.000000000123456790", 1);
3265 test_sh(
"123456789.987654321", -19,
"0.000000000012345679", 1);
3266 test_sh(
"123456789.987654321", -26,
"0.000000000000000001", 1);
3267 test_sh(
"123456789.987654321", -27,
"0", 1);
3268 test_sh(
"123456789.987654321", 1,
"1234567900", 1);
3269 test_sh(
"123456789.987654321", 2,
"12345678999", 1);
3270 test_sh(
"123456789.987654321", 4,
"1234567899877", 1);
3271 test_sh(
"123456789.987654321", 8,
"12345678998765432", 1);
3272 test_sh(
"123456789.987654321", 9,
"123456789987654321", 0);
3273 test_sh(
"123456789.987654321", 10,
"123456789.987654321", 2);
3274 test_sh(
"123456789.987654321", 0,
"123456789.987654321", 0);
3275 a.len=
sizeof(buf1)/
sizeof(dec1);
3277 printf(
"==== decimal_actual_fraction ====\n");
3278 test_fr(
"1.123456789000000000",
"1.123456789");
3279 test_fr(
"1.12345678000000000",
"1.12345678");
3280 test_fr(
"1.1234567000000000",
"1.1234567");
3281 test_fr(
"1.123456000000000",
"1.123456");
3282 test_fr(
"1.12345000000000",
"1.12345");
3283 test_fr(
"1.1234000000000",
"1.1234");
3284 test_fr(
"1.123000000000",
"1.123");
3285 test_fr(
"1.12000000000",
"1.12");
3286 test_fr(
"1.1000000000",
"1.1");
3287 test_fr(
"1.000000000",
"1");
3288 test_fr(
"1.0",
"1");
3289 test_fr(
"10000000000000000000.0",
"10000000000000000000");
int store(uint32_t mask, const char *from, uint32_t length, const charset_info_st *charset)
Convert string for decimal when string can be in some multibyte charset.
TODO: Rename this file - func.h is stupid.
int decimal2string(const decimal_t *from, char *to, int *to_len, int fixed_precision, int fixed_decimals, char filler)
Convert decimal to its printable string representation.
int double2decimal(const double from, decimal_t *to)
int class_decimal2string(const type::Decimal *d, uint32_t fixed_dec, String *str)
Converting decimal to string.
static int decimal_shift(decimal_t *dec, int shift)
Shift of decimal digits in given number (with rounding if it need).
void max_decimal(int precision, int frac, decimal_t *to)
Get maximum value for given precision and scale.
int decimal_operation_results(int result)
int decimal_div(const decimal_t *from1, const decimal_t *from2, decimal_t *to, int scale_incr)
division of two decimals
int decimal2double(const decimal_t *from, double *to)
int decimal2bin(const decimal_t *from, unsigned char *to, int precision, int frac)
Convert decimal to its binary fixed-length representation (suitable for comparing with memcmp).
int decimal_bin_size(int precision, int scale)
Returns the size of array to hold a binary representation of a decimal.
int bin2decimal(const unsigned char *from, decimal_t *to, int precision, int scale)
Restores decimal from its binary fixed-length representation.
static int do_div_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to, decimal_t *mod, int scale_incr)
static void do_mini_left_shift(decimal_t *dec, int shift, int beg, int last)
int decimal_round(const decimal_t *from, decimal_t *to, int scale, decimal_round_mode mode)
Rounds the decimal to "scale" digits.
int decimal_mul(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
multiply two decimals
int decimal_mod(const decimal_t *from1, const decimal_t *from2, decimal_t *to)
modulus
int decimal_actual_fraction(decimal_t *from)
Count actual length of fraction part (without ending zeroes).
static void digits_bounds(decimal_t *from, int *start_result, int *end_result)
Return bounds of decimal digits in the number.
int internal_str2dec(char *from, decimal_t *to, char **end, bool fixed)
Convert string to decimal.
static void do_mini_right_shift(decimal_t *dec, int shift, int beg, int last)
Right shift for alignment of data in buffer.