| 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-2019, 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 "fmgr.h" |
| 18 | |
| 19 | /* |
| 20 | * Limit on the precision (and hence scale) specifiable in a NUMERIC typmod. |
| 21 | * Note that the implementation limit on the length of a numeric value is |
| 22 | * much larger --- beware of what you use this for! |
| 23 | */ |
| 24 | #define NUMERIC_MAX_PRECISION 1000 |
| 25 | |
| 26 | /* |
| 27 | * Internal limits on the scales chosen for calculation results |
| 28 | */ |
| 29 | #define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION |
| 30 | #define NUMERIC_MIN_DISPLAY_SCALE 0 |
| 31 | |
| 32 | #define NUMERIC_MAX_RESULT_SCALE (NUMERIC_MAX_PRECISION * 2) |
| 33 | |
| 34 | /* |
| 35 | * For inherently inexact calculations such as division and square root, |
| 36 | * we try to get at least this many significant digits; the idea is to |
| 37 | * deliver a result no worse than float8 would. |
| 38 | */ |
| 39 | #define NUMERIC_MIN_SIG_DIGITS 16 |
| 40 | |
| 41 | /* The actual contents of Numeric are private to numeric.c */ |
| 42 | struct NumericData; |
| 43 | typedef struct NumericData *Numeric; |
| 44 | |
| 45 | /* |
| 46 | * fmgr interface macros |
| 47 | */ |
| 48 | |
| 49 | #define DatumGetNumeric(X) ((Numeric) PG_DETOAST_DATUM(X)) |
| 50 | #define DatumGetNumericCopy(X) ((Numeric) PG_DETOAST_DATUM_COPY(X)) |
| 51 | #define NumericGetDatum(X) PointerGetDatum(X) |
| 52 | #define PG_GETARG_NUMERIC(n) DatumGetNumeric(PG_GETARG_DATUM(n)) |
| 53 | #define PG_GETARG_NUMERIC_COPY(n) DatumGetNumericCopy(PG_GETARG_DATUM(n)) |
| 54 | #define PG_RETURN_NUMERIC(x) return NumericGetDatum(x) |
| 55 | |
| 56 | /* |
| 57 | * Utility functions in numeric.c |
| 58 | */ |
| 59 | extern bool numeric_is_nan(Numeric num); |
| 60 | int32 numeric_maximum_size(int32 typmod); |
| 61 | extern char *numeric_out_sci(Numeric num, int scale); |
| 62 | extern char *numeric_normalize(Numeric num); |
| 63 | |
| 64 | extern Numeric numeric_add_opt_error(Numeric num1, Numeric num2, |
| 65 | bool *have_error); |
| 66 | extern Numeric numeric_sub_opt_error(Numeric num1, Numeric num2, |
| 67 | bool *have_error); |
| 68 | extern Numeric numeric_mul_opt_error(Numeric num1, Numeric num2, |
| 69 | bool *have_error); |
| 70 | extern Numeric numeric_div_opt_error(Numeric num1, Numeric num2, |
| 71 | bool *have_error); |
| 72 | extern Numeric numeric_mod_opt_error(Numeric num1, Numeric num2, |
| 73 | bool *have_error); |
| 74 | extern int32 numeric_int4_opt_error(Numeric num, bool *error); |
| 75 | |
| 76 | #endif /* _PG_NUMERIC_H_ */ |
| 77 | |