469,327 Members | 1,226 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

get number that is raised to the power of

Hi,
Im not sure if this is more of a math question or a python question. I
have a variable in python:
>>v = 10.0**n
is there a way to find the value of n if i know only v aside from
str(v).split('+')[1] ? that seems like too much of a hack and I was
wondering if there was a faster way of doing it?
Thanks for any pointers
Astan

--
"Formulations of number theory: Complete, Consistent, Non-trivial. Choose two."
Animal Logic
http://www.animallogic.com

Please think of the environment before printing this email.

This email and any attachments may be confidential and/or privileged. If you are not the intended recipient of this email, you must not disclose or use the information contained in it. Please notify the sender immediately and delete this document if you have received it in error. We do not guarantee this email is error or virus free.

Jun 27 '08 #1
2 1252
On Fri, 02 May 2008 19:19:07 +1000, Astan Chee wrote:
Hi,
Im not sure if this is more of a math question or a python question. I
have a variable in python:
>>v = 10.0**n
is there a way to find the value of n if i know only v aside from
str(v).split('+')[1] ? that seems like too much of a hack and I was
wondering if there was a faster way of doing it?
That hack isn't even working properly because ``str(10.0**1)`` has no '+'
in its string representation.

Solution:

n = math.log(v, 10)

Ciao,
Marc 'BlackJack' Rintsch
Jun 27 '08 #2
Marc 'BlackJack' Rintsch wrote:
On Fri, 02 May 2008 19:19:07 +1000, Astan Chee wrote:
>Hi,
Im not sure if this is more of a math question or a python question. I
have a variable in python:
> >>v = 10.0**n
is there a way to find the value of n if i know only v aside from
str(v).split('+')[1] ?
Astan, you could try something like (n for n in range(1,100) if
10.0**n==v).next() or like len(str(int(v)))-1
>that seems like too much of a hack and I was
wondering if there was a faster way of doing it?
Faster ?! How fast do you need it to be ?
>
That hack isn't even working properly because ``str(10.0**1)`` has no '+'
in its string representation.
OTOH "%.0e" % (10.0**1) does
....but "%.0e" % (10.0**-1) of course doesn't
>
Solution:

n = math.log(v, 10)

Ciao,
Marc 'BlackJack' Rintsch
Beware of floating point approximations, though. Eg
>>assert log(10**3,10)==3
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
assert log(10**3,10)==3
AssertionError

Cheers, BB
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Matt | last post: by
2 posts views Thread by ashu | last post: by
20 posts views Thread by ravi | last post: by
6 posts views Thread by TDBNDVVDSC | last post: by
reply views Thread by Astan Chee | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.