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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
MariaDB
Commits
c41f992d
Commit
c41f992d
authored
Nov 03, 2004
by
unknown
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
error codes chaged to bitmap (for easier testing with masks)
two more round() modes - CEILING and FLOOR
parent
09e05035
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
50 deletions
+65
-50
include/decimal.h
include/decimal.h
+7
-4
strings/decimal.c
strings/decimal.c
+58
-46
No files found.
include/decimal.h
View file @
c41f992d
...
...
@@ -19,7 +19,7 @@
#include <my_global.h>
typedef
enum
{
TRUNCATE
=
0
,
HALF_EVEN
,
HALF_UP
}
decimal_round_mode
;
typedef
enum
{
TRUNCATE
=
0
,
HALF_EVEN
,
HALF_UP
,
CEILING
,
FLOOR
}
decimal_round_mode
;
typedef
int32
decimal_digit
;
typedef
struct
st_decimal
{
...
...
@@ -91,9 +91,12 @@ int decimal_round(decimal *from, decimal *to, int new_scale, decimal_round_mode
#define E_DEC_OK 0
#define E_DEC_TRUNCATED 1
#define E_DEC_OVERFLOW 2
#define E_DEC_DIV_ZERO 3
#define E_DEC_BAD_NUM 4
#define E_DEC_OOM 5
#define E_DEC_DIV_ZERO 4
#define E_DEC_BAD_NUM 8
#define E_DEC_OOM 16
#define E_DEC_ERROR 31
#define E_DEC_FATAL_ERROR 30
#endif
strings/decimal.c
View file @
c41f992d
...
...
@@ -819,12 +819,21 @@ int decimal_bin_size(int precision, int scale)
int
decimal_round
(
decimal
*
from
,
decimal
*
to
,
int
scale
,
decimal_round_mode
mode
)
{
int
frac0
=
scale
>
0
?
ROUND_UP
(
scale
)
:
scale
/
DIG_PER_DEC1
,
frac1
=
ROUND_UP
(
from
->
frac
),
frac1
=
ROUND_UP
(
from
->
frac
),
round_digit
,
intg0
=
ROUND_UP
(
from
->
intg
),
error
=
E_DEC_OK
,
len
=
to
->
len
;
dec1
*
buf0
=
from
->
buf
,
*
buf1
=
to
->
buf
,
x
,
y
,
carry
=
0
;
sanity
(
to
);
switch
(
mode
)
{
case
HALF_UP
:
case
HALF_EVEN
:
round_digit
=
5
;
break
;
case
CEILING
:
round_digit
=
from
->
sign
?
10
:
0
;
break
;
case
FLOOR
:
round_digit
=
from
->
sign
?
0
:
10
;
break
;
case
TRUNCATE
:
round_digit
=
10
;
break
;
default:
DBUG_ASSERT
(
0
);
}
if
(
unlikely
(
frac0
+
intg0
>
len
))
{
frac0
=
len
-
intg0
;
...
...
@@ -865,26 +874,20 @@ int decimal_round(decimal *from, decimal *to, int scale, decimal_round_mode mode
buf1
+=
intg0
+
frac0
-
1
;
if
(
scale
==
frac0
*
DIG_PER_DEC1
)
{
if
(
mode
!=
TRUNCATE
)
{
x
=
buf0
[
1
]
/
DIG_MASK
;
if
(
x
>
5
||
(
x
==
5
&&
(
mode
==
HALF_UP
||
*
buf0
&
1
)))
(
*
buf1
)
++
;
}
x
=
buf0
[
1
]
/
DIG_MASK
;
if
(
x
>
round_digit
||
(
round_digit
==
5
&&
x
==
5
&&
(
mode
==
HALF_UP
||
*
buf0
&
1
)))
(
*
buf1
)
++
;
}
else
{
int
pos
=
frac0
*
DIG_PER_DEC1
-
scale
-
1
;
if
(
mode
!=
TRUNCATE
)
{
x
=*
buf1
/
powers10
[
pos
];
y
=
x
%
10
;
if
(
y
>
5
||
(
y
==
5
&&
(
mode
==
HALF_UP
||
(
x
/
10
)
&
1
)))
x
+=
10
;
*
buf1
=
powers10
[
pos
]
*
(
x
-
y
);
}
else
*
buf1
=
(
*
buf1
/
powers10
[
pos
+
1
])
*
powers10
[
pos
+
1
];
x
=*
buf1
/
powers10
[
pos
];
y
=
x
%
10
;
if
(
y
>
round_digit
||
(
round_digit
==
5
&&
y
==
5
&&
(
mode
==
HALF_UP
||
(
x
/
10
)
&
1
)))
x
+=
10
;
*
buf1
=
powers10
[
pos
]
*
(
x
-
y
);
}
if
(
*
buf1
>=
DIG_BASE
)
{
...
...
@@ -1820,12 +1823,13 @@ void test_md(char *s1, char *s2)
printf
(
"
\n
"
);
}
char
*
round_mode
[]
=
{
"TRUNCATE"
,
"HALF_EVEN"
,
"HALF_UP"
,
"CEILING"
,
"FLOOR"
};
void
test_ro
(
char
*
s1
,
int
n
,
decimal_round_mode
mode
)
{
char
s
[
100
];
int
res
;
sprintf
(
s
,
"%s('%s', %d)"
,
(
mode
==
TRUNCATE
?
"truncate"
:
"round"
),
s1
,
n
);
sprintf
(
s
,
"'%s', %d, %s"
,
s1
,
n
,
round_mode
[
mode
]);
string2decimal
(
s1
,
&
a
,
0
);
res
=
decimal_round
(
&
a
,
&
b
,
n
,
mode
);
printf
(
"%-40s => res=%d "
,
s
,
res
);
...
...
@@ -1953,33 +1957,6 @@ main()
test_dv
(
"1"
,
"1"
);
test_dv
(
"0.0123456789012345678912345"
,
"9999999999"
);
printf
(
"==== decimal_round ====
\n
"
);
test_ro
(
"15.1"
,
0
,
HALF_UP
);
test_ro
(
"15.5"
,
0
,
HALF_UP
);
test_ro
(
"15.5"
,
0
,
HALF_UP
);
test_ro
(
"15.9"
,
0
,
HALF_UP
);
test_ro
(
"-15.1"
,
0
,
HALF_UP
);
test_ro
(
"-15.5"
,
0
,
HALF_UP
);
test_ro
(
"-15.9"
,
0
,
HALF_UP
);
test_ro
(
"15.1"
,
1
,
HALF_UP
);
test_ro
(
"-15.1"
,
1
,
HALF_UP
);
test_ro
(
"15.17"
,
1
,
HALF_UP
);
test_ro
(
"15.4"
,
-
1
,
HALF_UP
);
test_ro
(
"-15.4"
,
-
1
,
HALF_UP
);
test_ro
(
"5678.123451"
,
-
4
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
-
3
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
-
2
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
-
1
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
0
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
1
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
2
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
3
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
4
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
5
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
6
,
TRUNCATE
);
test_ro
(
"-5678.123451"
,
-
4
,
TRUNCATE
);
test_ro
(
"99999999999999999999999999999999999999"
,
-
31
,
TRUNCATE
);
printf
(
"==== decimal_mod ====
\n
"
);
test_md
(
"234"
,
"10"
);
test_md
(
"234.567"
,
"10.555"
);
...
...
@@ -2008,6 +1985,41 @@ main()
test_dc
(
"0"
,
"12"
);
test_dc
(
"-10"
,
"0"
);
test_dc
(
"4"
,
"4"
);
printf
(
"==== decimal_round ====
\n
"
);
test_ro
(
"5678.123451"
,
-
4
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
-
3
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
-
2
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
-
1
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
0
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
1
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
2
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
3
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
4
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
5
,
TRUNCATE
);
test_ro
(
"5678.123451"
,
6
,
TRUNCATE
);
test_ro
(
"-5678.123451"
,
-
4
,
TRUNCATE
);
test_ro
(
"99999999999999999999999999999999999999"
,
-
31
,
TRUNCATE
);
test_ro
(
"15.1"
,
0
,
HALF_UP
);
test_ro
(
"15.5"
,
0
,
HALF_UP
);
test_ro
(
"15.9"
,
0
,
HALF_UP
);
test_ro
(
"-15.1"
,
0
,
HALF_UP
);
test_ro
(
"-15.5"
,
0
,
HALF_UP
);
test_ro
(
"-15.9"
,
0
,
HALF_UP
);
test_ro
(
"15.1"
,
1
,
HALF_UP
);
test_ro
(
"-15.1"
,
1
,
HALF_UP
);
test_ro
(
"15.17"
,
1
,
HALF_UP
);
test_ro
(
"15.4"
,
-
1
,
HALF_UP
);
test_ro
(
"-15.4"
,
-
1
,
HALF_UP
);
test_ro
(
"15.1"
,
0
,
HALF_EVEN
);
test_ro
(
"15.5"
,
0
,
HALF_EVEN
);
test_ro
(
"14.5"
,
0
,
HALF_EVEN
);
test_ro
(
"15.9"
,
0
,
HALF_EVEN
);
test_ro
(
"15.1"
,
0
,
CEILING
);
test_ro
(
"-15.1"
,
0
,
CEILING
);
test_ro
(
"15.1"
,
0
,
FLOOR
);
test_ro
(
"-15.1"
,
0
,
FLOOR
);
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