7

I'm wondering if it's possible to define a custom data type which can only take a value between -3.1415926535897932 and 3.1415926535897931.

The idea is that a value below or above the range would automatically "wrap-around", eliminating the need to write code to do the conversion and also eliminating the possibility of error somewhere.

A.B.
  • 15,364
  • 3
  • 61
  • 64
  • 2
    what do you mean "wrap around"? – Neoh Jun 28 '13 at 15:05
  • 2
    Why do you think you cannot define that data type? – Vincenzo Pii Jun 28 '13 at 15:05
  • 1
    Of course it is possible to define a custom data type (a _class_ or a _struct_, in particular) with these properties. Why not? – Daniel Daranas Jun 28 '13 at 15:06
  • 3
    It would be better to know what you're trying to accomplish here. – Captain Skyhawk Jun 28 '13 at 15:06
  • By "wrap around" I mean for example the behavior of an unsigned int. When set to -1, it "wraps around" and is instead set to its max value. – A.B. Jun 28 '13 at 15:09
  • 2
    This will not eliminate the possibility of error somewhere. It may increase errors, because every operation using a fixed-precision approximation of π introduces another error. You should answer Captain Skyhawk’s question about your goal, since there may be better ways to work with your data than this hack. – Eric Postpischil Jun 28 '13 at 15:24
  • 2
    This is not a duplicate. This is a C++ question about defining a custom data type to achieve a task. The other is a C question about defining a function to achieve a task. Answers to this question might use things learned from the C question, but they will not be exactly the same. – cgmb Jun 28 '13 at 16:34
  • Do you want something like this: http://www.codeproject.com/Articles/190833/Circular-Values-Math-and-Statistics-with-Cplusplus ? –  Jun 28 '13 at 21:11
  • 1
    One solution (I heard) is to map 0 - 2pi to a 32 integer, where 2pi corresponds to 2^32. You can then subtract/add, and the normal integer overflow will do exactly what you want. –  Jun 29 '13 at 15:20

1 Answers1

0

Yes it is possible. In the method that sets the value check to see if it outside the limits and if so do whatever operation you want to do to force it to be inside the limits. fmod is one good choice for operation.

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52