Java: Is x a Power of 10?

Well, this took awhile to figure out. I wrote a Roman Numeral converter for my Java final and one of the requirements states: Subtract only powers of ten, such as I, X, or C. Writing VL for 45 is not allowed: write XLV instead.

This means I had to see if the number I'm working with was a power of 10. Initially, I did it the manual way like if i == 10 || i == 100, etc but figured there had to be a better way. I asked my physicist friend what math function I'd use and he suggested log. I tried using Java's Math.log function but kept coming up with really weird numbers. Math.pow looked like a good possiblity but I couldn't figure out how to compare 1.0E2 to 1.0.

So from 5:00pm to 8:00pm I tried about a hundred things, talked to about four people and even posted to the Sun Java forums. My always-reliable friend Lee from Microsoft walked me through a few things and explained that Java's log base is e (~ 2.71828) and I needed the base to be 10. How would I get it? The Sun bug database entry for adding pow10 gave me the answer: Math.log(x)/Math.log(10). I implemented that into my code until I scrolled down to the resolution "Being considered as part of larger math library effort in 1.5."

From there, I plugged "pow10 java" into the Google and found that pow10 is a part of StrictMath. The final solution looks something like this: if (StrictMath.log10(index)%1 == 0 && index>1) . Because StrictMath.log10(0)%1 equals 0, I had to add index>1. Instead of using mod, I could have also checked for StrictMath.log10(index) == (int)StrictMath.log10(index).