timespec_get: New module.
[gnulib.git] / lib / logb.c
blobeee9253092f35a434809aac767de9ac696ecb139
1 /* Floating-point exponent.
2 Copyright (C) 2012-2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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 General Public License for more details.
14 You should have received a copy of the GNU 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 #ifdef USE_LONG_DOUBLE
25 # define LOGB logbl
26 # define DOUBLE long double
27 # define L_(literal) literal##L
28 # define HUGEVAL HUGE_VALL
29 # define FREXP frexpl
30 # define ISNAN isnanl
31 #elif ! defined USE_FLOAT
32 # define LOGB logb
33 # define DOUBLE double
34 # define L_(literal) literal
35 # define HUGEVAL HUGE_VAL
36 # define FREXP frexp
37 # define ISNAN isnand
38 #else /* defined USE_FLOAT */
39 # define LOGB logbf
40 # define DOUBLE float
41 # define L_(literal) literal##f
42 # define HUGEVAL HUGE_VALF
43 # define FREXP frexpf
44 # define ISNAN isnanf
45 #endif
47 DOUBLE
48 LOGB (DOUBLE x)
50 if (isfinite (x))
52 if (x == L_(0.0))
53 /* Return -Infinity. */
54 return - HUGEVAL;
55 else
57 int e;
59 (void) FREXP (x, &e);
60 return (DOUBLE) (e - 1);
63 else
65 if (ISNAN (x))
66 return x; /* NaN */
67 else
68 /* Return +Infinity. */
69 return HUGEVAL;