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 669 of file numeric.c.

References NUMERIC_IS_NAN.

Referenced by gbt_numeric_penalty().

670 {
671  return NUMERIC_IS_NAN(num);
672 }
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170
int32 numeric_maximum_size ( int32  typmod)

Definition at line 680 of file numeric.c.

References DEC_DIGITS, NUMERIC_HDRSZ, and VARHDRSZ.

Referenced by type_maximum_size().

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

Definition at line 746 of file numeric.c.

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

Referenced by make_scalar_key().

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

Definition at line 719 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().

720 {
721  NumericVar x;
722  char *str;
723 
724  /*
725  * Handle NaN
726  */
727  if (NUMERIC_IS_NAN(num))
728  return pstrdup("NaN");
729 
730  init_var_from_num(num, &x);
731 
732  str = get_str_from_var_sci(&x, scale);
733 
734  return str;
735 }
static char * get_str_from_var_sci(NumericVar *var, int rscale)
Definition: numeric.c:5834
char * pstrdup(const char *in)
Definition: mcxt.c:1165
int scale
Definition: pgbench.c:106
static void init_var_from_num(Numeric num, NumericVar *dest)
Definition: numeric.c:5638
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:170