Results 1 to 6 of 6
Thread: Changing precision of a float?
 01262014, 11:09 AM #1▼ dafuq did I do?
 Join Date
 Dec 2013
 Posts
 203
 Rep Power
 0
Changing precision of a float?
Ok, so in my quest to achive perfect ray casting and line/plane intersection, what I believe to be my last problem is precision of a float. I need some precision (0.000) but I am dealing with the difference of (1.000000000) and (1.000000002), where the second number would be completely off. Any help? I looked at DecimalFormat, but that just puts it into a string, I need actual loss of precision.
*Sigh* nevermind.
Java Code:DecimalFormat form = new DecimalFormat("0.00"); String newX = form.format(x); String newY = form.format(y); String newZ = form.format(z); x = Float.parseFloat(newX); y = Float.parseFloat(newY); z = Float.parseFloat(newZ);
Last edited by zFollette; 01262014 at 11:16 AM.
1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111
 01262014, 02:51 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21
Re: Changing precision of a float?
Don't fiddle with any precision before you can actually find the point of intersection between a line and a plane. If you have the vector equation of the line, i.e. L(x) == l*(a*x, b*y, c*z)+(d, e, f) and the normal equation of the plane, i.e. a'*x+b'*y+c'*y == d', fill in the values of x, y, and z (i.e. l*a*x+d, l*b*y+e, l*c*z+f) in the equation of the plane and solve for l; this gives you x, y and z by filling in l in the vector equation of the line. Only if the line and the plane are near parallel, you should worry about accuracy.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 01262014, 09:41 PM #3▼ dafuq did I do?
 Join Date
 Dec 2013
 Posts
 203
 Rep Power
 0
Re: Changing precision of a float?
But I can find the intersection.
Like this:
Java Code:float x = 0, x1 = A.x, x2 = B.x, x3 = C.x; float y = 0, y1 = A.y, y2 = B.y, y3 = C.y; float z = 0, z1 = A.z, z2 = B.z, z3 = C.z; float[] xC = new float[]{x  x1, x2  x1, x3  x1}; float[] yC = new float[]{y  y1, y2  y1, y3  y1}; float[] zC = new float[]{z  z1, z2  z1, z3  z1}; float addI = (yC[1] * zC[2])  (yC[2] * zC[1]); float addJ = ((xC[1] * zC[2])  (xC[2] * zC[1])); float addK = (xC[1] * yC[2])  (xC[2] * yC[1]); float numOfTs = (addI * (end.x  start.x)) + (addJ * (end.y  start.y)) + (addK * (end.z  start.z)); float num = (addI * (x1)) + (addJ * (y1)) + (addK * (z1))  (addI * start.x)  (addJ * start.y)  (addK * start.z); float t = num / numOfTs; x = start.x + ((end.x  start.x) * t); y = start.y + ((end.y  start.y) * t); z = start.z + ((end.z  start.z) * t);
I am purposly applying a loss of precision to x, y, and z to filter out any very, very slight inaccuracies (tens of millionths)1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111
 01272014, 02:47 AM #4
Re: Changing precision of a float?
Sounds like you're running into floating point error. Floating point  Wikipedia, the free encyclopedia
If you must avoid this inaccuracy, you could do the calculations with BigDecimals. There's probably a way to do the calculations with floats and have the inaccuracy not matter, but I don't know how to help you with that because I know very little about raytracing.Get in the habit of using standard Java naming conventions!
 01272014, 05:16 AM #5▼ dafuq did I do?
 Join Date
 Dec 2013
 Posts
 203
 Rep Power
 0
Re: Changing precision of a float?
1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111
 01272014, 10:41 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,878
 Blog Entries
 7
 Rep Power
 21
Re: Changing precision of a float?
If x and y are 'almost equal', then xy is a measure of the absolute error (which doesn't say much), and xy/x is a measure of the relative error, which is what you want. Note that x and y need to be 'near equal', otherwise you also have to take xy/y into account ...
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
Similar Threads

Possible Loss of Precision?
By jwb4291 in forum New To JavaReplies: 6Last Post: 12022010, 05:28 AM 
Changing binding when changing the underlying model object
By ChrisNY in forum NetBeansReplies: 0Last Post: 08142010, 10:09 AM 
Possible loss of precision
By jankidudel in forum New To JavaReplies: 2Last Post: 08072010, 10:15 AM 
Precision
By c_walker in forum New To JavaReplies: 1Last Post: 10182009, 11:36 AM 
Wrong precision
By lclclc in forum New To JavaReplies: 0Last Post: 09282009, 09:55 AM
Bookmarks