For this purpose you can use the log and exp functions. To avoid rounding issues, it is best to use a double data type.
The log function gives the natural logarithm, but using any other base other than the number e (for example, log10 ()) will also give the same results, because there is only one unique set of numbers with equal logarithmic distance for a given interval:
#include <math.h> /* log & exp & round */ const double N = 100; // total no of sizes to generate const double LOW = 10; // range low-bound const double HIGH = 1000000; // range upper-bound double SIZES[N]; double GAP = (log(HIGH)-log(LOW))/N; SIZES[0] = LOW*exp(GAP); for(int i=1; i<N; ++i) { SIZES[i] = (SIZES[i-1]*exp(GAP)); } //rounding for(int i=1; i<N; ++i) { SIZES[i] = round(SIZES[i]); }
source share