Thread: Definifng a Custom Arithmetic Type

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    23

    Definifng a Custom Arithmetic Type

    I want to create an unsigned arithmetic type that can hold a maximum size of 360. Without having to create a method.

    It should be in such a manner that:
    Code:
    typedef uint8_t radius;
    radius rotation = 0;
    
    radius foo (radius rotation)
    {    return --rotation;
    }
    returns 359, instead of 255, or 65535 or whatever max value the type I base my type on can hold.

    Any ideas on how to tackle this?
    Thanks.
    Last edited by skyliner; 06-26-2014 at 02:35 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you mean by "without having to create a method"?

    One way of doing this is to create a class. The class interface will restrict what values can be stored into an object of the class. You can overload the various arithmetic operators such that they behave as if your objects are of an unsigned integer type with a range [0, 360].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    23
    Thank you, the operator overload is a great idea. I just wanted to simplify things for my class users by avoiding calling a method on the object.It looks neater that way.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by skyliner View Post
    I want to create an unsigned arithmetic type that can hold a maximum size of 360. Without having to create a method.
    Why not simply change your idea of how many "degrees" are in a circle?

    Instead of degrees, you could use 1/256ths of a circle as your fundamental angular unit. Or if that's not fine-grained enough, go up to 16 bits and use 1/65536th as your fundamental unit. Now you can take advantage of the way the hardware wraps things and don't have to invent some weird type.

    Data can be translated to/from the traditional 1/360 degrees on its way in and out of the system.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by brewbuck View Post
    Why not simply change your idea of how many "degrees" are in a circle?

    Instead of degrees, you could use 1/256ths of a circle as your fundamental angular unit. Or if that's not fine-grained enough, go up to 16 bits and use 1/65536th as your fundamental unit. Now you can take advantage of the way the hardware wraps things and don't have to invent some weird type.

    Data can be translated to/from the traditional 1/360 degrees on its way in and out of the system.
    FWIW, I've always preferred mapping angles to some value between zero and one (floating point, of course). That approach can be used to simplify the manipulation of many other things as well, such as colors (RGB, HSV, etc), amplitude values for PCM (audio) data, etc, etc.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Sebastiani View Post
    FWIW, I've always preferred mapping angles to some value between zero and one (floating point, of course). That approach can be used to simplify the manipulation of many other things as well, such as colors (RGB, HSV, etc), amplitude values for PCM (audio) data, etc, etc.
    Yeah, floats can do this efficiently as well if you map things that way. They don't wrap automatically, but you just have to take the fractional part which isn't too expensive (and you can manually wrap them yourself at an opportune moment).

    EDIT: "fractional part" in this case means x - floor(x), not x - trunc(x) which is important if the value is negative
    Last edited by brewbuck; 06-26-2014 at 06:52 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    23
    Quote Originally Posted by brewbuck View Post
    Why not simply change your idea of how many "degrees" are in a circle?
    0.o. That's simple and brilliant, or: "simply brilliant" haha, Thanks. I went ahead with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-23-2011, 02:04 PM
  2. Using cin with a custom type
    By qxcdfg in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2008, 07:31 PM
  3. HUGE custom data type
    By fuzzypig in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:03 AM
  4. arithmetic on pointer to an incomplete type?
    By Volair in forum C Programming
    Replies: 4
    Last Post: 11-19-2006, 06:53 PM
  5. saving to a file with custom type (.lee or something)
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2002, 03:05 PM