| 298 | // Converts a value to the nearest integer. |
| 299 | template <typename X, typename Out = int> |
| 300 | Out round(X x) |
| 301 | { |
| 302 | static_assert(!std::is_integral<X>::value, "type must be non-integral"); |
| 303 | static_assert(std::is_integral<Out>::value, "type must be integral"); |
| 304 | if (static_cast<double>(x) < static_cast<double>(std::numeric_limits<Out>::lowest())) |
| 305 | return std::numeric_limits<Out>::lowest(); |
| 306 | if (static_cast<double>(x) > static_cast<double>(std::numeric_limits<Out>::max())) |
| 307 | return std::numeric_limits<Out>::max(); |
| 308 | if (is_negative(x)) |
| 309 | x -= 1; |
| 310 | return static_cast<Out>(x + 0.5); |
| 311 | } |
| 312 | |
| 313 | // API search type: floor : a -> b |
| 314 | // fwd bind count: 0 |