PostgreSQL Source Code  git master
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

◆ DatumGetNumeric

◆ DatumGetNumericCopy

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

Definition at line 50 of file numeric.h.

Referenced by jsonb_numeric().

◆ NUMERIC_MAX_DISPLAY_SCALE

#define NUMERIC_MAX_DISPLAY_SCALE   NUMERIC_MAX_PRECISION

◆ NUMERIC_MAX_PRECISION

#define NUMERIC_MAX_PRECISION   1000

Definition at line 24 of file numeric.h.

Referenced by numerictypmodin().

◆ NUMERIC_MAX_RESULT_SCALE

#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().

◆ NUMERIC_MIN_DISPLAY_SCALE

#define NUMERIC_MIN_DISPLAY_SCALE   0

◆ NUMERIC_MIN_SIG_DIGITS

#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().

◆ NumericGetDatum

◆ PG_GETARG_NUMERIC

◆ PG_GETARG_NUMERIC_COPY

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

Definition at line 53 of file numeric.h.

◆ PG_RETURN_NUMERIC

Typedef Documentation

◆ Numeric

Definition at line 43 of file numeric.h.

Function Documentation

◆ numeric_is_nan()

bool numeric_is_nan ( Numeric  num)

Definition at line 680 of file numeric.c.

References NUMERIC_IS_NAN.

Referenced by gbt_numeric_penalty(), and PLyNumber_ToJsonbValue().

681 {
682  return NUMERIC_IS_NAN(num);
683 }
#define NUMERIC_IS_NAN(n)
Definition: numeric.c:171

◆ numeric_maximum_size()

int32 numeric_maximum_size ( int32  typmod)

Definition at line 691 of file numeric.c.

References DEC_DIGITS, NUMERIC_HDRSZ, and VARHDRSZ.

Referenced by type_maximum_size().

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

◆ numeric_normalize()

char* numeric_normalize ( Numeric  num)

Definition at line 757 of file numeric.c.

References get_str_from_var(), init_var_from_num(), NUMERIC_IS_NAN, pstrdup(), and generate_unaccent_rules::str.

Referenced by make_scalar_key().

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

◆ numeric_out_sci()

char* numeric_out_sci ( Numeric  num,
int  scale 
)

Definition at line 730 of file numeric.c.

References get_str_from_var_sci(), init_var_from_num(), NUMERIC_IS_NAN, pstrdup(), and generate_unaccent_rules::str.

Referenced by int8_to_char(), and numeric_to_char().

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