1998-12-30 20:56:35 +01:00
|
|
|
/* ----------
|
1999-02-14 00:22:53 +01:00
|
|
|
* numeric.h
|
1998-12-30 20:56:35 +01:00
|
|
|
*
|
|
|
|
* Definitions for the exact numeric data type of Postgres
|
|
|
|
*
|
|
|
|
* 1998 Jan Wieck
|
|
|
|
*
|
2001-10-28 07:26:15 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/include/utils/numeric.h,v 1.14 2001/10/28 06:26:09 momjian Exp $
|
1998-12-30 20:56:35 +01:00
|
|
|
*
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _PG_NUMERIC_H_
|
|
|
|
#define _PG_NUMERIC_H_
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* The hardcoded limits and defaults of the numeric data type
|
|
|
|
* ----------
|
|
|
|
*/
|
1999-01-05 12:12:11 +01:00
|
|
|
#define NUMERIC_MAX_PRECISION 1000
|
1998-12-30 20:56:35 +01:00
|
|
|
#define NUMERIC_DEFAULT_PRECISION 30
|
|
|
|
#define NUMERIC_DEFAULT_SCALE 6
|
|
|
|
|
2000-01-16 00:42:49 +01:00
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* Internal limits on the scales chosen for calculation results
|
|
|
|
* ----------
|
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
#define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION
|
2000-01-16 00:42:49 +01:00
|
|
|
#define NUMERIC_MIN_DISPLAY_SCALE (NUMERIC_DEFAULT_SCALE + 4)
|
1998-12-30 20:56:35 +01:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
#define NUMERIC_MAX_RESULT_SCALE (NUMERIC_MAX_PRECISION * 2)
|
1998-12-30 20:56:35 +01:00
|
|
|
#define NUMERIC_MIN_RESULT_SCALE (NUMERIC_DEFAULT_PRECISION + 4)
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------
|
2000-01-16 00:42:49 +01:00
|
|
|
* Sign values and macros to deal with packing/unpacking n_sign_dscale
|
1998-12-30 20:56:35 +01:00
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
#define NUMERIC_SIGN_MASK 0xC000
|
1999-05-25 18:15:34 +02:00
|
|
|
#define NUMERIC_POS 0x0000
|
1998-12-30 20:56:35 +01:00
|
|
|
#define NUMERIC_NEG 0x4000
|
|
|
|
#define NUMERIC_NAN 0xC000
|
2000-04-12 19:17:23 +02:00
|
|
|
#define NUMERIC_DSCALE_MASK 0x3FFF
|
1998-12-30 20:56:35 +01:00
|
|
|
#define NUMERIC_SIGN(n) ((n)->n_sign_dscale & NUMERIC_SIGN_MASK)
|
2000-01-16 00:42:49 +01:00
|
|
|
#define NUMERIC_DSCALE(n) ((n)->n_sign_dscale & NUMERIC_DSCALE_MASK)
|
1999-05-25 18:15:34 +02:00
|
|
|
#define NUMERIC_IS_NAN(n) (NUMERIC_SIGN(n) != NUMERIC_POS && \
|
1998-12-30 20:56:35 +01:00
|
|
|
NUMERIC_SIGN(n) != NUMERIC_NEG)
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* The Numeric data type stored in the database
|
2000-01-16 00:42:49 +01:00
|
|
|
*
|
|
|
|
* NOTE: by convention, values in the packed form have been stripped of
|
|
|
|
* all leading and trailing zeroes (except there will be a trailing zero
|
|
|
|
* in the last byte, if the number of digits is odd). In particular,
|
|
|
|
* if the value is zero, there will be no digits at all! The weight is
|
2000-07-17 05:05:41 +02:00
|
|
|
* arbitrary in that case, but we normally set it to zero.
|
1998-12-30 20:56:35 +01:00
|
|
|
* ----------
|
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
typedef struct NumericData
|
|
|
|
{
|
|
|
|
int32 varlen; /* Variable size */
|
|
|
|
int16 n_weight; /* Weight of 1st digit */
|
|
|
|
uint16 n_rscale; /* Result scale */
|
|
|
|
uint16 n_sign_dscale; /* Sign + display scale */
|
2000-01-16 00:42:49 +01:00
|
|
|
unsigned char n_data[1]; /* Digit data (2 decimal digits/byte) */
|
1999-05-26 00:43:53 +02:00
|
|
|
} NumericData;
|
1998-12-30 20:56:35 +01:00
|
|
|
typedef NumericData *Numeric;
|
|
|
|
|
|
|
|
#define NUMERIC_HDRSZ (sizeof(int32) + sizeof(uint16) * 3)
|
|
|
|
|
|
|
|
|
2000-06-13 09:35:40 +02:00
|
|
|
/*
|
|
|
|
* fmgr interface macros
|
|
|
|
*/
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
#define DatumGetNumeric(X) ((Numeric) PG_DETOAST_DATUM(X))
|
|
|
|
#define DatumGetNumericCopy(X) ((Numeric) PG_DETOAST_DATUM_COPY(X))
|
|
|
|
#define NumericGetDatum(X) PointerGetDatum(X)
|
|
|
|
#define PG_GETARG_NUMERIC(n) DatumGetNumeric(PG_GETARG_DATUM(n))
|
2000-07-17 05:05:41 +02:00
|
|
|
#define PG_GETARG_NUMERIC_COPY(n) DatumGetNumericCopy(PG_GETARG_DATUM(n))
|
2001-03-22 05:01:46 +01:00
|
|
|
#define PG_RETURN_NUMERIC(x) return NumericGetDatum(x)
|
2001-10-28 07:26:15 +01:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
#endif /* _PG_NUMERIC_H_ */
|