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-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#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 */
53struct NumericData;
54typedef struct NumericData *Numeric;
55
56/*
57 * fmgr interface macros
58 */
59
60static inline Numeric
62{
63 return (Numeric) PG_DETOAST_DATUM(X);
64}
65
66static inline Numeric
68{
70}
71
72static 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 */
85extern bool numeric_is_nan(Numeric num);
86extern bool numeric_is_inf(Numeric num);
87extern int32 numeric_maximum_size(int32 typmod);
88extern char *numeric_out_sci(Numeric num, int scale);
89extern char *numeric_normalize(Numeric num);
90
92extern Numeric int64_div_fast_to_numeric(int64 val1, int log10val2);
93
95 bool *have_error);
97 bool *have_error);
99 bool *have_error);
101 bool *have_error);
103 bool *have_error);
104extern int32 numeric_int4_opt_error(Numeric num, bool *have_error);
105extern int64 numeric_int8_opt_error(Numeric num, bool *have_error);
106
108 Numeric rmin, Numeric rmax);
109
110#endif /* _PG_NUMERIC_H_ */
int64_t int64
Definition: c.h:485
int32_t int32
Definition: c.h:484
#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
char * numeric_normalize(Numeric num)
Definition: numeric.c:1026
char * numeric_out_sci(Numeric num, int scale)
Definition: numeric.c:992
int32 numeric_maximum_size(int32 typmod)
Definition: numeric.c:953
int32 numeric_int4_opt_error(Numeric num, bool *have_error)
Definition: numeric.c:4515
static Numeric DatumGetNumericCopy(Datum X)
Definition: numeric.h:67
Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3486
Numeric numeric_div_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3262
Numeric int64_to_numeric(int64 val)
Definition: numeric.c:4401
Numeric random_numeric(pg_prng_state *state, Numeric rmin, Numeric rmax)
Definition: numeric.c:4346
static Numeric DatumGetNumeric(Datum X)
Definition: numeric.h:61
Numeric int64_div_fast_to_numeric(int64 val1, int log10val2)
Definition: numeric.c:4422
int64 numeric_int8_opt_error(Numeric num, bool *have_error)
Definition: numeric.c:4603
struct NumericData * Numeric
Definition: numeric.h:54
bool numeric_is_nan(Numeric num)
Definition: numeric.c:851
Numeric numeric_sub_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3063
Numeric numeric_mul_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:3141
Numeric numeric_add_opt_error(Numeric num1, Numeric num2, bool *have_error)
Definition: numeric.c:2985
bool numeric_is_inf(Numeric num)
Definition: numeric.c:862
static Datum NumericGetDatum(Numeric X)
Definition: numeric.h:73
static int scale
Definition: pgbench.c:182
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
Definition: regguts.h:323