Skip to content

Commit

Permalink
Introduce min/max observers for Number
Browse files Browse the repository at this point in the history
Three static member functions are introduced with
definitions consistent with std::numeric_limits:

static constexpr Number min() noexcept;

  Returns: The minimum positive value.  This is the value closest to zero.

static constexpr Number max() noexcept;

  Returns: The maximum possible value.

static constexpr Number lowest() noexcept;

  Returns: The negative value which is less than all other values.
  • Loading branch information
HowardHinnant committed Feb 7, 2023
1 parent c954a85 commit 4dc0206
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/ripple/basics/Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class Number
Number&
operator/=(Number const& x);

static constexpr Number
min() noexcept;
static constexpr Number
max() noexcept;
static constexpr Number
lowest() noexcept;

explicit operator XRPAmount() const; // round to nearest, even on tie
explicit operator rep() const; // round to nearest, even on tie

Expand Down Expand Up @@ -290,6 +297,24 @@ operator/(Number const& x, Number const& y)
return z;
}

inline constexpr Number
Number::min() noexcept
{
return Number{minMantissa, minExponent, unchecked{}};
}

inline constexpr Number
Number::max() noexcept
{
return Number{maxMantissa, maxExponent, unchecked{}};
}

inline constexpr Number
Number::lowest() noexcept
{
return -Number{maxMantissa, maxExponent, unchecked{}};
}

inline constexpr bool
Number::isnormal() const noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/basics/impl/IOUAmount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ IOUAmount::normalize()

if (*stNumberSwitchover)
{
const Number v{mantissa_, exponent_};
Number const v{mantissa_, exponent_};
mantissa_ = v.mantissa();
exponent_ = v.exponent();
if (exponent_ > maxExponent)
Expand Down

0 comments on commit 4dc0206

Please sign in to comment.