It is hard to know without input, but I think you are working with floating point overflow, because the values ββyou are working with are too large.
For example, if your units are in the SI unit system, then:
venus.mass = 4.87e+24; // kg earth.mass = 5.98e+24; // kg
and the numerator in your equation becomes:
self.mass * other.mass == 2.91047e+49;
The single precision of floating point numbers cannot be greater than 3.4e + 38, so the mass product is considered to be infinity.
Of course, the high exponents partially cancel each other, since the distances are also large (of the order of 1e + 10), and the gravitational constant is small, but if the above product is the final result, all the dependent results are wrong, too. ( -1.0#IND means indefinte and matches NaN , not a number, an invalid floating point number.)
There are several ways to fix this:
Use values ββthat can be safely squared in floating point mode. For example, if you normalize the masses with the mass of the earth, you get numbers around 1.0, which should be safe for calculations. Similarly, an astronomical unit can be a good unit for distances.
Reorder the expression so that you do not overflow intermediate expressions. For example, instead of (m1 * m2) / (d*d) , write (m1 / d) * (m2 / d) .
Use double instead of float. The maximum representable double is approx. 1.8e + 308. Note that this problem does not disappear with duoble , it just gives you more options for work. For example, you must be kind.
source share