- A silly logic problem I cannot solve
- Posted by vorange on March 19th, 2008
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
0. 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++;
}
}
- Posted by vorange on March 19th, 2008
On Mar 19, 4:03*am, vorange <orange...@yahoo.com> wrote:
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.
- Posted by Meindert Sprang on March 19th, 2008
"vorange" <orangepic@yahoo.com> wrote in message
news:145d989b-a5dd-4fe6-a598-3e20fdb8a231@59g2000hsb.googlegroups.com...
What's wrong with (int)(seednumber - givennumber)/fixed value?
Meindert
- Posted by Joel on March 19th, 2008
// 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++;
}
}
}
- Posted by cbarn24050@aol.com on March 19th, 2008
On Mar 19, 4:03�am, vorange <orange...@yahoo.com> wrote:
Read up on the DIVIDE function
Now you need to read up on the ADD function.
Basically I want to
Ok now you need the SUBTRACT function.
Have you got a ten year old anywhere in your household?
- Posted by cs_posting@hotmail.com on March 19th, 2008
On Mar 19, 12:09 am, vorange <orange...@yahoo.com> wrote:
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...
- Posted by Peter Harrison on March 19th, 2008
vorange wrote:
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
- Posted by Thad Smith on March 20th, 2008
vorange wrote:
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
- Posted by vorange on March 20th, 2008
On Mar 19, 9:16*pm, Peter Harrison <peter_harri...@ntlworld.com>
wrote:
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 +/-
2. What is the SIMPLEST way to do it.
- Posted by vorange on March 20th, 2008
On Mar 19, 1:30*pm, "Joel" <joelben...@gmail.com> wrote:
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.
- Posted by vorange on March 20th, 2008
On Mar 20, 2:18*am, Thad Smith <ThadSm...@acm.org> wrote:
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!
- Posted by vorange on March 20th, 2008
On Mar 19, 6:57*am, "Meindert Sprang" <m...@NOJUNKcustomORSPAMware.nl>
wrote:
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.
- Posted by vorange on March 20th, 2008
On Mar 19, 3:24*pm, cs_post...@hotmail.com wrote:
This might cause problems. It sounds like a bandaid around the
problem and I doubt if it would work.