how to avoid hypot() funtion returning infinity when it should not

Viewed 63

hypot() function returns infinity for a = b = 1e154 on OSX. It returns a valid value on Ubuntu.

#include <stdio.h>
#include <math.h>
int main() {
  long double h1 = hypot(1e153,1e153);
  long double h2 = hypot(1e154,1e154);
  long double h3 = hypot(1e155,1e155);
  printf("h1 = %Le\n", h1);
  printf("h2 = %Le\n", h2);
  printf("h3 = %Le\n", h3);
  return 0;
}

Result:

h1 = 1.414214e+153
h2 = inf
h3 = 1.414214e+155

It's a rare case but it overflows way under the maximum value (1e308 something)... Is there any way to avoid it? It overflows only on OSX.

0 Answers
Related