Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda