1 
2 /* @(#)w_hypot.c 5.1 93/09/24 */
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  *
7  * Developed at SunPro, a Sun Microsystems, Inc. business.
8  * Permission to use, copy, modify, and distribute this
9  * software is freely granted, provided that this notice
10  * is preserved.
11  * ====================================================
12  */
13 
14 /*
15 FUNCTION
16 	<<hypot>>, <<hypotf>>---distance from origin
17 INDEX
18 	hypot
19 INDEX
20 	hypotf
21 
22 ANSI_SYNOPSIS
23 	#include <math.h>
24 	double hypot(double <[x]>, double <[y]>);
25 	float hypotf(float <[x]>, float <[y]>);
26 
27 TRAD_SYNOPSIS
28 	double hypot(<[x]>, <[y]>)
29 	double <[x]>, <[y]>;
30 
31 	float hypotf(<[x]>, <[y]>)
32 	float <[x]>, <[y]>;
33 
34 DESCRIPTION
35 	<<hypot>> calculates the Euclidean distance
36 	@tex
37 	$\sqrt{x^2+y^2}$
38 	@end tex
39 	@ifnottex
40 	<<sqrt(<[x]>*<[x]> + <[y]>*<[y]>)>>
41 	@end ifnottex
42 	between the origin (0,0) and a point represented by the
43 	Cartesian coordinates (<[x]>,<[y]>).  <<hypotf>> differs only
44 	in the type of its arguments and result.
45 
46 RETURNS
47 	Normally, the distance value is returned.  On overflow,
48 	<<hypot>> returns <<HUGE_VAL>> and sets <<errno>> to
49 	<<ERANGE>>.
50 
51 	You can change the error treatment with <<matherr>>.
52 
53 PORTABILITY
54 	<<hypot>> and <<hypotf>> are not ANSI C.  */
55 
56 /*
57  * wrapper hypot(x,y)
58  */
59 
60 #include "fdlibm.h"
61 #include <errno.h>
62 
63 #ifndef _DOUBLE_IS_32BITS
64 
65 #ifdef __STDC__
hypot(double x,double y)66 	double hypot(double x, double y)/* wrapper hypot */
67 #else
68 	double hypot(x,y)		/* wrapper hypot */
69 	double x,y;
70 #endif
71 {
72 #ifdef _IEEE_LIBM
73 	return __ieee754_hypot(x,y);
74 #else
75 	double z;
76 	struct exception exc;
77 	z = __ieee754_hypot(x,y);
78 	if(_LIB_VERSION == _IEEE_) return z;
79 	if((!finite(z))&&finite(x)&&finite(y)) {
80 	    /* hypot(finite,finite) overflow */
81 #ifndef HUGE_VAL
82 #define HUGE_VAL inf
83 	    double inf = 0.0;
84 
85 	    SET_HIGH_WORD(inf,0x7ff00000);	/* set inf to infinite */
86 #endif
87 	    exc.type = OVERFLOW;
88 	    exc.name = "hypot";
89 	    exc.err = 0;
90 	    exc.arg1 = x;
91 	    exc.arg2 = y;
92 	    if (_LIB_VERSION == _SVID_)
93 	       exc.retval = HUGE;
94 	    else
95 	       exc.retval = HUGE_VAL;
96 	    if (_LIB_VERSION == _POSIX_)
97 	       errno = ERANGE;
98 	    else if (!matherr(&exc)) {
99 	     	errno = ERANGE;
100 	    }
101 	    if (exc.err != 0)
102 	       errno = exc.err;
103             return exc.retval;
104 	} else
105 	    return z;
106 #endif
107 }
108 
109 #endif /* defined(_DOUBLE_IS_32BITS) */
110