diff options
author | unknown <serg@serg.mylan> | 2004-10-20 15:26:03 +0200 |
---|---|---|
committer | unknown <serg@serg.mylan> | 2004-10-20 15:26:03 +0200 |
commit | 00bbd9a252553782627042b1a642233f5effc867 (patch) | |
tree | 16543d04931e284eba3fe1197ae779990df283f7 /strings | |
parent | 6853eb45c8085480643af94734a042b9c4c5b939 (diff) | |
download | mariadb-git-00bbd9a252553782627042b1a642233f5effc867.tar.gz |
decimal_cmp()
Diffstat (limited to 'strings')
-rw-r--r-- | strings/decimal.c | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/strings/decimal.c b/strings/decimal.c index afe5c8697d0..30dbe495d46 100644 --- a/strings/decimal.c +++ b/strings/decimal.c @@ -983,7 +983,8 @@ static int do_add(decimal *from1, decimal *from2, decimal *to) return error; } -/* to=from1-from2 */ +/* to=from1-from2. + if to==0, return -1/0/+1 - the result of the comparison */ static int do_sub(decimal *from1, decimal *from2, decimal *to) { int intg1=ROUND_UP(from1->intg), intg2=ROUND_UP(from2->intg), @@ -991,8 +992,6 @@ static int do_sub(decimal *from1, decimal *from2, decimal *to) int frac0=max(frac1, frac2), error; dec1 *buf1, *buf2, *buf0, *stop1, *stop2, *start1, *start2, carry=0; - to->sign=from1->sign; - /* let carry:=1 if from2 > from1 */ start1=buf1=from1->buf; stop1=buf1+intg1; start2=buf2=from2->buf; stop2=buf2+intg2; @@ -1026,10 +1025,18 @@ static int do_sub(decimal *from1, decimal *from2, decimal *to) carry=1; else /* short-circuit everything: from1 == from2 */ { + if (to == 0) /* decimal_cmp() */ + return 0; MAKE_ZERO(to); return E_DEC_OK; } } + + if (to == 0) /* decimal_cmp() */ + return carry == from1->sign ? 1 : -1; + + to->sign=from1->sign; + /* ensure that always from1 > from2 (and intg1 >= intg2) */ if (carry) { @@ -1109,6 +1116,13 @@ int decimal_sub(decimal *from1, decimal *from2, decimal *to) return do_add(from1, from2, to); } +int decimal_cmp(decimal *from1, decimal *from2) +{ + if (likely(from1->sign == from2->sign)) + return do_sub(from1, from2, 0); + return from1->sign > from2->sign ? -1 : 1; +} + /* multiply two decimals @@ -1685,6 +1699,17 @@ void test_ds(char *s1, char *s2) printf("\n"); } +void test_dc(char *s1, char *s2) +{ + char s[100]; + int res; + sprintf(s, "'%s' <=> '%s'", s1, s2); + string2decimal(s1, &a, 0); + string2decimal(s2, &b, 0); + res=decimal_cmp(&a, &b); + printf("%-40s => res=%d\n", s, res); +} + void test_dm(char *s1, char *s2) { char s[100]; @@ -1899,6 +1924,15 @@ main() test_d2b2d("-.000000012345000098765", 30, 20); test_d2b2d("1234500009876.5", 30, 5); + printf("==== decimal_cmp ====\n"); + test_dc("12","13"); + test_dc("13","12"); + test_dc("-10","10"); + test_dc("10","-10"); + test_dc("-12","-13"); + test_dc("0","12"); + test_dc("-10","0"); + test_dc("4","4"); return 0; } #endif |