PostgreSQL Source Code  git master
numeric.h
Go to the documentation of this file.
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-2021, 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 "fmgr.h"
18 
19 /*
20  * Limits on the precision and scale specifiable in a NUMERIC typmod. The
21  * precision is strictly positive, but the scale may be positive or negative.
22  * A negative scale implies rounding before the decimal point.
23  *
24  * Note that the minimum display scale defined below is zero --- we always
25  * display all digits before the decimal point, even when the scale is
26  * negative.
27  *
28  * Note that the implementation limits on the precision and display scale of a
29  * numeric value are much larger --- beware of what you use these for!
30  */
31 #define NUMERIC_MAX_PRECISION 1000
32 
33 #define NUMERIC_MIN_SCALE (-1000)
34 #define NUMERIC_MAX_SCALE 1000
35 
36 /*
37  * Internal limits on the scales chosen for calculation results
38  */
39 #define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION
40 #define NUMERIC_MIN_DISPLAY_SCALE 0
41 
42 #define NUMERIC_MAX_RESULT_SCALE (NUMERIC_MAX_PRECISION * 2)
43 
44 /*
45  * For inherently inexact calculations such as division and square root,
46  * we try to get at least this many significant digits; the idea is to
47  * deliver a result no worse than float8 would.
48  */
49 #define NUMERIC_MIN_SIG_DIGITS 16
50 
51 /* The actual contents of Numeric are private to numeric.c */
52 struct NumericData;
53 typedef struct NumericData *Numeric;
54 
55 /*
56  * fmgr interface macros
57  */
58 
59 #define DatumGetNumeric(X) ((Numeric) PG_DETOAST_DATUM(X))
60 #define DatumGetNumericCopy(X) ((Numeric) PG_DETOAST_DATUM_COPY(X))
61 #define NumericGetDatum(X) PointerGetDatum(X)
62 #define PG_GETARG_NUMERIC(n) DatumGetNumeric(PG_GETARG_DATUM(n))
63 #define PG_GETARG_NUMERIC_COPY(n) DatumGetNumericCopy(PG_GETARG_DATUM(n))
64 #define PG_RETURN_NUMERIC(x) return NumericGetDatum(x)
65 
66 /*
67  * Utility functions in numeric.c
68  */
69 extern bool numeric_is_nan(Numeric num);
70 extern bool numeric_is_inf(Numeric num);
72 extern char *numeric_out_sci(Numeric num, int scale);
73 extern char *numeric_normalize(Numeric num);
74 
75 extern Numeric int64_to_numeric(int64 val);
76 extern Numeric int64_div_fast_to_numeric(int64 val1, int log10val2);
77 
78 extern Numeric numeric_add_opt_error(Numeric num1, Numeric num2,
79  bool *have_error);
80 extern Numeric numeric_sub_opt_error(Numeric num1, Numeric num2,
81  bool *have_error);
82 extern Numeric numeric_mul_opt_error(Numeric num1, Numeric num2,
83  bool *have_error);
84 extern Numeric numeric_div_opt_error(Numeric num1, Numeric num2,
85  bool *have_error);
86 extern Numeric numeric_mod_opt_error(Numeric num1, Numeric num2,
87  bool *have_error);
88 extern int32 numeric_int4_opt_error(Numeric num, bool *error);
89 
90 #endif /* _PG_NUMERIC_H_ */
char * numeric_out_sci(Numeric num, int scale)
Definition: numeric.c:910
static void error(void)
Definition: sql-dyntest.c:147
int32 numeric_int4_opt_error(Numeric num, bool *error)
Definition: numeric.c:4222
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4157
int scale
Definition: pgbench.c:191
struct NumericData * Numeric
Definition: numeric.h:53
Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3284
signed int int32
Definition: c.h:429
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4136
Numeric numeric_add_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2783
Numeric numeric_mul_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2939
char * numeric_normalize(Numeric num)
Definition: numeric.c:944
int32 numeric_maximum_size(int32 typmod)
Definition: numeric.c:871
Numeric numeric_div_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3060
bool numeric_is_nan(Numeric num)
Definition: numeric.c:769
bool numeric_is_inf(Numeric num)
Definition: numeric.c:780
Numeric numeric_sub_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2861
long val
Definition: informix.c:664