Skip to content

Commit

Permalink
feat: std::numeric_limits support added
Browse files Browse the repository at this point in the history
Resolves #408
  • Loading branch information
mpusz committed Feb 5, 2025
1 parent b8855dc commit 8843982
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ add_mp_units_module(
include/mp-units/concepts.h
include/mp-units/core.h
include/mp-units/framework.h
include/mp-units/limits.h
MODULE_INTERFACE_UNIT mp-units-core.cpp
)

Expand Down
1 change: 1 addition & 0 deletions src/core/include/mp-units/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <mp-units/compat_macros.h>
#include <mp-units/concepts.h>
#include <mp-units/framework.h>
#include <mp-units/limits.h>

#if MP_UNITS_HOSTED
#include <mp-units/cartesian_vector.h>
Expand Down
154 changes: 154 additions & 0 deletions src/core/include/mp-units/limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include <mp-units/bits/module_macros.h>
#include <mp-units/framework/quantity.h>
#include <mp-units/framework/quantity_point.h>

#ifndef MP_UNITS_IN_MODULE_INTERFACE
#ifdef MP_UNITS_IMPORT_STD
import std;
#else
#include <limits>
#endif // MP_UNITS_IMPORT_STD
#endif // MP_UNITS_IN_MODULE_INTERFACE

template<auto R, typename Rep>
requires requires { typename std::numeric_limits<Rep>; }
class std::numeric_limits<mp_units::quantity<R, Rep>> : public std::numeric_limits<Rep> {
public:
static constexpr mp_units::quantity<R, Rep> min() noexcept
requires requires { mp_units::quantity<R, Rep>::min(); }
{
return mp_units::quantity<R, Rep>::min();
}

static constexpr mp_units::quantity<R, Rep> max() noexcept
requires requires { mp_units::quantity<R, Rep>::max(); }
{
return mp_units::quantity<R, Rep>::max();
}

static constexpr mp_units::quantity<R, Rep> lowest() noexcept
requires requires { std::numeric_limits<Rep>::lowest(); }
{
return {std::numeric_limits<Rep>::lowest(), R};
}

static constexpr mp_units::quantity<R, Rep> epsilon() noexcept
requires requires { std::numeric_limits<Rep>::epsilon(); }
{
return {std::numeric_limits<Rep>::epsilon(), R};
}

static constexpr mp_units::quantity<R, Rep> round_error() noexcept
requires requires { std::numeric_limits<Rep>::round_error(); }
{
return {std::numeric_limits<Rep>::round_error(), R};
}

static constexpr mp_units::quantity<R, Rep> infinity() noexcept
requires requires { std::numeric_limits<Rep>::infinity(); }
{
return {std::numeric_limits<Rep>::infinity(), R};
}

static constexpr mp_units::quantity<R, Rep> quiet_NaN() noexcept
requires requires { std::numeric_limits<Rep>::quiet_NaN(); }
{
return {std::numeric_limits<Rep>::quiet_NaN(), R};
}

static constexpr mp_units::quantity<R, Rep> signaling_NaN() noexcept
requires requires { std::numeric_limits<Rep>::signaling_NaN(); }
{
return {std::numeric_limits<Rep>::signaling_NaN(), R};
}

static constexpr mp_units::quantity<R, Rep> denorm_min() noexcept
requires requires { std::numeric_limits<Rep>::denorm_min(); }
{
return {std::numeric_limits<Rep>::denorm_min(), R};
}
};


