Miscellaneous > Programming & Networking
A programming challenge all up in your face.
mobrien_12:
--- Quote from: TheQuirk ---Suggestion: Why not make a function where you pick which variable is independent, and which is dependent? That way you could have something along the lines of the following.
--- Code: ---if (x1 == x2) {
happyfunction(y1, y2, x1, x1);
}
else {
happyfunction(x1, x2, y1, y2);
}
--- End code ---
--- End quote ---
Hmm. That's a good idea for my variation on Worker's code, rather than alter the way the data is loaded into variables...
worker201:
After inserting a bazillion little printf statements at various places, I have found that my program is no longer reading the data from the file properly. All potentially floating point values have been declared as doubles. I suppose for test purposes, they could all be read as integers, but for functionality, I want the input file to possible contain a decimal point.
Using %f statements, like:
fscanf(fRead, "%f%f%f", &circlex, &circley, &radius);
I get:
circlex[0]=0.000000
circley[0]=-0.000000
radius[0]=nan
If I switch them to %g, I get garbage for circlex, negative garbage for circley, and nan for radius.
WTF?
This worked when they were all floats. Does fscanf require double input to have a decimal place? Should I read the data as type float and convert to type double afterwards? This language is pissing me off right now.
H_TeXMeX_H:
The problem with converting float to double is some loss in accuracy. If you can't get it to work with doubles then just use float, but know that your answer may be slightly innacurate (not enough to mean too much).
mobrien_12:
--- Quote from: worker201 ---
Using %f statements, like:
fscanf(fRead, "%f%f%f", &circlex, &circley, &radius);
I get:
circlex[0]=0.000000
circley[0]=-0.000000
radius[0]=nan
--- End quote ---
Ah.. Found it. Double precisions have a different format code, becasue they are different kinds of variables. I never really used doubles, mostly becasue I never cared about the tiny benefits in accuracy they bring, so I didn't know this before. They take twice as much ram and unless you are on a 64-bit platform you take a computational power hit.
The format code is %lf instead of %f. Interestingly enough, you apply the same "l" (that's an "l" as in "long" not the number one) modifier to integers if you use double length integers.
Try this instead:
--- Code: ---
fRead = fopen(infilename, "r");
if (fRead==NULL) {
printf("input file unavailable\n");
return 1;
}
fscanf(fRead, "%lf", &x1);
fscanf(fRead, "%lf", &y1);
fscanf(fRead, "%lf", &x2);
fscanf(fRead, "%lf", &y2);
fscanf(fRead, "%d", &numcirc);
printf("x1 %lf y1 %lf x2 %lf y2 %lf numcirc %i\n",x1,y1,x2,y2,numcirc);
if (numcirc>15) {
printf("too many circles, aborting\n");
return 1;
}
for(i=0; i
mobrien_12:
The program still returns NAN... tracked it down to the function. One small error.. the includes must be in front of the function too, it's not much different than writing them in separate files.
--- Code: ---
/* BEGIN THE CALCARC FUNCTION */
#include
#include
double calcarc(double update,double rad,double chord,int a)
{
/* variable declarations */
double theta;
double rsquare;
double arc;
printf( "In startof calcarc, current total is %lf\n", update);
rsquare = pow(rad, 2);
printf( "rsquare %lf, chordsquare, %lf\n", rsquare, pow(chord,2));
printf( "argument to acos is %lf\n", ( ( (2 * rsquare) - pow(chord, 2) ) / (2 * rsquare) ));
printf( "acos is %lf\n", acos( ( (2 * rsquare) - pow(chord, 2) ) / (2 * rsquare) ));
theta = acos( ( (2 * rsquare) - pow(chord, 2) ) / (2 * rsquare) );
printf( "theta %lf\n", theta);
if (theta == 0)
{
printf("In calcarc, circle # %d does not intersect the line (tangent)\n", a+1);
return update;
}
else
{
printf("In calcarc, circle # %d intersects the line, calculating arc distance\n", a+1);
arc = theta * rad;
update = update - chord + arc;
printf( "In endof calcarc, current total is %lf\n", update);
return update;
}
}
--- End code ---
The problem is that acos is not defined for some reason.. it's returning NAN instead of 2pi. I can't figure out why right now.. gotta go to bed.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version