/src/postgres/src/include/utils/numeric.h
Line | Count | Source (jump to first uncovered line) |
1 | | /*------------------------------------------------------------------------- |
2 | | * |
3 | | * numeric.h |
4 | | * Definitions for the exact numeric data type of Postgres |
5 | | * |
6 | | * Original coding 1998, Jan Wieck. Heavily revised 2003, Tom Lane. |
7 | | * |
8 | | * Copyright (c) 1998-2025, PostgreSQL Global Development Group |
9 | | * |
10 | | * src/include/utils/numeric.h |
11 | | * |
12 | | *------------------------------------------------------------------------- |
13 | | */ |
14 | | #ifndef _PG_NUMERIC_H_ |
15 | | #define _PG_NUMERIC_H_ |
16 | | |
17 | | #include "common/pg_prng.h" |
18 | | #include "fmgr.h" |
19 | | |
20 | | /* |
21 | | * Limits on the precision and scale specifiable in a NUMERIC typmod. The |
22 | | * precision is strictly positive, but the scale may be positive or negative. |
23 | | * A negative scale implies rounding before the decimal point. |
24 | | * |
25 | | * Note that the minimum display scale defined below is zero --- we always |
26 | | * display all digits before the decimal point, even when the scale is |
27 | | * negative. |
28 | | * |
29 | | * Note that the implementation limits on the precision and display scale of a |
30 | | * numeric value are much larger --- beware of what you use these for! |
31 | | */ |
32 | 0 | #define NUMERIC_MAX_PRECISION 1000 |
33 | | |
34 | 0 | #define NUMERIC_MIN_SCALE (-1000) |
35 | 0 | #define NUMERIC_MAX_SCALE 1000 |
36 | | |
37 | | /* |
38 | | * Internal limits on the scales chosen for calculation results |
39 | | */ |
40 | 0 | #define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION |
41 | | #define NUMERIC_MIN_DISPLAY_SCALE 0 |
42 | | |
43 | 0 | #define NUMERIC_MAX_RESULT_SCALE (NUMERIC_MAX_PRECISION * 2) |
44 | | |
45 | | /* |
46 | | * For inherently inexact calculations such as division and square root, |
47 | | * we try to get at least this many significant digits; the idea is to |
48 | | * deliver a result no worse than float8 would. |
49 | | */ |
50 | 0 | #define NUMERIC_MIN_SIG_DIGITS 16 |
51 | | |
52 | | /* The actual contents of Numeric are private to numeric.c */ |
53 | | struct NumericData; |
54 | | typedef struct NumericData *Numeric; |
55 | | |
56 | | /* |
57 | | * fmgr interface macros |
58 | | */ |
59 | | |
60 | | static inline Numeric |
61 | | DatumGetNumeric(Datum X) |
62 | 0 | { |
63 | 0 | return (Numeric) PG_DETOAST_DATUM(X); |
64 | 0 | } Unexecuted instantiation: execExpr.c:DatumGetNumeric Unexecuted instantiation: execExprInterp.c:DatumGetNumeric Unexecuted instantiation: nodeTableFuncscan.c:DatumGetNumeric Unexecuted instantiation: clauses.c:DatumGetNumeric Unexecuted instantiation: to_tsany.c:DatumGetNumeric Unexecuted instantiation: wparser.c:DatumGetNumeric Unexecuted instantiation: cash.c:DatumGetNumeric Unexecuted instantiation: date.c:DatumGetNumeric Unexecuted instantiation: dbsize.c:DatumGetNumeric Unexecuted instantiation: format_type.c:DatumGetNumeric Unexecuted instantiation: formatting.c:DatumGetNumeric Unexecuted instantiation: json.c:DatumGetNumeric Unexecuted instantiation: jsonb.c:DatumGetNumeric Unexecuted instantiation: jsonb_gin.c:DatumGetNumeric Unexecuted instantiation: jsonb_op.c:DatumGetNumeric Unexecuted instantiation: jsonb_util.c:DatumGetNumeric Unexecuted instantiation: jsonfuncs.c:DatumGetNumeric Unexecuted instantiation: jsonbsubs.c:DatumGetNumeric Unexecuted instantiation: jsonpath.c:DatumGetNumeric Unexecuted instantiation: jsonpath_exec.c:DatumGetNumeric Unexecuted instantiation: jsonpath_gram.c:DatumGetNumeric Unexecuted instantiation: jsonpath_scan.c:DatumGetNumeric Unexecuted instantiation: numeric.c:DatumGetNumeric Unexecuted instantiation: pg_lsn.c:DatumGetNumeric Unexecuted instantiation: pseudorandomfuncs.c:DatumGetNumeric Unexecuted instantiation: timestamp.c:DatumGetNumeric |
65 | | |
66 | | static inline Numeric |
67 | | DatumGetNumericCopy(Datum X) |
68 | 0 | { |
69 | 0 | return (Numeric) PG_DETOAST_DATUM_COPY(X); |
70 | 0 | } Unexecuted instantiation: execExpr.c:DatumGetNumericCopy Unexecuted instantiation: execExprInterp.c:DatumGetNumericCopy Unexecuted instantiation: nodeTableFuncscan.c:DatumGetNumericCopy Unexecuted instantiation: clauses.c:DatumGetNumericCopy Unexecuted instantiation: to_tsany.c:DatumGetNumericCopy Unexecuted instantiation: wparser.c:DatumGetNumericCopy Unexecuted instantiation: cash.c:DatumGetNumericCopy Unexecuted instantiation: date.c:DatumGetNumericCopy Unexecuted instantiation: dbsize.c:DatumGetNumericCopy Unexecuted instantiation: format_type.c:DatumGetNumericCopy Unexecuted instantiation: formatting.c:DatumGetNumericCopy Unexecuted instantiation: json.c:DatumGetNumericCopy Unexecuted instantiation: jsonb.c:DatumGetNumericCopy Unexecuted instantiation: jsonb_gin.c:DatumGetNumericCopy Unexecuted instantiation: jsonb_op.c:DatumGetNumericCopy Unexecuted instantiation: jsonb_util.c:DatumGetNumericCopy Unexecuted instantiation: jsonfuncs.c:DatumGetNumericCopy Unexecuted instantiation: jsonbsubs.c:DatumGetNumericCopy Unexecuted instantiation: jsonpath.c:DatumGetNumericCopy Unexecuted instantiation: jsonpath_exec.c:DatumGetNumericCopy Unexecuted instantiation: jsonpath_gram.c:DatumGetNumericCopy Unexecuted instantiation: jsonpath_scan.c:DatumGetNumericCopy Unexecuted instantiation: numeric.c:DatumGetNumericCopy Unexecuted instantiation: pg_lsn.c:DatumGetNumericCopy Unexecuted instantiation: pseudorandomfuncs.c:DatumGetNumericCopy Unexecuted instantiation: timestamp.c:DatumGetNumericCopy |
71 | | |
72 | | static inline Datum |
73 | | NumericGetDatum(Numeric X) |
74 | 0 | { |
75 | 0 | return PointerGetDatum(X); |
76 | 0 | } Unexecuted instantiation: execExpr.c:NumericGetDatum Unexecuted instantiation: execExprInterp.c:NumericGetDatum Unexecuted instantiation: nodeTableFuncscan.c:NumericGetDatum Unexecuted instantiation: clauses.c:NumericGetDatum Unexecuted instantiation: to_tsany.c:NumericGetDatum Unexecuted instantiation: wparser.c:NumericGetDatum Unexecuted instantiation: cash.c:NumericGetDatum Unexecuted instantiation: date.c:NumericGetDatum Unexecuted instantiation: dbsize.c:NumericGetDatum Unexecuted instantiation: format_type.c:NumericGetDatum Unexecuted instantiation: formatting.c:NumericGetDatum Unexecuted instantiation: json.c:NumericGetDatum Unexecuted instantiation: jsonb.c:NumericGetDatum Unexecuted instantiation: jsonb_gin.c:NumericGetDatum Unexecuted instantiation: jsonb_op.c:NumericGetDatum Unexecuted instantiation: jsonb_util.c:NumericGetDatum Unexecuted instantiation: jsonfuncs.c:NumericGetDatum Unexecuted instantiation: jsonbsubs.c:NumericGetDatum Unexecuted instantiation: jsonpath.c:NumericGetDatum Unexecuted instantiation: jsonpath_exec.c:NumericGetDatum Unexecuted instantiation: jsonpath_gram.c:NumericGetDatum Unexecuted instantiation: jsonpath_scan.c:NumericGetDatum Unexecuted instantiation: numeric.c:NumericGetDatum Unexecuted instantiation: pg_lsn.c:NumericGetDatum Unexecuted instantiation: pseudorandomfuncs.c:NumericGetDatum Unexecuted instantiation: timestamp.c:NumericGetDatum |
77 | | |
78 | 0 | #define PG_GETARG_NUMERIC(n) DatumGetNumeric(PG_GETARG_DATUM(n)) |
79 | | #define PG_GETARG_NUMERIC_COPY(n) DatumGetNumericCopy(PG_GETARG_DATUM(n)) |
80 | 0 | #define PG_RETURN_NUMERIC(x) return NumericGetDatum(x) |
81 | | |
82 | | /* |
83 | | * Utility functions in numeric.c |
84 | | */ |
85 | | extern bool numeric_is_nan(Numeric num); |
86 | | extern bool numeric_is_inf(Numeric num); |
87 | | extern int32 numeric_maximum_size(int32 typmod); |
88 | | extern char *numeric_out_sci(Numeric num, int scale); |
89 | | extern char *numeric_normalize(Numeric num); |
90 | | |
91 | | extern Numeric int64_to_numeric(int64 val); |
92 | | extern Numeric int64_div_fast_to_numeric(int64 val1, int log10val2); |
93 | | |
94 | | extern Numeric numeric_add_opt_error(Numeric num1, Numeric num2, |
95 | | bool *have_error); |
96 | | extern Numeric numeric_sub_opt_error(Numeric num1, Numeric num2, |
97 | | bool *have_error); |
98 | | extern Numeric numeric_mul_opt_error(Numeric num1, Numeric num2, |
99 | | bool *have_error); |
100 | | extern Numeric numeric_div_opt_error(Numeric num1, Numeric num2, |
101 | | bool *have_error); |
102 | | extern Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, |
103 | | bool *have_error); |
104 | | extern int32 numeric_int4_opt_error(Numeric num, bool *have_error); |
105 | | extern int64 numeric_int8_opt_error(Numeric num, bool *have_error); |
106 | | |
107 | | extern Numeric random_numeric(pg_prng_state *state, |
108 | | Numeric rmin, Numeric rmax); |
109 | | |
110 | | #endif /* _PG_NUMERIC_H_ */ |