Rev 8793 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8793 | Rev 9765 | ||
---|---|---|---|
Line 15... | Line 15... | ||
15 | * inflicting too much of a performance hit. |
15 | * inflicting too much of a performance hit. |
16 | * |
16 | * |
17 | */ |
17 | */ |
Line 18... | Line 18... | ||
18 | 18 | ||
19 | /// #include |
- | |
20 | #include |
19 | /// #include |
- | 20 | #include |
|
Line 21... | Line 21... | ||
21 | #include |
21 | #include |
22 | 22 | ||
23 | /* Approximate square roots of DBL_MAX and DBL_MIN. Numbers |
23 | /* Approximate square roots of DBL_MAX and DBL_MIN. Numbers |
24 | between these two shouldn't neither overflow nor underflow |
24 | between these two shouldn't neither overflow nor underflow |
25 | when squared. */ |
25 | when squared. */ |
Line 26... | Line -... | ||
26 | #define __SQRT_DBL_MAX 1.3e+154 |
- | |
27 | #define __SQRT_DBL_MIN 2.3e-162 |
26 | #define __SQRT_DBL_MAX 1.3e+154 |
28 | 27 | #define __SQRT_DBL_MIN 2.3e-162 |
|
29 | double |
28 | |
30 | hypot(double x, double y) |
29 | double hypot(double x, double y) |
Line 31... | Line 30... | ||
31 | { |
30 | { |
32 | double abig = fabs(x), asmall = fabs(y); |
31 | double abig = fabs(x), asmall = fabs(y); |
33 | double ratio; |
- | |
34 | 32 | double ratio; |
|
Line 35... | Line 33... | ||
35 | /* Make abig = max(|x|, |y|), asmall = min(|x|, |y|). */ |
33 | |
36 | if (abig < asmall) |
34 | /* Make abig = max(|x|, |y|), asmall = min(|x|, |y|). */ |
37 | { |
35 | if (abig < asmall) { |
Line 51... | Line 49... | ||
51 | significant digits. Dividing ASMALL by ABIG scales them |
49 | significant digits. Dividing ASMALL by ABIG scales them |
52 | to a certain degree, so that accuracy is better. */ |
50 | to a certain degree, so that accuracy is better. */ |
Line 53... | Line 51... | ||
53 | 51 | ||
54 | if ((ratio = asmall / abig) > __SQRT_DBL_MIN && abig < __SQRT_DBL_MAX) |
52 | if ((ratio = asmall / abig) > __SQRT_DBL_MIN && abig < __SQRT_DBL_MAX) |
55 | return abig * sqrt(1.0 + ratio*ratio); |
53 | return abig * sqrt(1.0 + ratio * ratio); |
56 | else |
- | |
57 | { |
54 | else { |
58 | /* Slower but safer algorithm due to Moler and Morrison. Never |
55 | /* Slower but safer algorithm due to Moler and Morrison. Never |
59 | produces any intermediate result greater than roughly the |
56 | produces any intermediate result greater than roughly the |
60 | larger of X and Y. Should converge to machine-precision |
57 | larger of X and Y. Should converge to machine-precision |
Line 78... | Line 75... | ||
78 | 75 | ||
Line 79... | Line 76... | ||
79 | #ifdef TEST |
76 | #ifdef TEST |
Line 80... | Line -... | ||
80 | - | ||
81 | #include |
77 | |
82 | 78 | #include |
|
83 | int |
79 | |
84 | main(void) |
80 | int main(void) |
85 | { |
81 | { |
86 | printf("hypot(3, 4) =\t\t\t %25.17e\n", hypot(3., 4.)); |
82 | printf("hypot(3, 4) =\t\t\t %25.17e\n", hypot(3., 4.)); |