constexpr
constexpr copied to clipboard
Preposterous proposition
Consider the following IEEEish assumption: The widest floating point type can't exactly represent a bigger range of integers than the widest integer type. (that is, if we exceed the limits of widest int we're in a zone that is definitely not precise enough to have a fractional part, since it's not even good enough for integers)
I understand that this is quite dubious, almost unacceptable and nearly heretical, but look at my glorious trunc!
template <typename IntMax = std::intmax_t, typename Float = float>
[[nodiscard]] constexpr
Float trunc(Float f)
{
return abs(f) <= std::numeric_limits<IntMax>::max()
? static_cast<IntMax>(f)
: f;
}