AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / dba / Perguntas / 344772
Accepted
Sotis
Sotis
Asked: 2025-01-17 16:28:08 +0800 CST2025-01-17 16:28:08 +0800 CST 2025-01-17 16:28:08 +0800 CST

O tipo de dados `numeric(p,s)` é preciso?

  • 772

Eu sei que esse numericé um tipo de dado "exato", que representa números com a maior precisão possível para que não haja erros de ponto flutuante, como nos tipos de dados floatou real.

Mas e quanto a numeric(p,s)? Ele deve aproximar um número por causa da escala fixa, mas ele tem imprecisões de ponto flutuante ou é “seguro”? Não consegui encontrar nenhuma informação sobre ele. Talvez seja óbvio, mas eu gostaria de entender melhor esse aspecto.

postgresql
  • 1 1 respostas
  • 76 Views

1 respostas

  • Voted
  1. Best Answer
    Laurenz Albe
    2025-01-17T16:39:36+08:002025-01-17T16:39:36+08:00

    numeric(p,s)é preciso. É um “decimal codificado em binário”, ou seja, os dígitos são armazenados como estão. Veja a definição em src/backend/utils/adt/numeric.c:

    /* ----------
     * NumericVar is the format we use for arithmetic.  The digit-array part
     * is the same as the NumericData storage format, but the header is more
     * complex.
     *
     * The value represented by a NumericVar is determined by the sign, weight,
     * ndigits, and digits[] array.  If it is a "special" value (NaN or Inf)
     * then only the sign field matters; ndigits should be zero, and the weight
     * and dscale fields are ignored.
     *
     * Note: the first digit of a NumericVar's value is assumed to be multiplied
     * by NBASE ** weight.  Another way to say it is that there are weight+1
     * digits before the decimal point.  It is possible to have weight < 0.
     *
     * buf points at the physical start of the palloc'd digit buffer for the
     * NumericVar.  digits points at the first digit in actual use (the one
     * with the specified weight).  We normally leave an unused digit or two
     * (preset to zeroes) between buf and digits, so that there is room to store
     * a carry out of the top digit without reallocating space.  We just need to
     * decrement digits (and increment weight) to make room for the carry digit.
     * (There is no such extra space in a numeric value stored in the database,
     * only in a NumericVar in memory.)
     *
     * If buf is NULL then the digit buffer isn't actually palloc'd and should
     * not be freed --- see the constants below for an example.
     *
     * dscale, or display scale, is the nominal precision expressed as number
     * of digits after the decimal point (it must always be >= 0 at present).
     * dscale may be more than the number of physically stored fractional digits,
     * implying that we have suppressed storage of significant trailing zeroes.
     * It should never be less than the number of stored digits, since that would
     * imply hiding digits that are present.  NOTE that dscale is always expressed
     * in *decimal* digits, and so it may correspond to a fractional number of
     * base-NBASE digits --- divide by DEC_DIGITS to convert to NBASE digits.
     *
     * rscale, or result scale, is the target precision for a computation.
     * Like dscale it is expressed as number of *decimal* digits after the decimal
     * point, and is always >= 0 at present.
     * Note that rscale is not stored in variables --- it's figured on-the-fly
     * from the dscales of the inputs.
     *
     * While we consistently use "weight" to refer to the base-NBASE weight of
     * a numeric value, it is convenient in some scale-related calculations to
     * make use of the base-10 weight (ie, the approximate log10 of the value).
     * To avoid confusion, such a decimal-units weight is called a "dweight".
     *
     * NB: All the variable-level functions are written in a style that makes it
     * possible to give one and the same variable as argument and destination.
     * This is feasible because the digit buffer is separate from the variable.
     * ----------
     */
    typedef struct NumericVar
    {
        int         ndigits;        /* # of digits in digits[] - can be 0! */
        int         weight;         /* weight of first digit */
        int         sign;           /* NUMERIC_POS, _NEG, _NAN, _PINF, or _NINF */
        int         dscale;         /* display scale */
        NumericDigit *buf;          /* start of palloc'd space for digits[] */
        NumericDigit *digits;       /* base-NBASE digits */
    } NumericVar;
    
    • 3

relate perguntas

  • Posso ativar o PITR depois que o banco de dados foi usado

  • Práticas recomendadas para executar a replicação atrasada do deslocamento de tempo

  • Os procedimentos armazenados impedem a injeção de SQL?

  • Sequências Biológicas do UniProt no PostgreSQL

  • Qual é a diferença entre a replicação do PostgreSQL 9.0 e o Slony-I?

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    conectar ao servidor PostgreSQL: FATAL: nenhuma entrada pg_hba.conf para o host

    • 12 respostas
  • Marko Smith

    Como fazer a saída do sqlplus aparecer em uma linha?

    • 3 respostas
  • Marko Smith

    Selecione qual tem data máxima ou data mais recente

    • 3 respostas
  • Marko Smith

    Como faço para listar todos os esquemas no PostgreSQL?

    • 4 respostas
  • Marko Smith

    Listar todas as colunas de uma tabela especificada

    • 5 respostas
  • Marko Smith

    Como usar o sqlplus para se conectar a um banco de dados Oracle localizado em outro host sem modificar meu próprio tnsnames.ora

    • 4 respostas
  • Marko Smith

    Como você mysqldump tabela (s) específica (s)?

    • 4 respostas
  • Marko Smith

    Listar os privilégios do banco de dados usando o psql

    • 10 respostas
  • Marko Smith

    Como inserir valores em uma tabela de uma consulta de seleção no PostgreSQL?

    • 4 respostas
  • Marko Smith

    Como faço para listar todos os bancos de dados e tabelas usando o psql?

    • 7 respostas
  • Martin Hope
    Jin conectar ao servidor PostgreSQL: FATAL: nenhuma entrada pg_hba.conf para o host 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane Como faço para listar todos os esquemas no PostgreSQL? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh Por que o log de transações continua crescendo ou fica sem espaço? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland Listar todas as colunas de uma tabela especificada 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney O MySQL pode realizar consultas razoavelmente em bilhões de linhas? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx Como posso monitorar o andamento de uma importação de um arquivo .sql grande? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison Como você mysqldump tabela (s) específica (s)? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas Como posso cronometrar consultas SQL usando psql? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas Como inserir valores em uma tabela de uma consulta de seleção no PostgreSQL? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas Como faço para listar todos os bancos de dados e tabelas usando o psql? 2011-02-18 00:45:49 +0800 CST

Hot tag

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve