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-2023, 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 static inline Numeric
61 {
62  return (Numeric) PG_DETOAST_DATUM(X);
63 }
64 
65 static inline Numeric
67 {
68  return (Numeric) PG_DETOAST_DATUM_COPY(X);
69 }
70 
71 static inline Datum
73 {
74  return PointerGetDatum(X);
75 }
76 
77 #define PG_GETARG_NUMERIC(n) DatumGetNumeric(PG_GETARG_DATUM(n))
78 #define PG_GETARG_NUMERIC_COPY(n) DatumGetNumericCopy(PG_GETARG_DATUM(n))
79 #define PG_RETURN_NUMERIC(x) return NumericGetDatum(x)
80 
81 /*
82  * Utility functions in numeric.c
83  */
84 extern bool numeric_is_nan(Numeric num);
85 extern bool numeric_is_inf(Numeric num);
86 extern int32 numeric_maximum_size(int32 typmod);
87 extern char *numeric_out_sci(Numeric num, int scale);
88 extern char *numeric_normalize(Numeric num);
89 
90 extern Numeric int64_to_numeric(int64 val);
91 extern Numeric int64_div_fast_to_numeric(int64 val1, int log10val2);
92 
94  bool *have_error);
96  bool *have_error);
98  bool *have_error);
100  bool *have_error);
101 extern Numeric numeric_mod_opt_error(Numeric num1, Numeric num2,
102  bool *have_error);
103 extern int32 numeric_int4_opt_error(Numeric num, bool *have_error);
104 
105 #endif /* _PG_NUMERIC_H_ */
signed int int32
Definition: c.h:478
#define PG_DETOAST_DATUM_COPY(datum)
Definition: fmgr.h:242
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
long val
Definition: informix.c:664
int32 numeric_maximum_size(int32 typmod)
Definition: numeric.c:943
int32 numeric_int4_opt_error(Numeric num, bool *have_error)
Definition: numeric.c:4295
char * numeric_normalize(Numeric num)
Definition: numeric.c:1016
static Numeric DatumGetNumericCopy(Datum X)
Definition: numeric.h:66
Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3356
Numeric numeric_div_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3132
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4208
static Numeric DatumGetNumeric(Datum X)
Definition: numeric.h:60
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4229
struct NumericData * Numeric
Definition: numeric.h:53
bool numeric_is_nan(Numeric num)
Definition: numeric.c:841
Numeric numeric_sub_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2933
Numeric numeric_mul_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3011
char * numeric_out_sci(Numeric num, int scale)
Definition: numeric.c:982
Numeric numeric_add_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2855
bool numeric_is_inf(Numeric num)
Definition: numeric.c:852
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:72
int scale
Definition: pgbench.c:191
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64