Skip to content

Commit 66d3f6d

Browse files
committed
Added the option to add quadmath functions to namespace std:: (disabled by default since it is Undefined Behaviour)
1 parent 1ac968e commit 66d3f6d

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@
33
`quadmath_cpp` also includes functions not present in `<quadmath.h>`, such as `islessgreater(x, y)` and `fpclassify(x)` from C++11, in addition to a few C23 functions like `iseqsig(x, y)`.
44

55
Include the `quadmath_cpp.h` header in your code to use it. The header should compile fine on GCC from C++98 to C++23
6+
7+
***
8+
9+
If you want to bring `quadmath_cpp` into the `std::` namespace, define `QUADMATH_CPP_NAMESPACE_STD` to non-zero before including the header.
10+
```c++
11+
#define QUADMATH_CPP_NAMESPACE_STD 1
12+
#include "quadmath_cpp.h"
13+
```
14+
This will allow you to call `std::sin(x)` and `std::pow(x, y)`. However, extending the `std::` namespace is undefined behaviour, so take caution with this feature.

quadmath_cpp.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
** Author: zerico2005 (2024 - 2025)
3-
** Project: quadmath_cpp
4-
** License: MIT License
5-
** A copy of the MIT License should be included with
6-
** this project. If not, see https://opensource.org/license/MIT
2+
** Author: zerico2005 (2024 - 2025)
3+
** Project: quadmath_cpp
4+
** License: MIT License
5+
** A copy of the MIT License should be included with
6+
** this project. If not, see https://opensource.org/license/MIT
77
*/
88

99
#ifndef QUADMATH_CPP_H
@@ -13,13 +13,22 @@
1313
// <quadmath.h> overloads
1414
//------------------------------------------------------------------------------
1515

16+
#ifndef QUADMATH_CPP_NAMESPACE_STD
17+
/* disabled by default */
18+
#define QUADMATH_CPP_NAMESPACE_STD 0
19+
#endif
20+
1621
#include <quadmath.h>
1722

1823
#if __cplusplus >= 201103L
1924
/* for fpclassify return values */
2025
#include <cmath>
2126
#endif
2227

28+
#if QUADMATH_CPP_NAMESPACE_STD
29+
namespace std {
30+
#endif
31+
2332
/* Classification */
2433

2534
inline bool signbit(__float128 x) { return (signbitq(x) != 0); }
@@ -174,4 +183,8 @@ inline __float128 erfc(__float128 x) { return erfcq(x); }
174183
inline __float128 lgamma(__float128 x) { return lgammaq(x); }
175184
inline __float128 tgamma(__float128 x) { return tgammaq(x); }
176185

186+
#if QUADMATH_CPP_NAMESPACE_STD
187+
}
188+
#endif
189+
177190
#endif /* QUADMATH_CPP_H */

test/src/main.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
** Author: zerico2005 (2024 - 2025)
3-
** Project: quadmath_cpp
4-
** License: MIT License
5-
** A copy of the MIT License should be included with
6-
** this project. If not, see https://opensource.org/license/MIT
2+
** Author: zerico2005 (2024 - 2025)
3+
** Project: quadmath_cpp
4+
** License: MIT License
5+
** A copy of the MIT License should be included with
6+
** this project. If not, see https://opensource.org/license/MIT
77
*/
88

99
#include <cassert>
@@ -17,6 +17,7 @@
1717

1818
#include <quadmath.h>
1919

20+
#define QUADMATH_CPP_NAMESPACE_STD 0
2021
#include "../../quadmath_cpp.h"
2122

2223
void basic_test() {

0 commit comments

Comments
 (0)