Question: I'm developing a 3D space game, which is using alot of math formulas, navigation, ease effects, rotations, huge distances between planets, objects mass, and so on...

My Question is what would be the best way in doing so using math. Should I calculate everything as integers and obtain really large integers(over 20 digits), or use small numbers with decimals.

In my experience, math when using digits with decimals is not accurate, causing strange behavior when using large numbers with decimals.

Answer: 
I would avoid using decimals. They have known issues with precision: <http://floating-point-gui.de/>

---

I would recommend using integers, though if you need to work with very large integers I would suggest using a big number or big integer library such as one of these:

* <http://jsfromhell.com/classes/bignumber>
* <https://silentmatt.com/biginteger/>

The downside is you have to use these number objects and their methods rather than the primitive Number type and standard JS operators, but you'll have a lot more flexibility with operating on large numbers.

**Edit:**

As [le\_m](https://stackoverflow.com/users/1647737/le-m "le_m") pointed out, another downside is speed. The library methods won't run as fast as the native operators. You'll have to test for yourself to see if the performance is acceptable.
The distance from our Sun to Alpha Centauri is 4.153×1018 cm. You can represent this value well with the `Number` datatype which stores values up to 1.7977×10308 with about 17 significant figures.

However, what if you want to model a spaceship stationed at Alpha Centauri?

Due to the limited precision of `Number`, you can either store the value 4153000000000000000 or 4153000000000000500, but nothing in between. This means that you would have a maximal spacial resolution of 500 cm at Alpha Centauri. Your spaceship would look really clunky.

Could we use another datatype than `Number`? Of course you could use a library such as [BigNumber.js](https://github.com/MikeMcl/bignumber.js/) which provides support for nearly unlimited precision. You can park your spaceship one milimeter next to the hot core of Alpha Centauri without (numerical) issues:

```js
pos_acentauri = new BigNumber(4153000000000000000);

pos_spaceship = pos_acentauri.add(0.1); // one milimeter from Alpha Centauri

console.log(pos_spaceship); // 4153000000000000000.1
```

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/2.3.0/bignumber.min.js"></script>
```

However, not only would the captain of that ship burn to death, but your 3D engine would be slow as hell, too. That is because `Number` allows for really fast arithmetic computations in constant time, whereas e. g. the `BigNumber` addition computation time grows with the size of the stored value.

Solution: Use `Number` for your 3D engine. You could use different local coordinate systems, e. g. one for Alpha Centauri and one for our solar system. Only use `BigNumber` for things like the HUD, game stats and so on.