# AW: hypot division by zero

"Dehmel, Rüdiger" de@lmnet.de
Wed Oct 23 22:33:00 GMT 2002

```Hi Benjamin,

I don't know how to send a patch.

Testcase depends on config run, because the code is in a #ifndef HAVE_HYPOT
#endif
I my case a mingw cross compiler running on a sparc solaris2.6 has this
problem.

main()
{
printf(%d\n", hypot(0, 0));
}

The lenght of the hypothenuse of a right triangle with side x = 0 and y = 0
is 0!
Even a novice programmer shoud see the missing:
if (s == 0) return (0.);

code lines from libstdc++-v3/libmath/stubs.c
--- snip ---
/* Compute the hypothenuse of a right triangle with side x and y.  */
#ifndef HAVE_HYPOTF
float
hypotf(float x, float y)
{
float s = fabsf(x) + fabsf(y);
x /= s; y /= s;
return s * sqrtf(x * x + y * y);
}
#endif

#ifndef HAVE_HYPOT
double
hypot(double x, double y)
{
double s = fabs(x) + fabs(y);
x /= s; y /= s;
return s * sqrt(x * x + y * y);
}
#endif

#ifndef HAVE_HYPOTL
long double
hypotl(long double x, long double y)
{
long double s = fabsl(x) + fabsl(y);
x /= s; y /= s;
return s * sqrtl(x * x + y * y);
}
#endif
--- snip ---

Ruediger

> >the hypot functions in libstdc++-v3/libmath/stubs.c
> >makes a "division by zero"  exception when hypot(0, 0) is called.
> >
> >Some thing like:
> >
> >if (s == 0) return (0.);
> >
> >is missing.
> >
> >Other hypot implementations have such a test.
>
> You might want to submit a testcase, and a patch.
>
> -benjamin

```