The sudden jump to an entirely wrong value is caused by j
overflowing its int
. You could use an unsigned long
.
Thel loop will probably not reach 0 however, due to the fact, that floating point numbers are just an approximations of a sum of (negative) powers of 2. Such sums will not decrease when multiplying by 10 and then subtracting the integer part.
The best way would be having a fixed number of digits, multiplying with 10n and then chopping trailing zeroes.