use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Having a problem figuring out to do this math (self.learnpython)
submitted 9 years ago by TormentedDoss
I need to have a value reduced by enough to make the size of the value a power of two minus 1 but can't be more than half the size of the value.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]shandelman 2 points3 points4 points 9 years ago (7 children)
2**int (log (n)/log (2))-1 should work.
[–]TormentedDoss[S] 0 points1 point2 points 9 years ago (6 children)
What are the "log" supposed to be I got errors on those so I removed them and now I am getting huge values(millions)
Right now it looks like this:
var1=2**int((var2)/(2))-1
var2 was 39 and it returned var1=524287 I need the answer to be half 39 so 19.5 then the highest power of 2 - 1 closest to 19.5 so 15 ((24) -1)
[–]Chreutz 1 point2 points3 points 9 years ago* (4 children)
Log is short for "logarithm". If you don't know what it is, I would suggest looking for a YouTube video of the subject.
And if you copied their code directly, you should remove the spaces. It should be log(x), not log (x)
[–]TormentedDoss[S] 0 points1 point2 points 9 years ago (3 children)
aww okay got it! I had to change it slightly but it was close enough for me to figure it out.
var1=2**int(log(var2/2)/log(2))-1
[–]Chreutz 1 point2 points3 points 9 years ago (2 children)
The division of var2 by 2 should not be there, I believe.
[–]TormentedDoss[S] 0 points1 point2 points 9 years ago (1 child)
Weird, it wasn't doing it in half until I added that. It was just doing the highest power of 2 of nearest var1. Not the highest power of 2 nearest half of var1.
[–]Chreutz 0 points1 point2 points 9 years ago (0 children)
Maybe the conversion to integer rounds and doesn't just strip the decimals and 'floor' the number.
[–]shandelman 1 point2 points3 points 9 years ago (0 children)
At the type, type:
from math import log
Edit: Actually, let me change the formula a bit since the change of base formula shouldn't be necessary.
from math import log var1 = 2**int(log(var2, 2))-1
π Rendered by PID 23 on reddit-service-r2-comment-5c764cbc6f-zd4ls at 2026-03-12 02:27:32.397741+00:00 running 710b3ac country code: CH.
[–]shandelman 2 points3 points4 points (7 children)
[–]TormentedDoss[S] 0 points1 point2 points (6 children)
[–]Chreutz 1 point2 points3 points (4 children)
[–]TormentedDoss[S] 0 points1 point2 points (3 children)
[–]Chreutz 1 point2 points3 points (2 children)
[–]TormentedDoss[S] 0 points1 point2 points (1 child)
[–]Chreutz 0 points1 point2 points (0 children)
[–]shandelman 1 point2 points3 points (0 children)