Use 'round' instead of 'truncate'. Prelude> let numDigits = (+1) . round . logBase 10 . fromIntegral Prelude> map (numDigits . (10^)) [0..9] [1,2,3,4,5,6,7,8,9,10]
2009/8/22 Roberto López <plasterm...@hotmail.com>: > Ok. I wonder if someone could help me with this problem... > > I want to calculate the number of digits of a positive integer. I was > thinking of ... > > numDigits n = truncate (logBase 10 n) + 1 > > But (logBase 10 1000) = 2.9999999999999996 so numDigits 1000 = 2. > > Maybe adding a small amount > > numDigits n = truncate ((logBase 10 n) + 0.0000000000000005) + 1 > > Prelude> numDigits 100 > 3 > Prelude> numDigits 1000 > 4 > Prelude> numDigits 10000 > 5 > Prelude> numDigits 10000 > 5 > Prelude> numDigits 100000 > 6 > Prelude> numDigits 1000000 > 7 > Prelude> numDigits 10000000 > 8 > Prelude> numDigits 100000000 > 9 > Prelude> numDigits 1000000000 > 9 <---- This is wrong!!!! > Prelude> numDigits 10000000000 > 11 > > Is there a reliable way to calculate the number of digits by means of > logBase? > > > Regards! > > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > -- Eugene Kirpichov Web IR developer, market.yandex.ru _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe