Getting same numbers

closed account (zbREy60M)
Hello,

I am writing a program to calculate the spindown frequencies of certain celestial objects.
the variable a is their age.
the variable p is their period
prefrequency is the frequency of the waves emitted by the objects
postfrequency is supposed to be the frequeuncy of the waves emitted by the objects after time "a" has elapsed.

However, when I run the program, I get THE EXACT SAME NUMBERS for pre and post frequency. This completely boggles my mind. Even when I change some numbers around, it makes no difference, I just get the same values.

Halp?

1
2
3
4
double a = AgeComponents.GetAge()*10000000;
double p = abs (PeriodComponents.GetPeriod());  //This grabs the period anoesn'td gives a variable to be calculated; p and P are measured in seconds
double prefrequency = 2/p; //Note that this is the frequency of the gravitational waves, which is twice the frequency of the spinning gravitar
double postfrequency = pow(pow(prefrequency,-4.0)+(20*(300000000))/(32*(pow ( (3.1415), 4.0))*(6.673*.000000000001)*(pow(10.0,45.0))*(pow(E,2.0))*a), -.25);
Try it now:
1
2
3
4
5
6
7
8
9
10
11
double a = AgeComponents.GetAge() * 10000000.0;
double p = abs (PeriodComponents.GetPeriod());
double prefrequency = 2.0 / p;
//WARNING: This assumes E is the mathematical constant e.
//Store this in a non-const if that's not the case.
const double E2=E*E,
	pi=3.1415926535897932384626433832795,
	pi4=pi*pi*pi*pi,
	G=6.673E-11;
double divisor= 32.0 * pi4 * G * 1E+45 * E2 * a;
double postfrequency = pow( pow(prefrequency,-4.0) + 6E+9 / divisor, -.25 );
The likely problem: 20*300000000 is an operation on integers. This was probably overflowing. I replaced it with the equivalent 6E+9. Another possibility is 20.0*300000000.0. Those zeroes make a huge difference.

A few notes:
* Don't use pow() for scientific notation. The language already has a handy syntax for that.
* In general, don't call pow(a,b) where a and b are constants. Compute it by hand and write down the result instead.
* When writing complex expressions, it helps to declare more variables or constants. It's much easier to see how the expression is structured the way I wrote it, isn't it?
Last edited on
closed account (zbREy60M)
Sorry! E is not the mathematical constant e. I forgot to define what E was. E is the ellipticity of the gravitar, which is just a constant that is determined earlier in the program. Very sorry.

But yes, your way is a much better way of writing programming. I guess my program looks ridiculously messy. Thanks for taking the time to help me out.
Last edited on
closed account (zbREy60M)
After using your code, and making the change that E2 is just a double and not a const, I still am getting the same values for pre and post frequency.

Here is my full code of my program, with the area in question being around lines 124ish

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include "boost/random.hpp"
#include "boost/generator_iterator.hpp"
#include "boost/random/normal_distribution.hpp"
#include <boost/random/uniform_int.hpp>
#include <boost/random/uniform_01.hpp>

using namespace std;
using std::vector;  

