Incorrect calculation of function

  • Good day!


    KR C v.8.3.12, KR C4, KR 60HA


    I cannot understand what incorrect calculation of functions is connected with.
    From where such rounding on the example of receiving the center of a circle for three points in space undertakes?


    DEF TESTfct_3( )
    DECL REAL X1, Y1, Z1, X2, Y2, Z2, X3, Y3, Z3
    DECL REAL MA, MB, MC, MD, X21, Y21, Z21, X31, Y31, Z31, MQ1, MQ2, MQ3, MBY, MCZ
    DECL REAL X0, Y0, Z0


    X1 = -89.3200
    Y1 = 1379.43
    Z1 = 590.445
    X2 = -48.0000
    Y2 = 1452.19
    Z2 = 591.265
    X3 = -5.42000
    Y3 = 1379.88
    Z3 = 590.380
    ;
    MA=Y3*(Z1-Z2)+Y1*(Z2-Z3)+Y2*(-Z1+Z3)
    MB=X3*(-Z1+Z2)+X2*(Z1-Z3)+X1*(-Z2+Z3)
    MC=X3*(Y1-Y2)+X1*(Y2-Y3)+X2*(-Y1+Y3)
    MD=-X3*Y2*Z1+X2*Y3*Z1+X3*Y1*Z2-X1*Y3*Z2-X2*Y1*Z3+X1*Y2*Z3
    X21=X2-X1
    Y21=Y2-Y1
    Z21=Z2-Z1
    X31=X3-X1
    Y31=Y3-Y1
    Z31=Z3-Z1
    MQ1=X1*X1+Y1*Y1+Z1*Z1
    MQ2=X2*X2+Y2*Y2+Z2*Z2
    MQ3=X3*X3+Y3*Y3+Z3*Z3
    MBY=MQ3*(-Z1+Z2)+MQ2*(Z1-Z3)+MQ1*(-Z2+Z3)
    MCZ=MQ3*(Y1-Y2)+MQ1*(Y2-Y3)+MQ2*(-Y1+Y3)
    X0=(2*MA*MD+MB*MBY+MC*MCZ)/(2*(MA*MA+MB*MB+MC*MC))
    Y0=-((MQ1*Z21-MQ3*Z21+2*X0*X31*Z21-MQ1*Z31+MQ2*Z31-2*X0*X21*Z31)/(2*(Y31*Z21-Y21*Z31)))
    Z0=(-MQ1*Y21+MQ3*Y21-2*X0*X31*Y21+MQ1*Y31-MQ2*Y31+2*X0*X21*Y31)/(2*(-Y31*Z21+Y21*Z31))
    END


    it has to turn out:
    X0 = -47.4992543168
    Y0 = 1403.7947203
    Y0 = 590.696145504


    Perhaps there are settings of accuracy of calculation?
    It is possible to look at it on a simple example of simplification of function when viewing "Indication of variables - on one" for MA:
    MA=-Y2*Z1+Y3*Z1+Y1*Z2-Y3*Z2-Y1*Z3+Y2*Z3
    -5.125
    MA= Z1*(Y3-Y2)+Z2*(Y1-Y3)+Z3*(Y2-Y1)
    -5.09375
    MA=Y1*(Z2-Z3)+Y2*(Z3-Z1)+Y3*(Z1-Z2)
    -5.09851074
    And the correct MA value should turn out: -5.0983999999
    Why such difference in results and how to solve a problem?
    I apologize for a non-standard design of a question. I badly know English. :help:

    Edited once, last by yjagor ().

  • Hi,


    all your desired results are double precision. KRL REAL only has only approximatly single precision (if you know e.g. C/C++/Java that would be the difference double vs. float). Also consider reading


    https://en.wikipedia.org/wiki/Floating-point_arithmetic


    Running your code both offline in a C-application gives me in float precision
    X0 -47.499908
    Y0 1403.7947
    Z0 590.69611
    MA -5.0985336
    and inside my KRC4 V8.3#247 Controller
    X0 -47.4991188
    Y0 1403.81506
    Z0 589.785339
    MA -5.098526


    so I would say the results are pretty close.


    Fubini

  • Fubini I am very grateful to you for the answer. You suggested me an idea to solve our problem with the help of the xml-server on c# :zwink:

Advertising from our partners