Ver Mensaje Individual
  #17 (permalink)  
Antiguo 18/05/2016, 06:07
enrieto
 
Fecha de Ingreso: abril-2016
Mensajes: 31
Antigüedad: 8 años, 1 mes
Puntos: 5
Respuesta: Saber si un double se ha desbordado

Prueba
Código C:
Ver original
  1. #include <float.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. bool chechSum(double a, double b)
  7. {
  8.     if(copysign(a, b) == a &&
  9.         fabs(b) > DBL_MAX - fabs(a))
  10.         return false;
  11.  
  12.     return true;
  13. }
  14.  
  15.  
  16. int main()
  17. {
  18.     double a = 1.7976931348623158e+308;
  19.     double b = -0.01;
  20.     double s = 0;
  21.  
  22.     if (chechSum(a, b)) {
  23.         s = a + b;
  24.         printf("%f\n", s);
  25.     }
  26.     else
  27.         printf("overflow\n");
  28.  
  29. }