numeric.h 3.0 KB

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