floating point arithmetics it's not trivial and considering your problem i prefer to answer your question with some tip.
You can solve this with a library such as GMP or MPFR , and this is a good FAQ for both.
If you really want to master this, on almost every major programming language, you should absolutely start from reading the IEEE 754.