]>
Commit | Line | Data |
---|---|---|
a2a2059f BD |
1 | /* Implementation of various C99 functions |
2 | Copyright (C) 2004 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of the GNU Fortran 95 runtime library (libgfortran). | |
5 | ||
6 | Libgfortran is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | Libgfortran is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
17 | License along with libgfortran; see the file COPYING.LIB. If not, | |
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include <sys/types.h> | |
23 | #include <math.h> | |
24 | #include "libgfortran.h" | |
25 | ||
26 | ||
27 | /* Algorithm by Steven G. Kargl. */ | |
28 | ||
29 | #ifndef HAVE_ROUND | |
30 | /* Round to nearest integral value. If the argument is halfway between two | |
31 | integral values then round away from zero. */ | |
32 | ||
33 | double | |
34 | round(double x) | |
35 | { | |
36 | double t; | |
37 | int i; | |
38 | ||
39 | i = fpclassify(x); | |
40 | if (i == FP_INFINITE || i == FP_NAN) | |
41 | return (x); | |
42 | ||
43 | if (x >= 0.0) | |
44 | { | |
45 | t = ceil(x); | |
46 | if (t - x > 0.5) | |
47 | t -= 1.0; | |
48 | return (t); | |
49 | } | |
50 | else | |
51 | { | |
52 | t = ceil(-x); | |
53 | if (t + x > 0.5) | |
54 | t -= 1.0; | |
55 | return (-t); | |
56 | } | |
57 | } | |
58 | #endif | |
59 | ||
60 | #ifndef HAVE_ROUNDF | |
61 | /* Round to nearest integral value. If the argument is halfway between two | |
62 | integral values then round away from zero. */ | |
63 | ||
64 | float | |
65 | roundf(float x) | |
66 | { | |
67 | float t; | |
68 | int i; | |
69 | ||
70 | i = fpclassify(x); | |
71 | if (i == FP_INFINITE || i == FP_NAN) | |
72 | return (x); | |
73 | ||
74 | if (x >= 0.0) | |
75 | { | |
76 | t = ceilf(x); | |
77 | if (t - x > 0.5) | |
78 | t -= 1.0; | |
79 | return (t); | |
80 | } | |
81 | else | |
82 | { | |
83 | t = ceilf(-x); | |
84 | if (t + x > 0.5) | |
85 | t -= 1.0; | |
86 | return (-t); | |
87 | } | |
88 | } | |
89 | #endif | |
90 |