PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
numeric.h File Reference
#include "fmgr.h"
Include dependency graph for numeric.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define NUMERIC_MAX_PRECISION   1000
 
#define NUMERIC_MAX_DISPLAY_SCALE   NUMERIC_MAX_PRECISION
 
#define NUMERIC_MIN_DISPLAY_SCALE   0
 
#define NUMERIC_MAX_RESULT_SCALE   (NUMERIC_MAX_PRECISION * 2)
 
#define NUMERIC_MIN_SIG_DIGITS   16
 
#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))
 
#define PG_GETARG_NUMERIC_COPY(n)   DatumGetNumericCopy(PG_GETARG_DATUM(n))
 
#define PG_RETURN_NUMERIC(x)   return NumericGetDatum(x)
 

Typedefs

typedef struct NumericDataNumeric
 

Functions

bool numeric_is_nan (Numeric num)
 
int32 numeric_maximum_size (int32 typmod)
 
char * numeric_out_sci (Numeric num, int scale)
 
char * numeric_normalize (Numeric num)
 

Macro Definition Documentation

#define DatumGetNumericCopy (   X)    ((Numeric) PG_DETOAST_DATUM_COPY(X))

Definition at line 50 of file numeric.h.

#define NUMERIC_MAX_DISPLAY_SCALE   NUMERIC_MAX_PRECISION
#define NUMERIC_MAX_PRECISION   1000

Definition at line 24 of file numeric.h.

Referenced by numerictypmodin().

#define NUMERIC_MAX_RESULT_SCALE   (NUMERIC_MAX_PRECISION * 2)

Definition at line 32 of file numeric.h.

Referenced by exp_var(), numeric_exp(), numeric_round(), numeric_trunc(), and power_var().

#define NUMERIC_MIN_DISPLAY_SCALE   0
#define NUMERIC_MIN_SIG_DIGITS   16

Definition at line 39 of file numeric.h.

Referenced by log_var(), numeric_exp(), numeric_ln(), numeric_sqrt(), power_var(), and select_div_scale().

#define PG_GETARG_NUMERIC_COPY (   n)    DatumGetNumericCopy(PG_GETARG_DATUM(n))

Definition at line 53 of file numeric.h.

Typedef Documentation

Definition at line 43 of file numeric.h.

Function Documentation

bool numeric_is_nan ( Numeric  num)

Definition at line 679 of file numeric.c.

References NUMERIC_IS_NAN.

Referenced by gbt_numeric_penalty().

680 {
681  return NUMERIC_IS_NAN(num);
682 }
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
int32 numeric_maximum_size ( int32  typmod)

Definition at line 690 of file numeric.c.

References DEC_DIGITS, NUMERIC_HDRSZ, and VARHDRSZ.

Referenced by type_maximum_size().

691 {
692  int precision;
693  int numeric_digits;
694 
695  if (typmod < (int32) (VARHDRSZ))
696  return -1;
697 
698  /* precision (ie, max # of digits) is in upper bits of typmod */
699  precision = ((typmod - VARHDRSZ) >> 16) & 0xffff;
700 
701  /*
702  * This formula computes the maximum number of NumericDigits we could need
703  * in order to store the specified number of decimal digits. Because the
704  * weight is stored as a number of NumericDigits rather than a number of
705  * decimal digits, it's possible that the first NumericDigit will contain
706  * only a single decimal digit. Thus, the first two decimal digits can
707  * require two NumericDigits to store, but it isn't until we reach
708  * DEC_DIGITS + 2 decimal digits that we potentially need a third
709  * NumericDigit.
710  */
711  numeric_digits = (precision + 2 * (DEC_DIGITS - 1)) / DEC_DIGITS;
712 
713  /*
714  * In most cases, the size of a numeric will be smaller than the value
715  * computed below, because the varlena header will typically get toasted
716  * down to a single byte before being stored on disk, and it may also be
717  * possible to use a short numeric header. But our job here is to compute
718  * the worst case.
719  */
720  return NUMERIC_HDRSZ + (numeric_digits * sizeof(NumericDigit));
721 }
#define NUMERIC_HDRSZ
Definition: numeric.c:173
#define VARHDRSZ
Definition: c.h:439
signed int int32
Definition: c.h:246
int16 NumericDigit
Definition: numeric.c:99
#define DEC_DIGITS
Definition: numeric.c:95
char* numeric_normalize ( Numeric  num)

Definition at line 756 of file numeric.c.

References get_str_from_var(), init_var_from_num(), NUMERIC_IS_NAN, and pstrdup().

Referenced by make_scalar_key().

757 {
758  NumericVar x;
759  char *str;
760  int last;
761 
762  /*
763  * Handle NaN
764  */
765  if (NUMERIC_IS_NAN(num))
766  return pstrdup("NaN");
767 
768  init_var_from_num(num, &x);
769 
770  str = get_str_from_var(&x);
771 
772  /* If there's no decimal point, there's certainly nothing to remove. */
773  if (strchr(str, '.') != NULL)
774  {
775  /*
776  * Back up over trailing fractional zeroes. Since there is a decimal
777  * point, this loop will terminate safely.
778  */
779  last = strlen(str) - 1;
780  while (str[last] == '0')
781  last--;
782 
783  /* We want to get rid of the decimal point too, if it's now last. */
784  if (str[last] == '.')
785  last--;
786 
787  /* Delete whatever we backed up over. */
788  str[last + 1] = '\0';
789  }
790 
791  return str;
792 }
char * pstrdup(const char *in)
Definition: mcxt.c:1076
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:5721
static char * get_str_from_var(const NumericVar *var)
Definition: numeric.c:5764
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
char* numeric_out_sci ( Numeric  num,
int  scale 
)

Definition at line 729 of file numeric.c.

References get_str_from_var_sci(), init_var_from_num(), NUMERIC_IS_NAN, and pstrdup().

Referenced by int8_to_char(), and numeric_to_char().

730 {
731  NumericVar x;
732  char *str;
733 
734  /*
735  * Handle NaN
736  */
737  if (NUMERIC_IS_NAN(num))
738  return pstrdup("NaN");
739 
740  init_var_from_num(num, &x);
741 
742  str = get_str_from_var_sci(&x, scale);
743 
744  return str;
745 }
char * pstrdup(const char *in)
Definition: mcxt.c:1076
int scale
Definition: pgbench.c:106
static char * get_str_from_var_sci(const NumericVar *var, int rscale)
Definition: numeric.c:5917
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:5721
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170