problem with math fanction

hellow,

i have problem in my code,

The general purpose of this code is to describe a random motion of particles in squre plane.

theta=rand()%6283; // generate random angel between 0-6.282.
theta=theta/1000;
x= x+r*cos(theta); // x and y starts place of particle and he supposed
to make random step by theta. r=0.1
y= y+r*sin(theta);

The code run for many particles and each particle move randomly until its reach to other particle or to the boundary.

The problem is thay the final picture i get is not uniform and i dont know why.
It is important me to say that i have already checked the other part of the code and i am quite sure that the problem is in the part above.

thanks,

Maya
Is theta float or double? If it isn't all your results will be thrown off. To be more accurate, the only angles generated will be for integer radians: k*57.296°

On a different subject, you'll get better results if you do
1
2
3
theta=rand();
theta/=double(RAND_MAX+1);
theta*=3.141592*2.0;
hi helios,
first of all thank you for your comment.
theta is double in my code so i guss that isn't the problem.
can you explain me please what's the difference between what you recommend and what i did.

thanks,

maya
In your case the addition creates a new tempory objects, which will be used with the copy constructor to set x to the value.

The addition operator could look like this:

 
double operator+(const double &, const double &);


As you see, it returns a double (a new object, not an reference!). Thats what i meant by temporary object. Thats because sometimes its not wanted to change any of the param. (z = x + y)

But if you use += the compiler know, that he does not need to create a new temp. double, and so its generally faster. (ok with doubles there is no real deal, but since you properbly have 1000 instances...)

So generally, if you CAN, than use += or *=, -=, /= ... etc.

Maikel


Edit: I guess with smart compilers they do sometimes automatic optimations, but you can't be sure.
Last edited on
Huh??
The compiler just needs to generate the code to load the values to floating point registers (on x86). Temporary objects are only ever created when there are function calls involved.
I recommended that method because it gives higher granularity than what he was using. His expression gives a granularity of 1/6283 of a revolution, while mine gives one of at least 1/32768 of a revolution.
It doesn't really make that much of a difference, but if the bits are there, why not use them?
maikel wrote:

Edit: I guess with smart compilers they do sometimes automatic optimations, but you can't be sure.


That may work for doubles, but in general this does not go this way. (i checked the assembler code, and yes, it is doing the same)

Everything i wanted to say is, that it is generally better to use += instead of +, if possible.
In fact, the compiler try to optimize this, if he can.

Normally you would do things like this:

1
2
3
4
5
6
7
8
struct Complex {
    double m_r, m_i;
    Complex(double r, double i) : m_r(r), m_i(i) { }
    Complex operator+(Complex const &z) const
    {
         return Complex(m_r + z.r, m_i + z.i); // temporary variable
    }
};


If you use classes like this one, you should always try to use += instead of + (if possible), because it is generally more optimized. I mean, if it is written exactly for its case, than use it for this case.
Last edited on
Well, we are talking about doubles.
hi helios,
actually your advise help me a lot and it's work better now.
i will be glad to hear if you have more ideas how to get better result whan useing rand function.

one way i use it is:

x=rand()%10000; //generate random place between 0-1000 (in steps of 0.1).
x/=10;

thanks again,
maya
Topic archived. No new replies allowed.