Reputation: 374
In python the function math.log(1000, 10)
returns
2.9999999998 or some approximate value (neraly every third integer does that)
Which firstly is kind of messed up even though I imagine there's not much (except divisibility tests) to do about it.
And secondly it's not the value I want of course, how should I proceed? Casting to int will clearly return 2 and not 3... So what method is used to get the round to nearest int? In this case and in general, please.
Upvotes: 3
Views: 839
Reputation: 374
Someone removed his/her answer before I could accept it, so I write mine which is no more than a summary.
Two options that I liked:
In this particular case, since the operation was math.log(1000, 10)
, it could be replaced with math.log10(1000)
which shows much greater precision.
In a more general case, round(math.log(1000, 10)) will round 2.999... to the integer 3 so this would be more what was asked.
Upvotes: 1