Miscellaneous > Programming & Networking
A programming challenge all up in your face.
H_TeXMeX_H:
Much better, good job.
worker201:
What happens when the decimal point error occurs in the other direction? Meaning, what if we got -0.99999999999999934442 instead of -1? Didn't happen in this particular case, but I bet it could.
I think a momentary recast of the argument sent to acos would work better. Wouldn't have to change all the variables, just that one. The return from acos will be a double no matter what value you give it. So why not make it a value with acceptable precision?
mobrien_12:
--- Quote from: worker201 ---What happens when the decimal point error occurs in the other direction? Meaning, what if we got -0.99999999999999934442 instead of -1? Didn't happen in this particular case, but I bet it could.
I think a momentary recast of the argument sent to acos would work better. Wouldn't have to change all the variables, just that one. The return from acos will be a double no matter what value you give it. So why not make it a value with acceptable precision?
--- End quote ---
Yup, but it will just be imprecision then, tiny tiny numerical error. If the absolute value gets greater than one it's tragic because it breaks the trig function.
Nothing wrong with error creating a tiny bit of imprecision, it will be smaller than the decimals you see with single precision...we just have to sanity check the data to make sure it doesn't break something. Your stuff works! :)
Now does it work with tangents and vertical lines?
worker201:
Hahahaha, rockstar = me.
Okay, here's the latest fully operational version of the circleline program:
http://www.triple-bypass.net/download/circleline3.txt
It has been cleaned up a bit - at first it made sense to document every step of the messy quadratic equation. Now, extraneous assignments and variables have been removed. The calculation of circle arcs and the calculation of distances are both functions, and instead of returning a variable, they return an expression - again, removing unnecessary steps that were once invaluable.
The decimal point error has been corrected, and I am now getting nice accurate values with doubles. Here's the method:
--- Code: ---double theta;
double rsquare;
double arc;
float stepone;
rsquare = pow(rad, 2);
stepone = (2 * rsquare - pow(chord, 2)) / (2 * rsquare);
theta = acos(stepone);
--- End code ---
By declaring stepone, the argument passed to acos, as a float, I forced the Cosine Law calculation to return a float. Since all the variables in the equation are doubles, the result is automatically a double. Because of the declaration, however, the result is converted into a float. The result is that the double value is truncated to float precision. Even if you viewed the value of stepone (in the testfile) to 25 places, it is still just -1. stepone then gets converted back to a double when it is passed to acos. As you will see below, the results are great.
Also, since the results of acos are precise enough to pass the "== 0" test, my tangent-catcher works. And vertical lines are handled appropriately, using the calcarc and distancer function just as well as the normal lines. Horizontal lines are also dealt with appropriately.
Results:
1. normal test case
file1.dat:
--- Code: ---0
0
10
10
1
5 5 1
--- End code ---
--- Code: ---powbook:~ lholcombe$ ./a.out
Enter a filename for input:
file1.dat
circle # 1 intersects the line, calculating arc distance
Enter a filename for output:
file1.out
--- End code ---
file1.out:
--- Code: ---15.2837
--- End code ---
2. horizontal line with tangent circle
file2.dat:
--- Code: ---0
5
10
5
1
5 6 1
--- End code ---
--- Code: ---powbook:~ lholcombe$ ./a.out
Enter a filename for input:
file2.dat
circle # 1 does not intersect the line (tangent)
Enter a filename for output:
file2.out
--- End code ---
file2.out:
--- Code: ---10
--- End code ---
3. vertical line with intersecting circle
file3.dat:
--- Code: ---0
0
0
10
1
0 5 1
--- End code ---
--- Code: ---powbook:~ lholcombe$ ./a.out
Enter a filename for input:
file3.dat
circle # 1 intersects the line, calculating arc distance
Enter a filename for output:
file3.out
--- End code ---
file3.out:
--- Code: ---11.1416
--- End code ---
If anyone has any sensible suggestions for improving this program, please offer them.
mobrien_12:
Aces! Two completely different solutions in two different languages for the same problem with the same answer :)
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version