Fastest way to find the largest power of 10 smaller than x

Posted by peoro on Stack Overflow See other posts from Stack Overflow or by peoro
Published on 2010-12-22T21:04:28Z Indexed on 2010/12/22 22:54 UTC
Read the original article Hit count: 195

Is there any fast way to find the largest power of 10 smaller than a given number?

I'm using this algorithm, at the moment, but something inside myself dies anytime I see it:

10**( int( math.log10(x) ) ) # python
pow( 10, (int) log10(x) )   // C

I could implement simple log10 and pow functions for my problems with one loop each, but still I'm wondering if there is some bit magic for decimal numbers.

© Stack Overflow or respective owner

Related posts about algorithm

Related posts about language-agnostic