OLD | NEW |
1 /* | 1 /* |
2 This file is part of LilyPond, the GNU music typesetter. | 2 This file is part of LilyPond, the GNU music typesetter. |
3 | 3 |
4 Copyright (C) 1997--2020 Han-Wen Nienhuys <hanwen@xs4all.nl> | 4 Copyright (C) 1997--2020 Han-Wen Nienhuys <hanwen@xs4all.nl> |
5 | 5 |
6 LilyPond is free software: you can redistribute it and/or modify | 6 LilyPond is free software: you can redistribute it and/or modify |
7 it under the terms of the GNU General Public License as published by | 7 it under the terms of the GNU General Public License as published by |
8 the Free Software Foundation, either version 3 of the License, or | 8 the Free Software Foundation, either version 3 of the License, or |
9 (at your option) any later version. | 9 (at your option) any later version. |
10 | 10 |
11 LilyPond is distributed in the hope that it will be useful, | 11 LilyPond is distributed in the hope that it will be useful, |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 GNU General Public License for more details. | 14 GNU General Public License for more details. |
15 | 15 |
16 You should have received a copy of the GNU General Public License | 16 You should have received a copy of the GNU General Public License |
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>. | 17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>. |
18 */ | 18 */ |
19 | 19 |
20 #ifndef REAL_HH | 20 #ifndef REAL_HH |
21 #define REAL_HH | 21 #define REAL_HH |
22 | 22 |
23 // Needed because of extension definitions for POSIX functions. | 23 // Needed because of extension definitions for POSIX functions. |
24 #include "config.hh" | 24 #include "config.hh" |
25 | 25 |
26 #include <algorithm> | 26 #include <algorithm> |
27 #include <climits> | 27 #include <climits> |
28 #include <cmath> | 28 #include <cmath> |
| 29 #include <cstdlib> |
29 | 30 |
30 typedef double Real; | 31 typedef double Real; |
31 extern const Real infinity_f; | 32 static constexpr Real infinity_f = INFINITY; |
32 | 33 |
33 /* namespace std { */ | 34 using std::abs; // TODO: replace abs (x) with std::abs (x) and remove this |
34 | |
35 template<class T> inline T abs (T x) | |
36 { | |
37 return x > 0 ? x : -x; | |
38 } | |
39 | |
40 /* } namespace std */ | |
41 | 35 |
42 inline Real | 36 inline Real |
43 distance (Real x, Real y) | 37 distance (Real x, Real y) |
44 { | 38 { |
45 return abs (x - y); | 39 return std::abs (x - y); |
46 } | 40 } |
47 | 41 |
48 template<class T> inline int sign (T x) | 42 template<class T> |
| 43 inline int |
| 44 sign (T x) |
49 { | 45 { |
50 if (x) | 46 if (x != T (0)) |
51 return x > T (0) ? 1 : -1; | 47 return std::signbit (x) ? -1 : 1; |
52 return 0; | 48 return 0; |
53 } | 49 } |
54 | 50 |
55 template<class T> inline T sqr (T x) | 51 template<class T> |
| 52 constexpr auto |
| 53 sqr (T x)->decltype (x *x) |
56 { | 54 { |
57 return x * x; | 55 return x * x; |
58 } | 56 } |
59 | 57 |
60 #endif | 58 #endif |
OLD | NEW |