annotate lib/hypotl.c @ 17342:c75939cb6254

merge with default branch
author Michael Goffioul <michael.goffioul@gmail.com>
date Thu, 21 Feb 2013 14:57:31 +0000
parents e542fd46ad6f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16519
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Hypotenuse of a right-angled triangle.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16519
diff changeset
2 Copyright (C) 2012-2013 Free Software Foundation, Inc.
16519
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2012. */
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <math.h>
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 long double
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 hypotl (long double x, long double y)
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 {
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 return hypot (x, y);
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 }
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #else
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 long double
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 hypotl (long double x, long double y)
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 {
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 if (isfinite (x) && isfinite (y))
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 {
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 /* Determine absolute values. */
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 x = fabsl (x);
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 y = fabsl (y);
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 {
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 /* Find the bigger and the smaller one. */
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 long double a;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 long double b;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 if (x >= y)
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 {
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 a = x;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 b = y;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 }
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 else
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 {
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 a = y;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 b = x;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 }
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 /* Now 0 <= b <= a. */
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 {
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 int e;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 long double an;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 long double bn;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 /* Write a = an * 2^e, b = bn * 2^e with 0 <= bn <= an < 1. */
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 an = frexpl (a, &e);
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 bn = ldexpl (b, - e);
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 {
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 long double cn;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 /* Through the normalization, no unneeded overflow or underflow
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 will occur here. */
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 cn = sqrtl (an * an + bn * bn);
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 return ldexpl (cn, e);
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 }
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 }
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 }
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 }
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 else
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 {
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 if (isinf (x) || isinf (y))
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 /* x or y is infinite. Return +Infinity. */
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 return HUGE_VALL;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 else
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 /* x or y is NaN. Return NaN. */
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 return x + y;
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 }
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 }
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90
52c75970f518 New module 'hypotl'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 #endif