How to increment a for loop with a decimal value in C

13,393

Solution 1

Simple Solution: Multiple by your desired gain

double gain = 0.1;
for (k=0; k<BUFFER_LEN; k++) {                
  buffer[k] = sin(gain * 2*pi*(f/fs)*k);          //sine generation
}

No need to change your k loop, BUFFER_LEN & no floating point issues. 1/Gain does not need to be an integer.


Your original problem was likely due to:

int k;
for (k=0; k<BUFFER_LEN; k += 0.1) {                
  buffer[k] = sin(2*pi*f/fs*k);          //sine generation
}

In this case, the k += 0.1 did k = (int) (k + 0.1) which truncates the sum back to the original k. Thus your loop runs forever.

Solution 2

If all you want to do is increment by .1, your k value needs to be a float. After that, it's as simple as this:

for(double k = 0; k < limit; k += 0.1)

But the over usage of decimal values could cause rounding errors, and your code needs k to be a whole number. Here is perhaps a better solution:

for(int k = 0; k < limit * 10; k++){
    //Now, in your equation, use k/10.0
    buffer[k] = sin(2 * pi * f / fs * (k / 10.0));
}

Make sure the size of buffer is ten times what it would need to be!

Share:
13,393
user2459764
Author by

user2459764

Updated on June 04, 2022

Comments

  • user2459764
    user2459764 almost 2 years

    I have this code

    for (k=0; k<BUFFER_LEN; k++){                
    
               buffer[k] = sin(2*pi*f/fs*k);          //sine generation
    

    my loop increments by 1 each time - so k will be 1, 2, 3, 4, 5.... etc for each calculation

    I would like the loop to increment by 0.1 each time for example, so my sine calculation is more accurate? What would be the simplest way to achieve this? I tried incrementing by 0.1 in that for loop but dont think this is allowed as the program times out

    edit: here is a solution

    int i, k;
    float z=0.1;
    
    for(i = 0; i < BUFFER_LEN; i++){                         // fill the buffer
           buffer[k] = sin(2*pi*f/fs*z);                     // sine wave value generation
           z = z + 0.1;
           }
    
  • Qiu
    Qiu over 10 years
    If k equals e.g. 1.1, what does buffer[1.1] mean?
  • Burkhard
    Burkhard over 10 years
    @Qiu: true, corrected.
  • chux - Reinstate Monica
    chux - Reinstate Monica over 10 years
    Should BUFFER_LEN be an integer (a likely occurrence), BUFFER_LEN/10 will perform integer division. Example for (float k=0; k<4/10; k+=0.1) will iterate 0 times rather than expected 4.
  • Eric Postpischil
    Eric Postpischil over 10 years
    Due to rounding errors, using floating point in a loop iteration, as this code does, may overrun the desired endpoint and will have increasing rounding errors during the loop. Iteration counting should be done with integer arithmetic.
  • user2459764
    user2459764 over 10 years
    Yep! so simple i should have thought of that myself - yes your right, if i tried to increment with a non-integer the loop just ran forever - many thanks!!
  • Rob Starling
    Rob Starling over 10 years
    you should probably remove the double option, as the buffer array cannot be indexed by it
  • Burkhard
    Burkhard over 10 years
    @EricPostpischil: thanks. Very true. Did not think of that.