Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
Is it always safe to negate a floating point number

Consider:

double f = foo();
double g = -f;

where foo() can return anything that be assigned to f.

is double g = -f; safe in C and C++? For IEEE 754 types it obviously is but C and C++ do not restrict floating point implementation to that (unlike Java).

If it is safe, then would -g always compare true with foo()?

(The above is not true for int in 2's complement).

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Floating types are defined by §5.2.4.2.2 of the C standard (draft N1570 at least):

The characteristics of floating types are defined in terms of a model that describes a representation of floating-point numbers and values that provide information about an implementation’s floating-point arithmetic.21) The following parameters are used to define the model for each floating-point type:

  • s sign (±1)
  • b base or radix of exponent representation (an integer > 1)
  • e exponent (an integer between a minimum emin and a maximum emax)
  • p precision (the number of base-b digits in the significand)
  • fk nonnegative integers less than b (the significand digits)

A floating-point number (x) is defined by the following model:

So yes, that expression is somewhat safe, except in the case where foo returned one of the "other kinds of floating point numbers" not specified by the standard:

In addition to normalized floating-point numbers ( f1 > 0 if x ≠ 0), floating types may be able to contain other kinds of floating-point numbers, such as subnormal floating-point numbers (x ≠ 0, e = emin, f1 = 0) and unnormalized floating-point numbers (x ≠ 0, e > emin, f1 = 0), and values that are not floating-point numbers, such as infinities and NaNs.

and:

An implementation may give zero and values that are not floating-point numbers (such as infinities and NaNs) a sign or may leave them unsigned.

There are probably other caveats to this, but the standard goes in a fair bit of details about the characteristics of these types. You can read all about them in this publicly available draft.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda