arg_router  1.4.0
C++ command line argument parsing and routing
math.hpp
1 // Copyright (C) 2022-2023 by Camden Mannett.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
4 
5 #pragma once
6 
7 #include <type_traits>
8 
11 {
19 template <typename T>
20 [[nodiscard]] constexpr T abs(T value) noexcept
21 {
22  static_assert(std::is_integral_v<T>, "T must be an integral");
23  if constexpr (std::is_signed_v<T>) {
24  return value < T{0} ? -value : value;
25  } else {
26  return value;
27  }
28 }
29 
37 template <typename T>
38 [[nodiscard]] constexpr T num_digits(T value) noexcept
39 {
40  static_assert(std::is_integral_v<T>, "T must be an integral");
41 
42  constexpr auto base = T{10};
43 
44  value = abs(value);
45  auto i = T{1};
46  while (value /= base) {
47  ++i;
48  }
49 
50  return i;
51 }
52 
61 template <auto Base, typename T>
62 [[nodiscard]] constexpr T pow(T exp) noexcept
63 {
64  static_assert(std::is_integral_v<T>, "T must be an integral");
65  static_assert(Base > 0, "Base must be greater than zero");
66 
67  return exp <= T{0} ? T{1} : Base * pow<Base>(exp - T{1});
68 }
69 } // namespace arg_router::math
constexpr T num_digits(T value) noexcept
Definition: math.hpp:38
constexpr T pow(T exp) noexcept
Definition: math.hpp:62
constexpr T abs(T value) noexcept
Definition: math.hpp:20