Sep 15, 2015 at 5:02am UTC
Dear all,
I am using linux ubuntu to run my code. The code generates a log.txt file which includes the log(j) and log(dist). However some parts of the txt file are weird:
4.60517 2.20614
4.70048 2.25328
4.78749 2.29472
4.86753 2.32762
4.94164 2.38985
5.01064 2.3996
5.07517 2.46834
5.1358 2.49174
5.19296 2.51277
5.24702 2.54158
5.29832 2.56658
5.34711 2.59873
5.39363 2.61447
5.43808 2.62403
5.48064 2.66827
5.52146 2.67333
5.56068 2.68887
5.59842 2.72306
5.63479 2.73592
5.66988 2.74812
5.70378 2.75834
5.73657 2.80571
5.76832 2.78588
5.79909 2.80919
5.82895 2.8195
5.85793 2.86036
5.8861 2.86204
5.9135 2.87362
5.94017 2.8906
5.96615 2.914378.8.03916 3.934616.01616 2.91885
0408.10168 3.96.0638.13153 3.97069
.8.16052 3.98148.18869 4.000468.21609 4.01069
8.24276 4.02723
8.26873 4.07193
8.29405 4.06.21461 3.02305
6.23441 3.01737
6.25383 3.03573
6.27288 3.04906
6.29157 3.04131
6.30992 3.07895
6.32794 3.06244
36.6.34564 3.09646.6.36303 3.10076.6.38012 3.080966.39693 3.13949
8.55641 4.210336.42972 3.13394
6.44572 3.15114
6.46147 3.15688
6.47697 3.1679
6.49224 3.17347
6.50728 3.17409
8.68271 4.24868
8.69951 4.27109
8.71604 4.28425
8.7323 4.30127
8.7483 4.27528.76405 6.84405
3149
7956 4.31184
8.79482 4.31579
8.80986 4.31755
8.82468 4.30456
8.83928 4.32516
8.85367 4.32587
8.86785 4.36938
8.88184 4.35728
8.89563 4.35872
8.90924 4.36981
8.92266 4.36599
8.9359 4.37528
8.94898 4.405186.76849 3.28477
6.77992 3.29884
6.79122 3.33122
6.80239 3.31906
29876
4.41792
9.02401 4.4582
9.03599 4.45068
9.04782 4.42982
9.05952 4.45018
9.07108 4.44754
9.08251 4.46694
9.09381 4.46783
9.10498 4.47033
9.11603 4.45281
9.12696 4.48489
9.13777 4.47397
9.14846 4.48298
9.15905 4.48592
9.16952 4.50154
9.17988 4.51708
9.19014 4.51736
9.20029 4.52256
9.21034 4.54215
I'm not sure where is wrong.
I'm also pasting my code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <time.h>
using namespace std;
main () {
double x=0; /* initial position of polymer*/
double y=0;
double z=0;
double dist = 0;
srand48(time(NULL));
ofstream out("position.txt");
ofstream out2("log.txt");
ofstream out3("cm.txt");
/* j is the length of polymer chain */
for (int j =100; j <1000;j+=10) {
int p;
for ( p=1; p<1000;p++){
for (int i = 1; i < j; i++) {
double p1=drand48(); /* generate random num between 0 and 1*/
if(( p1 > 0) && ( p1 < 0.1666667))
{
x+=1;/*lattice space=bond length=1*/
}
else if (( p1 >0.1666668) && (p1 <0.3333333))
{
x+=-1;
}
else if (( p1 >0.333334)&& (p1< 0.5))
{
y+=1;
}
else if (( p1 >0.500001) && (p1 <0.6666667))
{
y+=-1;
}
else if (( p1 > 0.666668) && ( p1<0.8333333))
{
z+=1;
}
else
{
z+=-1;
}
out << x << " "<< y << " "<< z <<" "<< endl;
}
dist +=sqrt(x*x+y*y+z*z);
x=0;
y=0;
z=0;
}
dist =dist/p;
p=1;
cout << "x: " << x << ", " << "y: " << y << ", "<<"z: " << z << endl;
out2 <<log(j) << " " <<log(dist) << endl;
x=0;
y=0;
z=0;
}
out.close();
}
Sep 15, 2015 at 5:16am UTC
Please put
tags around your program. And it would be much easier to read and provide advice if it was properly or at least reasonably indented.
Also, which parts of the output are weird - we can guess but it might not be what you mean - an explanation of why it is weird might help too - ie why is it unexpected and what did you expect? :)
Sep 15, 2015 at 4:03pm UTC
I'm not sure why your output is screwed up, but I noticed some stuff with your if construct.
1 2 3 4 5 6 7 8
if (( p1 > 0) && ( p1 < 0.1666667))
{
x+=1;/*lattice space=bond length=1*/
}
else if (( p1 >0.1666668) && (p1 <0.3333333))
{
x+=-1;
}
In this code snippet, you are not doing anything if p1 == 0.1666667 or 0.3333333. You are also not doing anything for values between these two values. When working with floating point numbers, it is dangerous to compare against specific base-10 values.
You probably want to do something along the lines of
1 2 3 4 5 6 7 8 9 10 11 12 13
#define BOUNDARY_1 (0.1666667)
#define BOUNDARY_2 (0.3333333)
...
if (( p1 >= 0) && ( p1 < BOUNDARY_1))
{
x+=1;/*lattice space=bond length=1*/
}
else if (( p1 >= BOUNDARY_1) && (p1 < BOUNDARY_2))
{
x+=-1;
}
Where to use > vs. >= or < vs. <= are up to you.
If you want to compare 2 values for equivalence, you probably need to do something like:
1 2 3 4 5 6 7 8 9 10
#include <cmath>
#define THRESHOLD (0.0000000001)
...
if ( fabs(p1 - otherValue) < THRESHOLD)
{
/* do something */
}
Last edited on Sep 15, 2015 at 4:05pm UTC
Sep 15, 2015 at 7:54pm UTC
note also that Linux and Windows does not have the same EOL