Creating a linearly increasing vector

Hi everyone,

I have a bit of a strange problem at the moment. Have written the following function to create a linearly increasing vector, with a defined start point, finish point and length. Here it is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// A function that linearly divides a given interval into a given number of
  // points and assigns them to a vector
vectordouble linspace(int a, int b, int num)
{
             // create a vector of length num
             vectordouble v(num);
             double tmp = 0.0;
             
             //int lala = num;
             
             // now assign the values to the vector
             for (int i = 0; i < num; i++)
                 {
                      v.x[i] = a + i * ( (b - a) / num );
                 }
             return v;
}


The vector is created, but assigns -1 to every entry, as if i was not incrementing but simply staying at zero. When I remove the dividing 'num' it works fine - could it be a problem with number types?

Thanks, Mike
Last edited on
(b - a ) / num

is integer division since a, b, and num are integers.

you probably want floating point division.

you will need to cast one of the terms to double to force floating point arithmetic.
Topic archived. No new replies allowed.