Thread: limiting variables to a step size

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    10

    limiting variables to a step size

    there is a variable that can change from 0 - 100. And I would like the step size to be variable.
    e.g. 0, 1, 2, 3.....
    2, 4, 6 ....
    10, 20, 30, ...... etc

    aside from a huge if else statement like this

    if (value < stepSize)value = 0;
    else if(value < stepSize * 2) value = stepSize * 1;
    else if(value < stepSize * 3) value = stepSize * 2;

    what can I do?

    I thought it would be easy to do, but I have spent all day without success.

    thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I don't understand the problem. Why can't you just do this:
    Code:
    for (int i = 0; i <= 100; i += step_size)
        ...
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    10
    thanks. Cannot afford the delay of a for loop, it is an interrupt routine. On a single pass through the function, it must be determined that the value has increased or decreased by the step size, and then the lower boundary needs to be displayed. So, for example if the step size is 10, then any values from 0 -9 will show as 0, 10 - 19 will show as 10, etc.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Not being able to afford a for loop sounds like premature optimisation disease to me.

    > So, for example if the step size is 10, then any values from 0 -9 will show as 0, 10 - 19 will show as 10, etc.
    Perhaps
    result = (value / step_size ) * step_size;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2018
    Posts
    10
    Quote Originally Posted by Salem View Post
    Not being able to afford a for loop sounds like premature optimisation disease to me.

    > So, for example if the step size is 10, then any values from 0 -9 will show as 0, 10 - 19 will show as 10, etc.
    Perhaps
    result = (value / step_size ) * step_size;
    WOW, that's impressive, to me anyway. So simple. I have been banging my head on the desk all day, and had just finished writing out 64 if else statements to do the job, which works, but is so poor in design. That's great thanks.

    Now I need to work out how to wind it up to 127 from the last step. For example, if the step size is 32, then it won't go any further than 96. Any clues for me?

    Actually, I have it, but i guess there would be more elegant ways.

    int temp = value;
    result = (value / step_size ) * step_size;
    if(temp == 127) value = 127;
    Last edited by brownt; 02-09-2018 at 04:34 AM.

  6. #6
    Registered User
    Join Date
    Jan 2018
    Posts
    10
    what have I done here. This was an earlier attempt, it worked but it was like some sort of filter that let the value slowly wind its way up to the highest step.
    I actually like that delay for another feature, if I can make that delay variable. Can you tell me why this code winds up slowly to the highest step. About 1.5 seconds with a 10mS sample time.

    Code:
          if(value >  prevValue + stepSize) {prevValue += stepSize; } 
            else if(value <= prevValue- stepSize) {prevValue -=stepSize; }
            value = prevValue;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-09-2015, 12:30 PM
  2. Replies: 1
    Last Post: 03-08-2013, 03:16 AM
  3. Step by step read data from exe file to memory.
    By JoBlack in forum C++ Programming
    Replies: 1
    Last Post: 06-23-2012, 02:02 PM
  4. Temperature Conversion Table (with step size)
    By dorkiie in forum C Programming
    Replies: 5
    Last Post: 02-23-2011, 02:12 AM
  5. why does the memory increase step by step?
    By zcrself in forum C Programming
    Replies: 9
    Last Post: 07-14-2010, 12:04 AM

Tags for this Thread