My question is: Suppose you have computed an algorithm that gives the number of iterations and you would like to print the number of iterations out. But the output always many d
Instead of trying to convert the output into an integer, find out why it is not an integer in the first place, and fix it there.
Did you initialize it as an integer, e.g. num.iterations <- 0L
or num.iterations <- integer(1)
or did you make the mistake of setting it to 0
(a numeric)?
When you incremented it, did you add 1
(a numeric) or 1L
(an integer)?
If you are not sure, go through your code and check your variable's type using the class
function.
Fixing the problem at the root could save you a lot of trouble down the line. It could also make your code more efficient as numerous operations are faster on integers than numerics (an example).