1
0
Fork 0
tsgames/build/assets/include/math.hpp

73 lines
1.3 KiB
C++

#pragma once
#include <math.h>
#include <type_traits.hpp>
#ifdef abs
#undef abs
#endif
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
namespace std {
template <typename T> T max(T a, T b) {
return a < b ? b : a;
}
template <typename T> T min(T a, T b) {
return a < b ? a : b;
}
template <typename T> T abs(T a) {
return a < 0 ? -a : a;
}
#define UNARY(fn) \
template <typename T> \
typename std::enable_if<std::is_convertible<T, double>::value, T>::type \
fn(T a) { return static_cast<T>(::fn(static_cast<double>(a))); }
#define UNARY_BOOL(fn) \
template <typename T> \
typename std::enable_if<std::is_convertible<T, double>::value, bool>::type \
fn(T a) { return ::fn(static_cast<double>(a)); }
#define BINARY(fn) \
template <typename T> \
typename std::enable_if<std::is_convertible<T, double>::value, T>::type \
fn(T a, T b) { return static_cast<T>(::fn(static_cast<double>(a), static_cast<double>(b))); }
UNARY(floor)
UNARY(ceil)
UNARY(round)
UNARY(trunc)
UNARY(exp)
UNARY(log)
UNARY(log10)
UNARY(sqrt)
UNARY(sin)
UNARY(cos)
UNARY(tan)
UNARY(asin)
UNARY(acos)
UNARY(atan)
UNARY(sinh)
UNARY(cosh)
UNARY(tanh)
UNARY_BOOL(isnan)
UNARY_BOOL(isinf)
BINARY(fmod)
BINARY(pow)
BINARY(atan2)
#undef UNARY
#undef UNARY_BOOL
#undef BINARY
} // namespace std