Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

stravager/in_range_ext

Open more actions menu

Repository files navigation

in_range_ext/in_range_ext.h

This header provides an equivalent to C++20's std::in_range<integer>(value) for floating-point values.

It defines the function

namespace in_range_ext {
  template<integer I, std::floating_point F> constexpr bool in_range(F f);
}

which returns true iff the floating-point value f is in range for integer type I.

integer is a concept matching std::integral types other than character types and bool (as for std::in_range).

For this one function, most of the work is in finding, at compile time, the highest and lowest floating-point values in the range of the integer type. This cannot be done by direct conversion in the general case in C++ because of rounding. For example on a typical 32-/64-bit system with

std::numeric_limits<int>::digits   31
std::numeric_limits<int>::max()    0x7fffffff (2^31 - 1)
std::numeric_limits<float>::radix  2
std::numeric_limits<float>::digts  24

float(0x7fffffff) rounded to nearest is 0x80000000 (2^31), just outside int range.

Instead, the code establishes these boundaries "manually" using a decomposed floating-point representation.

It is designed to work for arbitrary integer width, floating-point radix, and precision, including the common cases where the floating-point range is much larger, and less common ones where it may be smaller, for example IEEE half-precision aka binary16.

About

C++ std::in_range equivalent for floating-point values

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.