My code solve quadratic equation (in game logic tick) to solve the task - find satellite tick offset along an orbit of moveable object in space.
And I've encountered errors in discriminant (farther D
) calculation. I'll remind: D = b^2 - 4ac
.
As it is orbit of big object, my a
,b
& c
are numbers of order like:
1E+8
1E+12
1E+16
Accordingly, b^2
is number of order about 1E+24
, & 4ac
is about 1E+24
too.
BUT this equation root are much less numbers, 'cos they are just coordinates on scene. So roots are about 1E+3 ... 1E+4
.
The problem (updated - concretized): because of floating of values of floats (& doubles) b^2
& 4ac
have inaccuracy, which is small enough (relatively to these very big numbers [measured absolute inaccuracy has order about 1E+18
]), BUT as D
== the difference among them, so when D
is (from bigger values side) to value of order as mentioned inaccuracy is (1E+18
), its value begin to fluctuating in range about +1E+18 .. -1E+18
(i.e. fluctuating range is wider than [-100% .. +100%] of actual value!
Obviously, this fluctuation causes wrong (even wrong directed) tick offsets. And my satellite begin to wobble (and it's awful)).
Note: when I said "when D
is approaching to zero" actually D
is still far enough from zero, so I can't just assign it to zerro in this range of values.
I've considered using of fixed-point calculations (which could rescue me from my problem). But, it is not suggested to use in tick logic ('cos they are much less optimized, and probably will be very slow).
My question: How can I try to solve my problem? May be there are some common solutions for my case? Thanks a lot for any advise!
PS: All formulas are good (I calulated all in excel & got proper results, when floats in my code failed).
PPS: I tried doubles insted of floats (not all calculations, but my a
, b
& c
are doubles now) & problem did not disappear.
Updated: I made a mistake - confused order of the orders of a
, b
& c
. So "b^2
is number of order about 1E+16
, & 4ac
is about 1E+28
" was wrong. Now it fixed to 1E+24
both. (I've wrote this to the already written comments were understandable)
Update#2: "The problem" section is concretized.
Update#3: Real case of values (for reference): Note: as "accurate values" here I mark values calculated manually in Excel.
a == 1.43963872E+8
b == 3.24884062357827E+12
c == 1.83291898112689E+16
//floats:
b^2 == 1.05549641E+25
4ac == 1.05549641E+25
D == 0.0
root:
y = -1.12835273E+4
//doubles:
b^2 == 1.0554965397412443E+25
4ac == 1.0554964543412880E+25
D == 8.5399956328598733E+17
roots:
y1 == -1.1280317962726038E+4
y2 == -1.1286737079932651E+4
//accurate values:
b^2 == 1.05549653974124E+25
4ac == 1.05549645434129E+25
D == 8.53999563285987E+17
roots:
y1 == -1.128031796E+4
y2 == -1.128673708E+4
It looks like Ok with doubles, but it's not, 'cos here I gave only part of calculations - here I start from same a, b & c values, but their real values in my code are also calculated. And contain inaccuracity, which yields problems even with doubles.