template<auto R, auto PO, typename Rep>
requires requires { typename std::numeric_limits<Rep>; }
class std::numeric_limits<mp_units::quantity_point<R, PO, Rep>> : public std::numeric_limits<Rep> {
public:
static constexpr mp_units::quantity_point<R, PO, Rep> min() noexcept
requires requires { mp_units::quantity_point<R, PO, Rep>::min(); }
{
return mp_units::quantity_point<R, PO, Rep>::min();
}

static constexpr mp_units::quantity_point<R, PO, Rep> max() noexcept
requires requires { mp_units::quantity_point<R, PO, Rep>::max(); }
{
return mp_units::quantity_point<R, PO, Rep>::max();
}

static constexpr mp_units::quantity_point<R, PO, Rep> lowest() noexcept
requires requires { std::numeric_limits<mp_units::quantity<R, Rep>>::lowest(); }
{
return {std::numeric_limits<mp_units::quantity<R, Rep>>::lowest(), PO};
}

static constexpr mp_units::quantity_point<R, PO, Rep> epsilon() noexcept
requires requires { std::numeric_limits<mp_units::quantity<R, Rep>>::epsilon(); }
{
return {std::numeric_limits<mp_units::quantity<R, Rep>>::epsilon(), PO};
}

static constexpr mp_units::quantity_point<R, PO, Rep> round_error() noexcept
requires requires { std::numeric_limits<mp_units::quantity<R, Rep>>::round_error(); }
{
return {std::numeric_limits<mp_units::quantity<R, Rep>>::round_error(), PO};
}

static constexpr mp_units::quantity_point<R, PO, Rep> infinity() noexcept
requires requires { std::numeric_limits<mp_units::quantity<R, Rep>>::infinity(); }
{
return {std::numeric_limits<mp_units::quantity<R, Rep>>::infinity(), PO};
}

static constexpr mp_units::quantity_point<R, PO, Rep> quiet_NaN() noexcept
requires requires { std::numeric_limits<mp_units::quantity<R, Rep>>::quiet_NaN(); }
{
return {std::numeric_limits<mp_units::quantity<R, Rep>>::quiet_NaN(), PO};
}

static constexpr mp_units::quantity_point<R, PO, Rep> signaling_NaN() noexcept
requires requires { std::numeric_limits<mp_units::quantity<R, Rep>>::signaling_NaN(); }
{
return {std::numeric_limits<mp_units::quantity<R, Rep>>::signaling_NaN(), PO};
}

static constexpr mp_units::quantity_point<R, PO, Rep> denorm_min() noexcept
requires requires { std::numeric_limits<mp_units::quantity<R, Rep>>::denorm_min(); }
{
return {std::numeric_limits<mp_units::quantity<R, Rep>>::denorm_min(), PO};
}
};
3 changes: 2 additions & 1 deletion src/core/include/mp-units/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ template<auto R1, typename Rep1, auto R2, typename Rep2>
*/
template<Representation Rep, Reference R>
requires requires { std::numeric_limits<Rep>::epsilon(); }
[[nodiscard]] constexpr quantity<R{}, Rep> epsilon(R r) noexcept
[[deprecated("Use `std::numeric_limits<Quantity>::epsilon()` instead")]] [[nodiscard]] constexpr quantity<R{}, Rep>
epsilon(R r) noexcept
{
return {static_cast<Rep>(std::numeric_limits<Rep>::epsilon()), r};
}
Expand Down
14 changes: 0 additions & 14 deletions test/runtime/math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,6 @@ TEST_CASE("math operations", "[math]")
}
}

SECTION("numeric_limits functions")
{
SECTION("'epsilon' works as expected using default floating type")
{
REQUIRE(epsilon<double>(isq::length[m]).numerical_value_in(m) ==
std::numeric_limits<decltype(1. * isq::length[m])::rep>::epsilon());
}
SECTION("'epsilon' works as expected using integers")
{
REQUIRE(epsilon<int>(isq::length[m]).numerical_value_in(m) ==
std::numeric_limits<decltype(1 * isq::length[m])::rep>::epsilon());
}
}

SECTION("floor functions")
{
SECTION("floor 1 second with target unit second should be 1 second")
Expand Down
3 changes: 2 additions & 1 deletion test/static/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ add_library(
iec_test.cpp
imperial_test.cpp
international_test.cpp
isq_test.cpp
isq_angle_test.cpp
isq_test.cpp
limits_test.cpp
natural_test.cpp
prime_test.cpp
quantity_point_test.cpp
Expand Down
Loading

0 comments on commit 8843982

Please sign in to comment.