different outputs from 'identical' codes...

Ok, I've edited this post to make this problem a bit clearer (i think!)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 

#include "yhead.h"

int main()
{
    ofstream out1("t_t.dat");
    

    int npoints = 2940000;
    int i, trns,x=0;

    double h,t;
    double gamma;
    double B, A, a;
    double I,s_1,s_2,s_3,s_4;
    double g,r_1,r_2,r_3,r_4;
    double q,t_1,t_2,t_3,t_4;
    vector<double> intensity;
    h = 0.005;
    //trns = 1960000;
    gamma = 0.04;
    B = 5.8;
    A = 7;
    a = 1.8;
    

    for(i=1;i < npoints;i++)
    {
        s_1 = (g - q - 1)*I;				// I
        r_1 = gamma*(A - g - g*I);			// G
        t_1 = gamma*(B - q - a*q*I);   		        // Q

        s_2 = ((g + 0.5*h*r_1) - ( q + 0.5*h*t_1  ) -1)*(I + 0.5*h*s_1);
        r_2 = gamma*(A - (g + 0.5*h*r_1  ) - (g + 0.5*h*r_1 )*(I + 0.5*h*s_1  ));
        t_2 = gamma*(B - (q + 0.5*h*t_1 )- a*(q +0.5*h*t_1 )*(I + 0.5*h*s_1 ));	

        s_3 = ((g + 0.5*h*r_2 ) - (q + 0.5*h*t_2) - 1)*(I + 0.5*h*s_2);
        r_3 = gamma*(A-(g + 0.5*h*r_2)- (g +0.5*h*r_2 )*(I + 0.5*h*s_2));
        t_3 = gamma*(B-(q + 0.5*h*t_2)- a*(q +0.5*h*t_2 )*(I + 0.5*h*s_2));	

        s_4 = ((g+ h*r_3 ) - (q+ h*t_3 ) - 1 )*(I + h*s_3 );
        r_4 =  gamma*(A - (g + h*r_3  )      - (g + 0.5*h*r_3 )*(I + 0.5*h*s_3  ));
        t_4 =  gamma*(B - (q + h*t_3 )	- a*(q + h*t_3 )*(I + h*s_3 ));	

        I = I + (h/6.0)*(s_1 + 2.0*s_2 + 2.0*s_3 + s_4);
        g = g + (h/6.0)*(r_1 + 2.0*r_2 + 2.0*r_3 + r_4);
        q = q + (h/6.0)*(t_1 + 2.0*t_2 + 2.0*t_3 + t_4);


        x = x + 1;
      
        if(x >=20)
        {	
           
           t = h*i;
            //intensity.push_back(I);
            out1<< t << "     "<< I <<"\n";

            x = 0;	
        }
    }
    return 0;
}





basically, if I run the code above I get what I want in the output file..


HOWEVER....

if i uncomment line 57.. then it somehow changes the data being sent to my output file....


why?!
Last edited on
Many variables in your example are not even visibly declared in the example. How do you expect people to help you?
sorry! .. I think its a bit clearer now
"I" is not initialized.

Althought I don't think that pushing an uninitialized value into a vector causes your problems here, it's a good hint that you have something wrong int the code above which you didn't show us ;-)

Please, can you make sure again that you are posting the code you have the problem with?

Ciao, Imi.
"I" is not initialized.


ok, sorry again!

I think you might be right to be honest..!

It works when "I" is uninitialized... but not when it is!

How do I fix this!?
Last edited on
closed account (z05DSL3A)
With g,q, and I being used un-initialized, I don't see how you could get anything but unreliable results.
With g,q, and I being used un-initialized, I don't see how you could get anything but unreliable results.



I'm not really sure how they are correct either... but I know that they are.. (the results are verifiable!)

when they are initialized.. it all goes wrong!

I don't understand what is happening.




EDIT: using GDB to watch the values...

when the intensity.push_back line is commented out...

> I = 1.920550753E-314

on the first run...



when the intensity.push_back line is included....

>I = 1.8793288799E-317

on the first run...



so it is clearly doing something... I just don't understand why
Last edited on
closed account (z05DSL3A)
What should the initial values of g,q, and I be?

What are you expecting to see?
so it is clearly doing something... I just don't understand why


It's giving you seemingly random / unpredictable behavior because the variables are uninitialized.

If you're initializing them and you're getting incorrect results... then either:

1) Your initializing them incorrectly
or
2) Your formula is incorrect.


Whether or not you sometimes get correct results with uninitialized variables is a total fluke, will not happen on all machines, is unreliable, and is entirely moot for testing purposes.

What you're doing now is basically like flipping a coin and expecting it to come up as heads, but when it sometimes comes up as tails you ask "how come I got tails? It doesn't make any sense!"
Last edited on
http://tinypic.com/r/119nocw/5

this is what I want...

but if I initialize "I" as 0.. then it just stays ==0 the whole time

1) Your initializing them incorrectly



I think this is what the root of the problem is to be honest...


BUT>>>

I would very much like to replicate what the program is doing in the 'uninitialized' form... It can't possibly be a
total fluke
.. it genuinely can't! but... I would like to know what's going on ....
closed account (z05DSL3A)
It would make sense with I = 0 that the output would be all 0

I = 0.1; //or some other no-zero value
g = 0;
q = 0;

I = 0.1; //or some other no-zero value
g = 0;
q = 0;



had literally just figured this out!!

.. so it seems that for uninitialized values.. c++ assigns a very small non-zero number to them unless it is specifically set ==0?


It would make sense with I = 0 that the output would be all 0


what exactly was your reasoning for this? I would genuinely like to know!
closed account (z05DSL3A)
If you don't initialise a variable it will have any junk that happens to be in the memory location.

what exactly was your reasoning for this? I would genuinely like to know!

If I were zero then S_1 evaluate to zero, if S_1 were zero ...
sorry!.. of course



my mistake...

thanks a lot guys!
.. so it seems that for uninitialized values.. c++ assigns a very small non-zero number to them unless it is specifically set ==0?


No. C++ doesn't assign anything. That's why they're uninitialized.

What you get is garbage/uninitialized RAM, which could be ANYTHING. It is completely unreliable. You should never use uninitialized variables.

You just happened to get very small numbers this time. Another machine could have just as easily got positive infinity, NaN, zero, or any number.
Topic archived. No new replies allowed.