Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return variable "current" with additions/subtractions

Status
Not open for further replies.

jarettl

Programmer
Jan 6, 2005
2
CA
Ok, so it's REAL late (early) and I'm willing to bet someone has drugged my coffee.

Anyways, I can't get this silly code to work properly. Any suggestions? (should be pretty straightforward)


int BounceFlow (float max, float min, float increment, float current)
{
int RoundTrip=0;
int total=current;

if (current >= max)
RoundTrip = 1;

if (current <= min)
RoundTrip = 0;

if (RoundTrip == 0)
return (total += increment);

if (RoundTrip == 1)
return (total -= increment);

return current;
}


Typicaly the code is supposed to return the variable "current" with the apropriate additions/subtractions.

Thanks in advance,
JarettL
 
Well I'd eliminate that roundtrip variable.
The use of += and -= simply confuses the issue.
And if you're trying to bounce between say 0.1 and 0.7, then returning an int is simply no good.
Code:
float BounceFlow (float max, float min, float increment, float current)
{
    if (current >= max) return current - increment;
    if (current <= min) return current + increment;
    return current;
}

--
 
1. Never use min and max as var names. It's not a good style: both are well-known names of standard library functions (or even macros in some implementations).
2. Never use float (except in a case of hard memory conditions;). Use double as a float point type. Apropos: float is slower than double on fast CPUs.
3. See posts above (about int/float mixture). In your original snippet you never return current...
 
Thank you all very much for your input.

JarettL
 
As written, this code is equivilant to:
Code:
    if (current >= max)
        return (total -= increment);
    else
        return (total += increment);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top