Line data Source code
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 61 18316154 : DatumGetNumeric(Datum X) 62 : { 63 18316154 : return (Numeric) PG_DETOAST_DATUM(X); 64 : } 65 : 66 : static inline Numeric 67 18 : DatumGetNumericCopy(Datum X) 68 : { 69 18 : return (Numeric) PG_DETOAST_DATUM_COPY(X); 70 : } 71 : 72 : static inline Datum 73 3693158 : NumericGetDatum(Numeric X) 74 : { 75 3693158 : 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 : 94 : extern Numeric numeric_add_opt_error(Numeric num1, Numeric num2, 95 : bool *have_error); 96 : extern Numeric numeric_sub_opt_error(Numeric num1, Numeric num2, 97 : bool *have_error); 98 : extern Numeric numeric_mul_opt_error(Numeric num1, Numeric num2, 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 : 107 : extern Numeric random_numeric(pg_prng_state *state, 108 : Numeric rmin, Numeric rmax); 109 : 110 : #endif /* _PG_NUMERIC_H_ */