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-2024, 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 #define NUMERIC_MAX_PRECISION 1000
33 
34 #define NUMERIC_MIN_SCALE (-1000)
35 #define NUMERIC_MAX_SCALE 1000
36 
37 /*
38  * Internal limits on the scales chosen for calculation results
39  */
40 #define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION
41 #define NUMERIC_MIN_DISPLAY_SCALE 0
42 
43 #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 #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
62 {
63  return (Numeric) PG_DETOAST_DATUM(X);
64 }
65 
66 static inline Numeric
68 {
69  return (Numeric) PG_DETOAST_DATUM_COPY(X);
70 }
71 
72 static inline Datum
74 {
75  return PointerGetDatum(X);
76 }
77 
78 #define PG_GETARG_NUMERIC(n) DatumGetNumeric(PG_GETARG_DATUM(n))
79 #define PG_GETARG_NUMERIC_COPY(n) DatumGetNumericCopy(PG_GETARG_DATUM(n))
80 #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 
95  bool *have_error);
97  bool *have_error);
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 
108  Numeric rmin, Numeric rmax);
109 
110 #endif /* _PG_NUMERIC_H_ */
signed int int32
Definition: c.h:508
#define PG_DETOAST_DATUM_COPY(datum)
Definition: fmgr.h:242
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
long val
Definition: informix.c:689
int32 numeric_maximum_size(int32 typmod)
Definition: numeric.c:952
int32 numeric_int4_opt_error(Numeric num, bool *have_error)
Definition: numeric.c:4394
char * numeric_normalize(Numeric num)
Definition: numeric.c:1025
static Numeric DatumGetNumericCopy(Datum X)
Definition: numeric.h:67
Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3365
Numeric numeric_div_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3141
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4280
Numeric random_numeric(pg_prng_state *state, Numeric rmin, Numeric rmax)
Definition: numeric.c:4225
static Numeric DatumGetNumeric(Datum X)
Definition: numeric.h:61
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4301
int64 numeric_int8_opt_error(Numeric num, bool *have_error)
Definition: numeric.c:4482
struct NumericData * Numeric
Definition: numeric.h:54
bool numeric_is_nan(Numeric num)
Definition: numeric.c:850
Numeric numeric_sub_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2942
Numeric numeric_mul_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3020
char * numeric_out_sci(Numeric num, int scale)
Definition: numeric.c:991
Numeric numeric_add_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2864
bool numeric_is_inf(Numeric num)
Definition: numeric.c:861
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:73
static int scale
Definition: pgbench.c:181
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
Definition: regguts.h:323