Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 5th, 2012, 05:43 AM
 JamesA1
Registered User

Join Date: Sep 2012
Posts: 12
Time spent in forums: 3 h 18 m 41 sec
Reputation 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
September 5th, 2012, 05:52 AM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 33
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;
}```

#3
September 5th, 2012, 05:58 AM
 JamesA1
Registered User

Join Date: Sep 2012
Posts: 12
Time spent in forums: 3 h 18 m 41 sec
Reputation Power: 0
Respect. I came up with a very stupid solution but yours was elegant...and within a few minutes!

Thank you.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Loop to iterate from a number in the range 0:2*pi and back to the number