I tried using the constrained fucntion but it seems very limited in that i can't perform any arithmetic fucntions on it, and it doesn't like using an int as a data type.
basically i want to have 2 variables, lets say Value1 & Value2. I want each f these values to be limited to 0-359.
Now i want to be able to do some subtractions
e.g. Value1 = 10 / Value2 = 7
Value1 - Value2 = 3
now lets say values are a bit different.
Value1 = 0 / Value2 = 358
Value1 - Value2 = -2
(basically speaking i want the subtraction to count backwards, and when it goes past 0, to cycle back to 359 counting each step as it goes so instead of the value being -358 i want it to be -2
I want to contrain both of those numbers to bein in a range of 0-359 and have the numbers rollover.
i want to compare the 2 numbers and work out the difference, so that if value1 is 0 and value2 is 358 then the difference between them is actually actually -2
now if i did it the other way round and had value1 as 358 and value2 as 0 then the differencewould be +2
it's important to keep the +/- values of the numbers.
How can the device (or the language) know that 358 - 0 == -2, and indeed, if you have one value 0, and you slowly increment another value from 0 to 358, at what point does the difference go negative?
I know what you're trying to say. However, the language won't be able to supply this logic directly. You will have to get close by implementing a class, and then overloading all of the required operators, to achieve something close.
just thought it would be easy to define a range of numbers 0 to 359 and then have the numbers roll on in the same fashion as an intege(r)
That's just a side-effect of binary arithmetic, the same way as a car's odometer flipping over after (n) thousand miles is side-effect of decimal arithmetic.
Integer arithmetic is always implicitly modulo 2^n with n=8,16,32,...
To have the same effect with 360, you can explicitly use %360 after your other calculation (although the performance won't be very good because of the additional division):
int a=100;
int b=(a+300) % 360; // b=40
int c=(a-200) % 360; // c=260
that looks exactly the type of thing i need. I'll give it a try hopefully tomorrow when my replacement arduino chip arrives as my other one died last night.
Integer arithmetic is always implicitly modulo 2^n with n=8,16,32,...
Really? I thought C didn't define the representation of its integers - most modern hardware happens to use 2-s complement arithmetic, but I was under the impression that's not in the C definition.
In practice these days everything is 2's complement of course, so its only an issue with exotic legacy hardware.
Don't get me started on the brokenness of the % operator for negative values BTW!