Android Example TIP: Replace your Floats by Doubles

It may happen sometimes that, if your dealing with complex non-integer calculations, your code may not perform exactly the way you want to. I came across this problem when I was trying so create a solar system simulation, my Earth wouldn't achieve a stable orbit until I replaced all the FLOAT variables by DOUBLES.

I'm guessing it's way more precise. Erel might enlighten us on this.
EDIT: I think I've found the answer: http://www.java-samples.com/showtutorial.php?tutorialid=261
Float is 32bit while Double is 64bit. If we learned anything from the 90's is that more bits = more power!! :D

B4X:
'Before
Dim EarthMass = 5.972 * Power(10, 24) As Float     'in KG
B4X:
'After
Dim EarthMass = 5.972 * Power(10, 24) As Double    'in KG
 
Last edited:

wonder

Expert
Licensed User
Longtime User
For me, using Float variables to do calculations is 'none sense' because you loose a lot of accuracy with almost no memory trouble.
A lesson I should have learned one year ago... :rolleyes:
 
Top