1 /*-------------------------------------------------------------------------
4 * Definitions for the exact numeric data type of Postgres
6 * Original coding 1998, Jan Wieck. Heavily revised 2003, Tom Lane.
8 * Copyright (c) 1998-2022, PostgreSQL Global Development Group
10 * src/include/utils/numeric.h
12 *-------------------------------------------------------------------------
14 #ifndef _PG_NUMERIC_H_
15 #define _PG_NUMERIC_H_
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.
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
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!
31 #define NUMERIC_MAX_PRECISION 1000
33 #define NUMERIC_MIN_SCALE (-1000)
34 #define NUMERIC_MAX_SCALE 1000
37 * Internal limits on the scales chosen for calculation results
39 #define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION
40 #define NUMERIC_MIN_DISPLAY_SCALE 0
42 #define NUMERIC_MAX_RESULT_SCALE (NUMERIC_MAX_PRECISION * 2)
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.
49 #define NUMERIC_MIN_SIG_DIGITS 16
51 /* The actual contents of Numeric are private to numeric.c */
53 typedef struct NumericData
*Numeric
;
56 * fmgr interface macros
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)
67 * Utility functions in numeric.c
69 extern bool numeric_is_nan(Numeric num
);
70 extern bool numeric_is_inf(Numeric num
);
71 int32
numeric_maximum_size(int32 typmod
);
72 extern char *numeric_out_sci(Numeric num
, int scale
);
73 extern char *numeric_normalize(Numeric num
);
75 extern Numeric
int64_to_numeric(int64 val
);
76 extern Numeric
int64_div_fast_to_numeric(int64 val1
, int log10val2
);
78 extern Numeric
numeric_add_opt_error(Numeric num1
, Numeric num2
,
80 extern Numeric
numeric_sub_opt_error(Numeric num1
, Numeric num2
,
82 extern Numeric
numeric_mul_opt_error(Numeric num1
, Numeric num2
,
84 extern Numeric
numeric_div_opt_error(Numeric num1
, Numeric num2
,
86 extern Numeric
numeric_mod_opt_error(Numeric num1
, Numeric num2
,
88 extern int32
numeric_int4_opt_error(Numeric num
, bool *error
);
90 #endif /* _PG_NUMERIC_H_ */