Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Postgres FD Implementation
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
Abuhujair Javed
Postgres FD Implementation
Commits
5e7c3d91
Commit
5e7c3d91
authored
Jul 03, 2015
by
Tom Lane
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add documentation and regression tests concerning rounding of numerics.
Michael Paquier, reviewed by Fabien Coelho
parent
8eb6407a
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
169 additions
and
0 deletions
+169
-0
doc/src/sgml/datatype.sgml
doc/src/sgml/datatype.sgml
+25
-0
src/test/regress/expected/int2.out
src/test/regress/expected/int2.out
+20
-0
src/test/regress/expected/int4.out
src/test/regress/expected/int4.out
+20
-0
src/test/regress/expected/int8-exp-three-digits.out
src/test/regress/expected/int8-exp-three-digits.out
+20
-0
src/test/regress/expected/int8.out
src/test/regress/expected/int8.out
+20
-0
src/test/regress/expected/numeric.out
src/test/regress/expected/numeric.out
+24
-0
src/test/regress/sql/int2.sql
src/test/regress/sql/int2.sql
+10
-0
src/test/regress/sql/int4.sql
src/test/regress/sql/int4.sql
+10
-0
src/test/regress/sql/int8.sql
src/test/regress/sql/int8.sql
+10
-0
src/test/regress/sql/numeric.sql
src/test/regress/sql/numeric.sql
+10
-0
No files found.
doc/src/sgml/datatype.sgml
View file @
5e7c3d91
...
...
@@ -612,6 +612,31 @@ NUMERIC
equivalent. Both types are part of the <acronym>SQL</acronym>
standard.
</para>
<para>
When rounding values, the <type>numeric</type> type rounds ties away
from zero, while (on most machines) the <type>real</type>
and <type>double precision</type> types round ties to the nearest even
number. For example:
<programlisting>
SELECT x,
round(x::numeric) AS num_round,
round(x::double precision) AS dbl_round
FROM generate_series(-3.5, 3.5, 1) as x;
x | num_round | dbl_round
------+-----------+-----------
-3.5 | -4 | -4
-2.5 | -3 | -2
-1.5 | -2 | -2
-0.5 | -1 | -0
0.5 | 1 | 0
1.5 | 2 | 2
2.5 | 3 | 2
3.5 | 4 | 4
(8 rows)
</programlisting>
</para>
</sect2>
...
...
src/test/regress/expected/int2.out
View file @
5e7c3d91
...
...
@@ -286,3 +286,23 @@ FROM (VALUES (-2.5::float8),
2.5 | 2
(7 rows)
-- check rounding when casting from numeric
SELECT x, x::int2 AS int2_value
FROM (VALUES (-2.5::numeric),
(-1.5::numeric),
(-0.5::numeric),
(0.0::numeric),
(0.5::numeric),
(1.5::numeric),
(2.5::numeric)) t(x);
x | int2_value
------+------------
-2.5 | -3
-1.5 | -2
-0.5 | -1
0.0 | 0
0.5 | 1
1.5 | 2
2.5 | 3
(7 rows)
src/test/regress/expected/int4.out
View file @
5e7c3d91
...
...
@@ -383,3 +383,23 @@ FROM (VALUES (-2.5::float8),
2.5 | 2
(7 rows)
-- check rounding when casting from numeric
SELECT x, x::int4 AS int4_value
FROM (VALUES (-2.5::numeric),
(-1.5::numeric),
(-0.5::numeric),
(0.0::numeric),
(0.5::numeric),
(1.5::numeric),
(2.5::numeric)) t(x);
x | int4_value
------+------------
-2.5 | -3
-1.5 | -2
-0.5 | -1
0.0 | 0
0.5 | 1
1.5 | 2
2.5 | 3
(7 rows)
src/test/regress/expected/int8-exp-three-digits.out
View file @
5e7c3d91
...
...
@@ -866,3 +866,23 @@ FROM (VALUES (-2.5::float8),
2.5 | 2
(7 rows)
-- check rounding when casting from numeric
SELECT x, x::int8 AS int8_value
FROM (VALUES (-2.5::numeric),
(-1.5::numeric),
(-0.5::numeric),
(0.0::numeric),
(0.5::numeric),
(1.5::numeric),
(2.5::numeric)) t(x);
x | int8_value
------+------------
-2.5 | -3
-1.5 | -2
-0.5 | -1
0.0 | 0
0.5 | 1
1.5 | 2
2.5 | 3
(7 rows)
src/test/regress/expected/int8.out
View file @
5e7c3d91
...
...
@@ -866,3 +866,23 @@ FROM (VALUES (-2.5::float8),
2.5 | 2
(7 rows)
-- check rounding when casting from numeric
SELECT x, x::int8 AS int8_value
FROM (VALUES (-2.5::numeric),
(-1.5::numeric),
(-0.5::numeric),
(0.0::numeric),
(0.5::numeric),
(1.5::numeric),
(2.5::numeric)) t(x);
x | int8_value
------+------------
-2.5 | -3
-1.5 | -2
-0.5 | -1
0.0 | 0
0.5 | 1
1.5 | 2
2.5 | 3
(7 rows)
src/test/regress/expected/numeric.out
View file @
5e7c3d91
...
...
@@ -730,6 +730,30 @@ SELECT a, ceil(a), ceiling(a), floor(a), round(a) FROM ceil_floor_round;
(7 rows)
DROP TABLE ceil_floor_round;
-- Check rounding, it should round ties away from zero.
SELECT i as pow,
round((-2.5 * 10 ^ i)::numeric, -i),
round((-1.5 * 10 ^ i)::numeric, -i),
round((-0.5 * 10 ^ i)::numeric, -i),
round((0.5 * 10 ^ i)::numeric, -i),
round((1.5 * 10 ^ i)::numeric, -i),
round((2.5 * 10 ^ i)::numeric, -i)
FROM generate_series(-5,5) AS t(i);
pow | round | round | round | round | round | round
-----+----------+----------+----------+---------+---------+---------
-5 | -0.00003 | -0.00002 | -0.00001 | 0.00001 | 0.00002 | 0.00003
-4 | -0.0003 | -0.0002 | -0.0001 | 0.0001 | 0.0002 | 0.0003
-3 | -0.003 | -0.002 | -0.001 | 0.001 | 0.002 | 0.003
-2 | -0.03 | -0.02 | -0.01 | 0.01 | 0.02 | 0.03
-1 | -0.3 | -0.2 | -0.1 | 0.1 | 0.2 | 0.3
0 | -3 | -2 | -1 | 1 | 2 | 3
1 | -30 | -20 | -10 | 10 | 20 | 30
2 | -300 | -200 | -100 | 100 | 200 | 300
3 | -3000 | -2000 | -1000 | 1000 | 2000 | 3000
4 | -30000 | -20000 | -10000 | 10000 | 20000 | 30000
5 | -300000 | -200000 | -100000 | 100000 | 200000 | 300000
(11 rows)
-- Testing for width_bucket(). For convenience, we test both the
-- numeric and float8 versions of the function in this file.
-- errors
...
...
src/test/regress/sql/int2.sql
View file @
5e7c3d91
...
...
@@ -102,3 +102,13 @@ FROM (VALUES (-2.5::float8),
(
0
.
5
::
float8
),
(
1
.
5
::
float8
),
(
2
.
5
::
float8
))
t
(
x
);
-- check rounding when casting from numeric
SELECT
x
,
x
::
int2
AS
int2_value
FROM
(
VALUES
(
-
2
.
5
::
numeric
),
(
-
1
.
5
::
numeric
),
(
-
0
.
5
::
numeric
),
(
0
.
0
::
numeric
),
(
0
.
5
::
numeric
),
(
1
.
5
::
numeric
),
(
2
.
5
::
numeric
))
t
(
x
);
src/test/regress/sql/int4.sql
View file @
5e7c3d91
...
...
@@ -145,3 +145,13 @@ FROM (VALUES (-2.5::float8),
(
0
.
5
::
float8
),
(
1
.
5
::
float8
),
(
2
.
5
::
float8
))
t
(
x
);
-- check rounding when casting from numeric
SELECT
x
,
x
::
int4
AS
int4_value
FROM
(
VALUES
(
-
2
.
5
::
numeric
),
(
-
1
.
5
::
numeric
),
(
-
0
.
5
::
numeric
),
(
0
.
0
::
numeric
),
(
0
.
5
::
numeric
),
(
1
.
5
::
numeric
),
(
2
.
5
::
numeric
))
t
(
x
);
src/test/regress/sql/int8.sql
View file @
5e7c3d91
...
...
@@ -215,3 +215,13 @@ FROM (VALUES (-2.5::float8),
(
0
.
5
::
float8
),
(
1
.
5
::
float8
),
(
2
.
5
::
float8
))
t
(
x
);
-- check rounding when casting from numeric
SELECT
x
,
x
::
int8
AS
int8_value
FROM
(
VALUES
(
-
2
.
5
::
numeric
),
(
-
1
.
5
::
numeric
),
(
-
0
.
5
::
numeric
),
(
0
.
0
::
numeric
),
(
0
.
5
::
numeric
),
(
1
.
5
::
numeric
),
(
2
.
5
::
numeric
))
t
(
x
);
src/test/regress/sql/numeric.sql
View file @
5e7c3d91
...
...
@@ -667,6 +667,16 @@ INSERT INTO ceil_floor_round VALUES ('-0.000001');
SELECT
a
,
ceil
(
a
),
ceiling
(
a
),
floor
(
a
),
round
(
a
)
FROM
ceil_floor_round
;
DROP
TABLE
ceil_floor_round
;
-- Check rounding, it should round ties away from zero.
SELECT
i
as
pow
,
round
((
-
2
.
5
*
10
^
i
)::
numeric
,
-
i
),
round
((
-
1
.
5
*
10
^
i
)::
numeric
,
-
i
),
round
((
-
0
.
5
*
10
^
i
)::
numeric
,
-
i
),
round
((
0
.
5
*
10
^
i
)::
numeric
,
-
i
),
round
((
1
.
5
*
10
^
i
)::
numeric
,
-
i
),
round
((
2
.
5
*
10
^
i
)::
numeric
,
-
i
)
FROM
generate_series
(
-
5
,
5
)
AS
t
(
i
);
-- Testing for width_bucket(). For convenience, we test both the
-- numeric and float8 versions of the function in this file.
...
...
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