Skip to content

Commit eafd44f

Browse files
simonat2ndQuadranttuhaihe
authored andcommitted
Update README.md
Clarify explanations
1 parent 9e5f5dd commit eafd44f

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

contrib/fixeddecimal/README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ features of PostgreSQL's builtin NUMERIC type, but with vastly increased
1111
performance. Fixeddecimal is targeted to cases where performance and disk space
1212
are a critical.
1313

14+
Just use FIXEDDECIMAL(n, 2) rather than NUMERIC(n, 2) for n=3..17
15+
1416
Often there are data storage requirements where the built in REAL and
1517
DOUBLE PRECISION types cannot be used due to the non-exact representation of
1618
numbers using these types, e.g. where monetary values need to be stored. In many
1719
of these cases NUMERIC is an almost perfect type, although with NUMERIC
1820
performance is no match for the performance of REAL or DOUBLE PRECISION, as
19-
these use CPU native processor types. FixedDecimal aims to offer performance
20-
advantages over NUMERIC without the imprecise representations that are
21-
apparent in REAL and DOUBLE PRECISION, but it comes with some caveats...
21+
these use CPU native processor types.
22+
23+
FixedDecimal delivers performance advantages over NUMERIC with full precision for
24+
addition and subtraction. Just as occurs with REAL and DOUBLE PRECISION, there
25+
are some caveats for multiplication and division.
2226

2327
Behavioural differences between FIXEDDECIMAL and NUMERIC
2428
--------------------------------------------------------
@@ -50,8 +54,9 @@ implementations as in the case with NUMERIC.
5054
FIXEDDECIMAL has a fixed scale value, which by default is 2. Internally numbers
5155
are stores as the actual value multiplied by 100. e.g. 50 would be stored as
5256
5000, and 1.23 would be stored as 123. This internal representation allows very
53-
fast addition and subtraction between two fixeddecimal types. Multiplication
54-
between two fixeddecimal types is slightly more complex. If we wanted to
57+
fast and accurate addition and subtraction between two fixeddecimal types.
58+
59+
Multiplication between two fixeddecimal types is slightly more complex. If we
5560
perform 2.00 * 3.00 in fixeddecimal, internally these numbers would be 200 and
5661
300 respectively, so internally 200 * 300 becomes 60000, which must be divided
5762
by 100 in order to obtain the correct internal result of 600, which of course
@@ -113,7 +118,7 @@ test=# select '1.239'::fixeddecimal;
113118

114119
It is especially important to remember that this truncation also occurs during
115120
arithmetic. Notice in the following example the result is 1120 rather than
116-
1129:
121+
1129, since 1.129 is immediately rounded to 1.12 on input.
117122

118123
```
119124
test=# select '1000'::fixeddecimal * '1.129'::fixeddecimal;

0 commit comments

Comments
 (0)