Results 1 to 6 of 6
  1. #1
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default 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; 01-26-2014 at 10:16 AM.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Changing precision of a float?

    Quote Originally Posted by JosAH View Post
    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:
    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);
    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)
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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!

  5. #5
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Changing precision of a float?

    Quote Originally Posted by kjkrum View Post
    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.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Possible Loss of Precision?
    By jwb4291 in forum New To Java
    Replies: 6
    Last Post: 12-02-2010, 04:28 AM
  2. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  3. Possible loss of precision
    By jankidudel in forum New To Java
    Replies: 2
    Last Post: 08-07-2010, 10:15 AM
  4. Precision
    By c_walker in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 11:36 AM
  5. Wrong precision
    By lclclc in forum New To Java
    Replies: 0
    Last Post: 09-28-2009, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •