defining an number with a set range.

Hi all,

i'm tryin to define an intiger with a defined range of 0-360. when i defineit i want it to rollover the numver count like an int.

e.g. if it's on 360 and does a count +1 it goes to 0 and if i do a count -1 it goes to 359.

Any pointers would be greatley appreciated.

thanks
Rich.

Modulo arithmetic
http://arduino.cc/en/Reference/Modulo

Pop an if statement in there after a number has been added to the previous value

if(val > 360)
{
val = 0;
}

This library takes care of it:
http://www.arduino.cc/playground/Code/Constrain

:slight_smile:

Hi AlphaBeta,

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

Rich.

Let me try and explain a bit clearer.

I want to have 2 values. Value1 & Value2

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.

Thanks in advance for any help.

Rich.

You'll have to code that logic yourself.

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.

class Periodic {
    int value;
    int period;
    Periodic(int periodicity) { this->period = periodicity; }
};
operator =(Periodic& lhs, int value) { lhs.value = value % lhs.period; };
operator +(Periodic& lhs, Periodic& rhs) { ... };
operator -(Periodic& lhs, Periodic& rhs) { ... };
operator *(Periodic& lhs, Periodic& rhs) { ... };
operator /(Periodic& lhs, Periodic& rhs) { ... };
operator ++(Periodic& lhs) { ... };
operator ++(Periodic& lhs, int postop) { ... };
operator --(Periodic& lhs) { ... };
operator --(Periodic& lhs, int postop) { ... };

And so on.

Wow this is starting to get way over my head.

I 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

Ok thanks for peoples help, back to the drawing board.

Rich.

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.

I'll maybe play around with the numbers and see if i can avoid the 0 value by defining the start number in the half way between at 180.

Rich.

Hi ribuck,

AWOL was pointing you at the right place:

Modulo arithmetic
http://arduino.cc/en/Reference/Modulo

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

MikeT

Hi Mike,

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.

Thanks, really appreciate it

Rich.

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!

for negative values you can just mulitply by -1

e.g. int A=(100+300) % 360; // A=40

A * -1 = -40

Rich.

MarkT,

i now know what you mean about the issues with the % operator

The modulo operator basically doesn't seem to work at all, e.g.

if you do x = 5 % 9; (Result = 4 ) Great this works.

now if i was to do x = 5 % 12 (Result = 5 ?????) when it should really be 7

as soon as you go higher than the number 10 it fails and i haven't got a clue why.

Rich.

x = 5 % 12 (Result = 5 ?????) when it should really be 7

Why should it be 7? :-?