class Gravitar //creates a class called Gravitar
{
public: //public assesors
	double GetRadius();
	void SetRadius(double radius);
	double GetAngle();
	void SetAngle(double angle);
	double GetZed();
	void SetZed(double zed);
	double GetAge();
	void SetAge(double age);
	double GetPeriod();
	void SetPeriod(double period);
private: //private values
	double Radius;
	double Angle;
	double Zed;
	double Age;
	double Period;
};
double Gravitar::GetRadius() //GetRadius, public assessor function returns value of Radius
{
	return Radius; //returns the value Radius
}
double Gravitar::GetAngle()
{
	return Angle; //returns the value of Angle
}
double Gravitar::GetZed() 
{
	return Zed;
}
double Gravitar::GetAge()
{
	return Age;
}
double Gravitar::GetPeriod()
{
	return Period;
}
void Gravitar::SetRadius(double radius)//sets radius to Radius
{
	Radius = radius;
}
void Gravitar::SetAngle(double angle)
{
	Angle = angle;
}
void Gravitar::SetZed(double zed)
{
	Zed = zed;
}
void Gravitar::SetAge(double age)
{
	Age = age;
}
void Gravitar::SetPeriod(double period)
{
	Period = period;
}
int main()
{
int m ;
cout << "How many Gravitars?\n\n";
cin >> m;
double e ;
cout << "\n\nWhat is the ellipticity of the objects? Enter 6, 7, 8, 9, for gravitars of ellipticity of 10^-x.\n\n";
cin >> e;

double E =  pow (10, -e); //the ellipticity should vary from 10^-6 to 10^-7 to 10^-8 to 10^-9


	boost::mt19937 rng( time(0) );
	boost::normal_distribution<> nd(5, .69);
	boost::variate_generator<boost::mt19937&,  boost::normal_distribution<> > var_nor(rng, nd);
 
	boost::minstd_rand intgen;
	boost::uniform_01<boost::minstd_rand> gen(intgen);
  
double R;

ofstream myfile //("agedata.txt"); 
("positiondata.txt");

for (R = 0; R < m; ++R)
{	

	double radius = sqrt(gen());
	double angle = gen();
	double zed = gen();
	double age = gen();
	double period = var_nor(); // normal distribution of periods with a mean of 5 and a standard deviation of .69 seconds 

Gravitar RadialComponents;
RadialComponents.SetRadius(radius);
Gravitar AngularComponents;
AngularComponents.SetAngle(angle);
Gravitar ZedComponents;
ZedComponents.SetZed(zed);
Gravitar AgeComponents;
AgeComponents.SetAge(age);
Gravitar PeriodComponents;
PeriodComponents.SetPeriod(period);

double x = RadialComponents.GetRadius()*cos (AngularComponents.GetAngle()*6.283185307179586476925286766559)*15329.729; 
double y = RadialComponents.GetRadius()*sin (AngularComponents.GetAngle()*6.283185307179586476925286766559)*15329.729;
double z = ZedComponents.GetZed()*153.29729;
double a = AgeComponents.GetAge() * 10000000.0;
double p = abs (PeriodComponents.GetPeriod()); //to be calculated; p and P are measured in seconds
double prefrequency = 2.0 / p;
//WARNING: This assumes E is the mathematical constant e.
//Store this in a non-const if that's not the case.
double E2=E*E;
const double pi=3.1415926535897932384626433832795,
	pi4=pi*pi*pi*pi,
	G=6.673E-11;
double divisor= 32.0 * pi4 * G * 1E+45 * E2 * a;
double postfrequency = pow( pow(prefrequency,-4.0) + 6E+9 / divisor, -.25 );

cout  << "Gravitar #" << R+1 << "    " << x << "    " << y << "    " << z << "    " << a << "    " << p << "   " << E << "   " << prefrequency << "   " << postfrequency << endl;

if (myfile.is_open())
{
	myfile << " " << x << "  " << y << "  " << z << " " << endl;
//	myfile << " " << a << "  " << p << endl;
}
}

myfile.close();//*//

system("PAUSE");
return 0;
}
Last edited on
Is the difference supposed to be big or very small? If the latter, it's possible that the printing mechanism is simply not displaying enough digits for you to know that the numbers are different.
closed account (zbREy60M)
The difference should be quite large. Especially for the gravitars that spin down over 10's of millions of years.
Well, working it out by hand, the limit as 6E+9 / divisor tends to zero of
pow( pow(prefrequency,-4.0) + 6E+9 / divisor, -.25 ); is equal to the prefrequency.

With the divisor quantity having a factor of 1E+45 on top of a value of a presumably in the order of 10^20, divisor is probably in the order of 10^36 (that's after taking G and E^2 into account). That's bringing the quantity 6E+9 / divisor awfully close to zero.

To prove that this is the case, do this:

1
2
double postfrequency1 = pow( pow(prefrequency,-4.0), -.25 );
double postfrequency2 = pow( pow(prefrequency,-4.0) + 1000000, -.25);


You'll see that postfrequency1 == prefrequency whereas postfrequency2 != prefrequency.

Last edited on
Topic archived. No new replies allowed.