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-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 : /* forward declaration to avoid node.h include */ 21 : typedef struct Node Node; 22 : 23 : /* 24 : * Limits on the precision and scale specifiable in a NUMERIC typmod. The 25 : * precision is strictly positive, but the scale may be positive or negative. 26 : * A negative scale implies rounding before the decimal point. 27 : * 28 : * Note that the minimum display scale defined below is zero --- we always 29 : * display all digits before the decimal point, even when the scale is 30 : * negative. 31 : * 32 : * Note that the implementation limits on the precision and display scale of a 33 : * numeric value are much larger --- beware of what you use these for! 34 : */ 35 : #define NUMERIC_MAX_PRECISION 1000 36 : 37 : #define NUMERIC_MIN_SCALE (-1000) 38 : #define NUMERIC_MAX_SCALE 1000 39 : 40 : /* 41 : * Internal limits on the scales chosen for calculation results 42 : */ 43 : #define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION 44 : #define NUMERIC_MIN_DISPLAY_SCALE 0 45 : 46 : #define NUMERIC_MAX_RESULT_SCALE (NUMERIC_MAX_PRECISION * 2) 47 : 48 : /* 49 : * For inherently inexact calculations such as division and square root, 50 : * we try to get at least this many significant digits; the idea is to 51 : * deliver a result no worse than float8 would. 52 : */ 53 : #define NUMERIC_MIN_SIG_DIGITS 16 54 : 55 : /* The actual contents of Numeric are private to numeric.c */ 56 : struct NumericData; 57 : typedef struct NumericData *Numeric; 58 : 59 : /* 60 : * fmgr interface macros 61 : */ 62 : 63 : static inline Numeric 64 45553372 : DatumGetNumeric(Datum X) 65 : { 66 45553372 : return (Numeric) PG_DETOAST_DATUM(X); 67 : } 68 : 69 : static inline Numeric 70 18 : DatumGetNumericCopy(Datum X) 71 : { 72 18 : return (Numeric) PG_DETOAST_DATUM_COPY(X); 73 : } 74 : 75 : static inline Datum 76 4205454 : NumericGetDatum(Numeric X) 77 : { 78 4205454 : return PointerGetDatum(X); 79 : } 80 : 81 : #define PG_GETARG_NUMERIC(n) DatumGetNumeric(PG_GETARG_DATUM(n)) 82 : #define PG_GETARG_NUMERIC_COPY(n) DatumGetNumericCopy(PG_GETARG_DATUM(n)) 83 : #define PG_RETURN_NUMERIC(x) return NumericGetDatum(x) 84 : 85 : /* 86 : * Utility functions in numeric.c 87 : */ 88 : extern bool numeric_is_nan(Numeric num); 89 : extern bool numeric_is_inf(Numeric num); 90 : extern int32 numeric_maximum_size(int32 typmod); 91 : extern char *numeric_out_sci(Numeric num, int scale); 92 : extern char *numeric_normalize(Numeric num); 93 : 94 : extern Numeric int64_to_numeric(int64 val); 95 : extern Numeric int64_div_fast_to_numeric(int64 val1, int log10val2); 96 : 97 : extern Numeric numeric_add_safe(Numeric num1, Numeric num2, Node *escontext); 98 : extern Numeric numeric_sub_safe(Numeric num1, Numeric num2, Node *escontext); 99 : extern Numeric numeric_mul_safe(Numeric num1, Numeric num2, Node *escontext); 100 : extern Numeric numeric_div_safe(Numeric num1, Numeric num2, Node *escontext); 101 : extern Numeric numeric_mod_safe(Numeric num1, Numeric num2, Node *escontext); 102 : extern int32 numeric_int4_safe(Numeric num, Node *escontext); 103 : extern int64 numeric_int8_safe(Numeric num, Node *escontext); 104 : 105 : extern Numeric random_numeric(pg_prng_state *state, 106 : Numeric rmin, Numeric rmax); 107 : 108 : #endif /* _PG_NUMERIC_H_ */