### Thread: Loop to iterate from a number in the range 0:2*pi and back to the number

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
12
Rep Power
0

#### Loop to iterate from a number in the range 0:2*pi and back to the number

Hello All,

I have been trying to construct a loop but need your help.

I need to iterate from an *arbitrary* float that lies in between 0 and 2*pi, go *up to* 2*pi (need not be dead precise at terminating at 2*pi) and then from 0 to that number again. The increament step is not fixed and can be flexible

Ex: if the starting number is 4 and the sincreament size is 1, then the loop must output:

4, 5, 6, 0 , 1, 2 ,3, 4

This must also work for floating numbers with smaller increament sizes e.g 0.2

2. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
34
Try fmod()

See running example at ideone.

[code=c]#include <math.h>
#include <stdio.h>

#define PI 3.1415926536
#define TAU (2 * PI)

int main(void) {
double step = 1;
double start = 4;
double index;
for (index = 0; index < TAU; index += step) {
double curr = fmod(start + index, TAU);
printf("%d %f\n", (int)curr, curr);
}
puts("");
return 0;
}[/code]
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
12
Rep Power
0
Respect. I came up with a very stupid solution but yours was elegant...and within a few minutes!

Thank you.