A silly logic problem I cannot solve

Mar 19, 2008 14 Replies

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



  1. 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++; } }


Sorry this should read :

As i DECREMENT, I go downto 0 and then 'wrap around' back to 3600.

But you get the idea.... I'm just trying to find a number that is within +/- 2 on that 0 to 3600 number line.

What's wrong with (int)(seednumber - givennumber)/fixed value?

Meindert

// Maybe something like this? while(endflag != 1) { float plustwo, minustwo;

plustwo = seednumber+2; minustwo = seednumber-2;

if(plustwo > 3600) plustwo = (0 + (plustwo - 3600)); if(minustwo > 3600) minustwo = (0 + (minustwo-3600)); if(plustwo < 0) plustwo += 3600; if(minustwo < 0) minustwo += 3600; if( ((seenumber+2)==plustwo)&&((seednumber-2)==minustwo) { if( (givennumber < plustwo) && (givennumber > minustwo) ) endflag = 1; else { seedvalue = seedvalue - 190.12; numinc++; } } else { if( ((givennumber < plustwo)||(givennumber > minustwo)) && (minustwo plustwo) ) endflag = 1;

else { seedvalue = seedvalue - 190.12; numinc++; } } }

p me

Read up on the DIVIDE function

Now you need to read up on the ADD function.

Basically I want to

get

Ok now you need the SUBTRACT function.

rk

of

Have you got a ten year old anywhere in your household?

dvalue -2))

value;

If it gets confusing, you could always add an offset greater than your decrement to the seed and endpoint, and thus eliminate the wrapping logic...

I don't think you can.

Since your seed value is an arbitrary number in the range 0-3600, there is no guarantee that repeated subtractions of another arbitrary number will get you within any particular target range. With the values given, you have a potential error around the target range of about +/- 85.06.

If you simply want to know how many steps are required to go past another value, that is easy enough.

How about you tell us what your problem really is, that this appears to be an incorrect solution to.

Pete

Others can give a solution to the comparison problem. I have a word of warning that if it important to keep an accurate value (tenths of a degree?) as you make adjustments without feedback correction. I suggest using fixed point arithmetic, rather than floating point since you will continue to accumulate small errors as you increment or decrement with a binary approximation.

Thad

Instead of this contrived and oversimplfied version of the problem, why not just explain what you are REALLY trying to do?

seedvalue is fixed.

All I'm trying to do is find a fixed number along a number line of 0 to 3600 to within +/- 2. I continously subtract 190.1 from my seednumber until I find that fixed number. If I ever go to 0 or below, I continue the subtraction of what's left on the other end (3600).

That's it.

My problem is that I'm confused about how to write the logic of the If statement. If I subtract 190.1 and lets say land up at is located at

0.1 then -2 of 0.1 is on the other side (3598.1) and +2 is 2.1. Note the +2 (or maxvalue) is smaller than -2 (the minvalue) which screws up my comparison of if(givennumber > minvalue) && (givennumber < maxvalue).

I just wanted to know how to write the logic of the if statement such that it would work in such a scenario as I was confused.

So stated simply, how do you write a logic statement to look for a fixed number given a number circulating around 0 to 3600 to within +/-

  1. What is the SIMPLEST way to do it.

Thank you Joel. I will have to test your answer out and will let you know. I think you understood what my problem is but it seems my description confused everyone else.

Thank you again.

This is a good point. That is why I have a range of finding the givennumber to within +/- 2. I have a maximum number of iterations in which I will find it so it does not go on endlessly. It will not fall out of that +/- 2 range for those number of iterations. Possibly it could if it kept going though.

But good point!

The decrementing of 190.1 from the seedvalue of 1984 is circular around 0 to 3600.

That is if I keep subtracting 190.1 from the seednumber and keep going that way, I will eventually land up with a value below 0. I take the remainder below 0 subtracting it from 3600. On and on it goes until I find my givennumber.

If the given number is say 5, then its easy enough as its between a +2 range of 7 and -2 range of 3. But if the given number is 0.1, then the +2 range is 2.1 and the -2 range is on the other side (3598.1).

I was just confused about how to write the if statement to make a comparison of a given number to within +/- 2 in all cases where a number circulates about 0 to 3600. I count the number of iterations it took me to get to there.

You solution assumes that we do not circulate around 0 to 3600.

Thank you anyway.

This might cause problems. It sounds like a bandaid around the problem and I doubt if it would work.

Too bad you want to use floats. If all the quantities in the problem could be expressed using (scaled) intergers it would be trivial to solve.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required