# Changing precision of a float?

• 01-26-2014, 11:09 AM
zFollette
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.

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);```
• 01-26-2014, 02:51 PM
JosAH
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,

Jos
• 01-26-2014, 09:41 PM
zFollette
Re: Changing precision of a float?
Quote:

Originally Posted by JosAH
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,

Jos

But I can find the intersection.

Like this:
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);```
A, B, and C being Vectors on a plane, and start and end being the beginning and end points of a cast ray.
I am purposly applying a loss of precision to x, y, and z to filter out any very, very slight inaccuracies (tens of millionths)
• 01-27-2014, 02:47 AM
kjkrum
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.
• 01-27-2014, 05:16 AM
zFollette
Re: Changing precision of a float?
Quote:

Originally Posted by kjkrum
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.

Its fine, the inaccuracy is only 2 ten millionths. Thats why it is ok to lose some precision, so that won't happen.
• 01-27-2014, 10:41 AM
JosAH
Re: Changing precision of a float?
If x and y are 'almost equal', then |x-y| is a measure of the absolute error (which doesn't say much), and |x-y|/|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 |x-y|/|y| into account ...

kind regards,

Jos