سؤال

I am creating a small gravity simulator and I need it to "update" each particles position a small amount.

To do this I am trying to implement the increment of time increase in the equation

D = 1/2 A_g * T^2

to be a very small number such as .001 or even smaller.

In my attempt to do this I used 'float' because the rest of my array's were in that data type but that failed so I need help.

In my research of this topic I came across the variable type of 'BigDecimal' but I am confused on how to use this and I would not like to create a new variable for each one of my particles (Hopefully it can work with just another slot in my array's).

هل كانت مفيدة؟

المحلول

The correct1 answer is most likely to use float. If you use it correctly, float is capable of representing numbers as small as 1.1755e-38 (normalized).

My guess is that you didn't write the float literal correctly. The literal 0.001 is actually a double. If you want a float literal you need to use an f or F suffix; i.e. 0.001f.

(This guess is consistent with the error message ....)


1 - Changing to use double instead of float is also a good answer. The difference is performance and space usage is unlikely to be significant, and the extra precision of double may be beneficial. On the other hand, the dynamics of n-body interactions are unstable. Unless you get really deeply into the mathematics (numerical analysis) of solving the DE's numerically before writing your code, you are likely find that it is not floating point precision that limits the fidelity of your modelling ...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top