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 | SYNOPSIS |
---|
23 | #include <math.h> |
---|
24 | double hypot(double <[x]>, double <[y]>); |
---|
25 | float hypotf(float <[x]>, float <[y]>); |
---|
26 | |
---|
27 | DESCRIPTION |
---|
28 | <<hypot>> calculates the Euclidean distance |
---|
29 | @tex |
---|
30 | $\sqrt{x^2+y^2}$ |
---|
31 | @end tex |
---|
32 | @ifnottex |
---|
33 | <<sqrt(<[x]>*<[x]> + <[y]>*<[y]>)>> |
---|
34 | @end ifnottex |
---|
35 | between the origin (0,0) and a point represented by the |
---|
36 | Cartesian coordinates (<[x]>,<[y]>). <<hypotf>> differs only |
---|
37 | in the type of its arguments and result. |
---|
38 | |
---|
39 | RETURNS |
---|
40 | Normally, the distance value is returned. On overflow, |
---|
41 | <<hypot>> returns <<HUGE_VAL>> and sets <<errno>> to |
---|
42 | <<ERANGE>>. |
---|
43 | |
---|
44 | You can change the error treatment with <<matherr>>. |
---|
45 | |
---|
46 | PORTABILITY |
---|
47 | <<hypot>> and <<hypotf>> are not ANSI C. */ |
---|
48 | |
---|
49 | /* |
---|
50 | * wrapper hypot(x,y) |
---|
51 | */ |
---|
52 | |
---|
53 | #include "fdlibm.h" |
---|
54 | #include <errno.h> |
---|
55 | |
---|
56 | #ifndef _DOUBLE_IS_32BITS |
---|
57 | |
---|
58 | #ifdef __STDC__ |
---|
59 | double hypot(double x, double y)/* wrapper hypot */ |
---|
60 | #else |
---|
61 | double hypot(x,y) /* wrapper hypot */ |
---|
62 | double x,y; |
---|
63 | #endif |
---|
64 | { |
---|
65 | #ifdef _IEEE_LIBM |
---|
66 | return __ieee754_hypot(x,y); |
---|
67 | #else |
---|
68 | double z; |
---|
69 | struct exception exc; |
---|
70 | z = __ieee754_hypot(x,y); |
---|
71 | if(_LIB_VERSION == _IEEE_) return z; |
---|
72 | if((!finite(z))&&finite(x)&&finite(y)) { |
---|
73 | /* hypot(finite,finite) overflow */ |
---|
74 | #ifndef HUGE_VAL |
---|
75 | #define HUGE_VAL inf |
---|
76 | double inf = 0.0; |
---|
77 | |
---|
78 | SET_HIGH_WORD(inf,0x7ff00000); /* set inf to infinite */ |
---|
79 | #endif |
---|
80 | exc.type = OVERFLOW; |
---|
81 | exc.name = "hypot"; |
---|
82 | exc.err = 0; |
---|
83 | exc.arg1 = x; |
---|
84 | exc.arg2 = y; |
---|
85 | if (_LIB_VERSION == _SVID_) |
---|
86 | exc.retval = HUGE; |
---|
87 | else |
---|
88 | exc.retval = HUGE_VAL; |
---|
89 | if (_LIB_VERSION == _POSIX_) |
---|
90 | errno = ERANGE; |
---|
91 | else if (!matherr(&exc)) { |
---|
92 | errno = ERANGE; |
---|
93 | } |
---|
94 | if (exc.err != 0) |
---|
95 | errno = exc.err; |
---|
96 | return exc.retval; |
---|
97 | } else |
---|
98 | return z; |
---|
99 | #endif |
---|
100 | } |
---|
101 | |
---|
102 | #endif /* defined(_DOUBLE_IS_32BITS) */ |
---|