annotate lib/fmod.c @ 16511:2074f2bf7216

math code: Add comments. * lib/acosl.c: Add comment about related glibc source files. * lib/asinl.c: Likewise. * lib/atanl.c: Likewise. * lib/expl.c: Likewise. * lib/logl.c: Likewise. * lib/sincosl.c: Likewise. * lib/sinl.c: Likewise. * lib/tanl.c: Likewise. * lib/trigl.c: Likewise. * lib/cosl.c: Likewise. Fix comments.
author Bruno Haible <bruno@clisp.org>
date Wed, 29 Feb 2012 12:30:07 +0100
parents 8b3ead70232c
children 0badbd4d62e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16486
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Remainder.
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2011-2012 Free Software Foundation, Inc.
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #include <config.h>
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /* Specification. */
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <math.h>
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 double
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 fmod (double x, double y)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 {
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 if (isinf (y))
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 return x;
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 else
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 {
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 double q = - trunc (x / y);
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 double r = fma (q, y, x); /* = x + q * y, computed in one step */
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 /* Correct possible rounding errors in the quotient x / y. */
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 if (y >= 0)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 {
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 if (x >= 0)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 {
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 /* Expect 0 <= r < y. */
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 if (r < 0)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 q += 1, r = fma (q, y, x);
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 else if (r >= y)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 q -= 1, r = fma (q, y, x);
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 }
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 else
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 {
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 /* Expect - y < r <= 0. */
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 if (r > 0)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 q -= 1, r = fma (q, y, x);
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 else if (r <= - y)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 q += 1, r = fma (q, y, x);
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 }
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 }
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 else
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 {
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 if (x >= 0)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 {
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 /* Expect 0 <= r < - y. */
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 if (r < 0)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 q -= 1, r = fma (q, y, x);
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 else if (r >= - y)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 q += 1, r = fma (q, y, x);
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 }
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 else
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 {
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 /* Expect y < r <= 0. */
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 if (r > 0)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 q += 1, r = fma (q, y, x);
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 else if (r <= y)
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 q -= 1, r = fma (q, y, x);
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 }
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 }
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 return r;
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 }
8b3ead70232c fmod-ieee: Work around test failures on OSF/1, mingw.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 }