Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mariadb
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
mariadb
Commits
c593ab8e
Commit
c593ab8e
authored
Oct 20, 2004
by
serg@serg.mylan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
decimal_cmp()
parent
30947b7b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
3 deletions
+38
-3
include/decimal.h
include/decimal.h
+1
-0
strings/decimal.c
strings/decimal.c
+37
-3
No files found.
include/decimal.h
View file @
c593ab8e
...
...
@@ -45,6 +45,7 @@ int decimal_result_size(decimal *from1, decimal *from2, char op, int param);
int
decimal_add
(
decimal
*
from1
,
decimal
*
from2
,
decimal
*
to
);
int
decimal_sub
(
decimal
*
from1
,
decimal
*
from2
,
decimal
*
to
);
int
decimal_cmp
(
decimal
*
from1
,
decimal
*
from2
);
int
decimal_mul
(
decimal
*
from1
,
decimal
*
from2
,
decimal
*
to
);
int
decimal_div
(
decimal
*
from1
,
decimal
*
from2
,
decimal
*
to
,
int
scale_incr
);
int
decimal_mod
(
decimal
*
from1
,
decimal
*
from2
,
decimal
*
to
);
...
...
strings/decimal.c
View file @
c593ab8e
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment