It may be a simple problem but its messed me up sadly. Please help me as I'm confused :(
The problem :
Imagine a number line from 0 to 3600. I have a seed number and a given number on that that number line. Both are FLOATS.
seednumber = 1984 givennumber = 0.1
The objective is to keep on decrementing 1984 by a fixed value (e.g.
190.12) until I reach 0.1 and count the number of increments it took me to get there.As i increment I go upto 3600 and then 'wrap around' back to 0... and it keeps going that way.. until I get to 0.1. Basically I want to count the number of increments it takes to get to 0.1. I may not get to 0.1 exactly so I have a range of +/- 2 on that 0.1 value. (3598.1 to 2.1). Herein lies the problem.
How do I write the simple IF statement for this? This seems to work (below) except when I get to the edge (e.g. if seedvalue is 3599.4. then 3599.4 + 2 goes over 3600)... and the comparison in the if statement is all messed up. Likewise when I go to the other edge of
- Say 0.33 - 2 is a negaive number and the comparison is again messed up.
How do I write the if statement such that I always am able to compare the given number to within a range of +/- 2 ?
while(endflag != 1) { if((givennumber < seedvalue + 2) && (givennumber > seedvalue -2)) { endflag = 1; } else { seedvalue = seedvalue - 190.12; if(seedvalue < 0) { seedvalue = 3600 + seedvalue; } numinc++; } }