openat: don’t close (-1)
[gnulib.git] / lib / ilogb.c
blobbaae4e9789826b592239dcaee4acb554d4121117
1 /* Floating-point exponent.
2 Copyright (C) 2012-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #if ! (defined USE_LONG_DOUBLE || defined USE_FLOAT)
18 # include <config.h>
19 #endif
21 /* Specification. */
22 #include <math.h>
24 #include <limits.h>
26 #ifdef USE_LONG_DOUBLE
27 # define ILOGB ilogbl
28 # define DOUBLE long double
29 # define L_(literal) literal##L
30 # define FREXP frexpl
31 # define ISNAN isnanl
32 #elif ! defined USE_FLOAT
33 # define ILOGB ilogb
34 # define DOUBLE double
35 # define L_(literal) literal
36 # define FREXP frexp
37 # define ISNAN isnand
38 #else /* defined USE_FLOAT */
39 # define ILOGB ilogbf
40 # define DOUBLE float
41 # define L_(literal) literal##f
42 # define FREXP frexpf
43 # define ISNAN isnanf
44 #endif
46 int
47 ILOGB (DOUBLE x)
49 if (isfinite (x))
51 if (x == L_(0.0))
52 return FP_ILOGB0;
53 else
55 int e;
57 (void) FREXP (x, &e);
58 return e - 1;
61 else
63 if (ISNAN (x))
64 return FP_ILOGBNAN;
65 else
66 return INT_MAX;