2003-03-20 20:58:05 -05:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
1999-02-13 18:22:53 -05:00
|
|
|
* numeric.h
|
2003-03-20 20:58:05 -05:00
|
|
|
* Definitions for the exact numeric data type of Postgres
|
1998-12-30 14:56:35 -05:00
|
|
|
*
|
2003-03-20 20:58:05 -05:00
|
|
|
* Original coding 1998, Jan Wieck. Heavily revised 2003, Tom Lane.
|
1998-12-30 14:56:35 -05:00
|
|
|
*
|
2006-03-05 10:59:11 -05:00
|
|
|
* Copyright (c) 1998-2006, PostgreSQL Global Development Group
|
1998-12-30 14:56:35 -05:00
|
|
|
*
|
2006-07-11 09:54:25 -04:00
|
|
|
* $PostgreSQL: pgsql/src/include/utils/numeric.h,v 1.22 2006/07/11 13:54:24 momjian Exp $
|
1998-12-30 14:56:35 -05:00
|
|
|
*
|
2003-03-20 20:58:05 -05:00
|
|
|
*-------------------------------------------------------------------------
|
1998-12-30 14:56:35 -05:00
|
|
|
*/
|
|
|
|
|
#ifndef _PG_NUMERIC_H_
|
|
|
|
|
#define _PG_NUMERIC_H_
|
|
|
|
|
|
2006-07-11 09:54:25 -04:00
|
|
|
#include "fmgr.h"
|
|
|
|
|
|
2002-10-02 15:21:26 -04:00
|
|
|
/*
|
|
|
|
|
* Hardcoded precision limit - arbitrary, but must be small enough that
|
|
|
|
|
* dscale values will fit in 14 bits.
|
1998-12-30 14:56:35 -05:00
|
|
|
*/
|
1999-01-05 06:12:11 -05:00
|
|
|
#define NUMERIC_MAX_PRECISION 1000
|
1998-12-30 14:56:35 -05:00
|
|
|
|
2002-10-02 15:21:26 -04:00
|
|
|
/*
|
2000-01-15 18:42:49 -05:00
|
|
|
* Internal limits on the scales chosen for calculation results
|
|
|
|
|
*/
|
1999-05-25 12:15:34 -04:00
|
|
|
#define NUMERIC_MAX_DISPLAY_SCALE NUMERIC_MAX_PRECISION
|
2002-10-02 15:21:26 -04:00
|
|
|
#define NUMERIC_MIN_DISPLAY_SCALE 0
|
1998-12-30 14:56:35 -05:00
|
|
|
|
1999-05-25 12:15:34 -04:00
|
|
|
#define NUMERIC_MAX_RESULT_SCALE (NUMERIC_MAX_PRECISION * 2)
|
1998-12-30 14:56:35 -05:00
|
|
|
|
2002-10-02 15:21:26 -04:00
|
|
|
/*
|
|
|
|
|
* For inherently inexact calculations such as division and square root,
|
|
|
|
|
* we try to get at least this many significant digits; the idea is to
|
|
|
|
|
* deliver a result no worse than float8 would.
|
|
|
|
|
*/
|
|
|
|
|
#define NUMERIC_MIN_SIG_DIGITS 16
|
1998-12-30 14:56:35 -05:00
|
|
|
|
2002-10-02 15:21:26 -04:00
|
|
|
|
|
|
|
|
/*
|
2000-01-15 18:42:49 -05:00
|
|
|
* Sign values and macros to deal with packing/unpacking n_sign_dscale
|
1998-12-30 14:56:35 -05:00
|
|
|
*/
|
|
|
|
|
#define NUMERIC_SIGN_MASK 0xC000
|
1999-05-25 12:15:34 -04:00
|
|
|
#define NUMERIC_POS 0x0000
|
1998-12-30 14:56:35 -05:00
|
|
|
#define NUMERIC_NEG 0x4000
|
|
|
|
|
#define NUMERIC_NAN 0xC000
|
2000-04-12 13:17:23 -04:00
|
|
|
#define NUMERIC_DSCALE_MASK 0x3FFF
|
1998-12-30 14:56:35 -05:00
|
|
|
#define NUMERIC_SIGN(n) ((n)->n_sign_dscale & NUMERIC_SIGN_MASK)
|
2000-01-15 18:42:49 -05:00
|
|
|
#define NUMERIC_DSCALE(n) ((n)->n_sign_dscale & NUMERIC_DSCALE_MASK)
|
2003-03-20 20:58:05 -05:00
|
|
|
#define NUMERIC_IS_NAN(n) (NUMERIC_SIGN(n) != NUMERIC_POS && \
|
|
|
|
|
NUMERIC_SIGN(n) != NUMERIC_NEG)
|
1998-12-30 14:56:35 -05:00
|
|
|
|
|
|
|
|
|
2002-10-02 15:21:26 -04:00
|
|
|
/*
|
1998-12-30 14:56:35 -05:00
|
|
|
* The Numeric data type stored in the database
|
2000-01-15 18:42:49 -05:00
|
|
|
*
|
|
|
|
|
* NOTE: by convention, values in the packed form have been stripped of
|
2003-03-20 20:58:05 -05:00
|
|
|
* all leading and trailing zero digits (where a "digit" is of base NBASE).
|
|
|
|
|
* In particular, if the value is zero, there will be no digits at all!
|
|
|
|
|
* The weight is arbitrary in that case, but we normally set it to zero.
|
1998-12-30 14:56:35 -05:00
|
|
|
*/
|
1999-05-25 12:15:34 -04:00
|
|
|
typedef struct NumericData
|
|
|
|
|
{
|
2003-03-20 20:58:05 -05:00
|
|
|
int32 varlen; /* Variable size (std varlena header) */
|
1999-05-25 12:15:34 -04:00
|
|
|
int16 n_weight; /* Weight of 1st digit */
|
|
|
|
|
uint16 n_sign_dscale; /* Sign + display scale */
|
2003-03-20 20:58:05 -05:00
|
|
|
char n_data[1]; /* Digits (really array of NumericDigit) */
|
1999-05-25 18:43:53 -04:00
|
|
|
} NumericData;
|
2003-03-20 20:58:05 -05:00
|
|
|
|
1998-12-30 14:56:35 -05:00
|
|
|
typedef NumericData *Numeric;
|
|
|
|
|
|
2003-03-20 20:58:05 -05:00
|
|
|
#define NUMERIC_HDRSZ (sizeof(int32) + sizeof(int16) + sizeof(uint16))
|
1998-12-30 14:56:35 -05:00
|
|
|
|
|
|
|
|
|
2000-06-13 03:35:40 -04:00
|
|
|
/*
|
|
|
|
|
* fmgr interface macros
|
|
|
|
|
*/
|
|
|
|
|
|
2001-03-21 23:01:46 -05: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-16 23:05:41 -04:00
|
|
|
#define PG_GETARG_NUMERIC_COPY(n) DatumGetNumericCopy(PG_GETARG_DATUM(n))
|
2001-03-21 23:01:46 -05:00
|
|
|
#define PG_RETURN_NUMERIC(x) return NumericGetDatum(x)
|
2001-10-28 01:26:15 -05:00
|
|
|
|
2001-11-05 12:46:40 -05:00
|
|
|
#endif /* _PG_NUMERIC_H_ */
|