Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 871705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:40:49+00:00 2026-05-15T10:40:49+00:00

I have a method in C# as follows (which wraps a number across a

  • 0

I have a method in C# as follows (which wraps a number across a range, say 0 to 360… if you pass 0-359 you get the same value, if you pass 360 you get 0, 361 gets 1, etc.):

    /// <summary>
    /// Wraps the value across the specified boundary range.
    /// 
    /// If the value is in the range <paramref name="min"/> (inclusive) to <paramref name="max"/> (exclusive),
    /// <paramref name="value"/> will be returned. If <paramref name="value"/> is equal to <paramref name="max"/>,
    /// <paramref name="min"/> will be returned. The method essentially creates a loop between <paramref name="min"/>
    /// and <paramref name="max"/>.
    /// </summary>
    /// <param name="value">The value to wrap.</param>
    /// <param name="min">The minimum value of the boundary range, inclusive.</param>
    /// <param name="max">The maximum value of the boundary range, exclusive.</param>
    /// <returns>The value wrapped across the specified range.</returns>
    public static T Wrap<T>(T value, T min, T max) where T : IComparable<T>
    {
        // If it's positive or negative infinity, we just return the minimum, which is the "origin"
        bool infinityDouble = typeof(T) == typeof(double) && (double.IsPositiveInfinity(Convert.ToDouble(value)) || double.IsNegativeInfinity(Convert.ToDouble(value)));
        bool infinityFloat = typeof(T) == typeof(float) && (float.IsPositiveInfinity(Convert.ToSingle(value)) || float.IsNegativeInfinity(Convert.ToSingle(value)));
        if (infinityDouble || infinityFloat)
        {
            return min;
        }

        // If the value is between the origin (inclusive) and the maximum value (exclusive), just return the value
        if (value.CompareTo(min) >= 0 && value.CompareTo(max) < 0)
        {
            return value;
        }

        // The range of the wrapping function
        var range = (dynamic)max - (dynamic)min;

        return ((((value % range) + range) - min) % range) + min;
    }

I also needed this method in C++, which I defined as follows:

/*!
    Wraps the value across the specified boundary range.

    If the value is in the range \a min (inclusive) to \a max (exclusive), \a value will be returned.
    If \a value is equal to \a max, \a min will be returned. The method essentially creates a loop between
    \a min and \a max.

    \param value The value to wrap.
    \param min The minimum value of the boundary range, inclusive.
    \param max The maximum value of the boundary range, exclusive.
    \return The value wrapped across the specified range.
 */
template <typename T> const T& MathHelper::wrap(const T &value, const T &min, const T &max)
{
    // If it's positive or negative infinity, we just return the minimum, which is the "origin"
    bool infinityDouble = value == std::numeric_limits<double>::infinity() || value == -std::numeric_limits<double>::infinity();
    bool infinityFloat = value == std::numeric_limits<float>::infinity() || value == -std::numeric_limits<float>::infinity();
    if (infinityDouble || infinityFloat)
    {
        return min;
    }

    // If the value is between the origin (inclusive) and the maximum value (exclusive), just return the value
    if (value >= min && value < max)
    {
        return value;
    }

    // The range of the wrapping function
    T range = max - min;

    return ((((value % range) + range) - min) % range) + min;
}

Now my question is: am I checking for infinity correctly in the C++ version? I can’t see any way to say “if double, do these checks, if float, do these checks”. If it’s not the type I want, will it just return false? Also, why is the % operator not defined for float and double? I guess I’ll have to implement the modulo operator myself. The method is pretty much intended for numeric types – byte, short, int, long, float, double.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:40:50+00:00Added an answer on May 15, 2026 at 10:40 am

    With the facilities provided by numeric_limits, you don’t really need to use any complex specializations or anything like that for the infinity check.

    template <typename T> 
    const T& MathHelper::wrap(const T &value, const T &min, const T &max) {
      bool isInfinity = std::numeric_limits<T>::has_infinity() 
                     && (std::abs(value) == std::numeric_limits<T>::infinity());
      //the rest
    }
    

    Your final step, involving operator% will be more complicated. You will need to provide a custom mod function, that is overloaded to pass the floating point types into std::modf instead of using operator%. You might be able to use type traits [either via boost or TR1] to minimize the repetative aspects of this, although I’m not sure what the most elegant method of doing so would be. Perhaps something along the lines of:

    template<typename T>
    typename std::enable_if<std::is_floating_point<T>::value, T>::type mod(T, T) {
       //use std::modf
    }
    
    template<typename T>
    typename std::enable_if<std::is_integral<T>::value, T>::type mod(T, T) {
       //use %
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller with an action method as follows: public class InventoryController :
I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method in .NET (C#) which returns string[][] . When using RegAsm
I have a number of class, all with exactly the same interface. This interface
I have an sql scenario as follows which I have been trying to improve.
I have an abstract Catalog class as follows. It has a static method OpenCatalog()
I have a method as follows: Public Sub Send() Dim caughtException As Exception =
I if have a method signature as follows public void deposit(@RequestParam(accountId) Integer accountId, @RequestParam(amount)
I have a reference number of the following type DAA76647.1 which I want to
So I have a method which is supposed to check if a table exists

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.