what's the most sensible way to find the number of digits in an int (without converting it to a string) in C?
please respond
regards, highschool dropout who can't into math
What made you think you can be a competent programmer without math?
Programming is all math.
>>56166247
i'm working on it senpai
>>56166101
divide by 10 until you reach 0, keep track with a counter starting at 1 if the number != 0
http://stackoverflow.com/questions/6655754/finding-the-number-of-digits-of-an-integer
That solution is more or less what i thought of but its pretty repetitive and wont work if you are using huge numbers
You really shouldn't skip basic math, it's incredibly useful and chances are, you'll waste much more time writing inefficient shit like >>56166327int intlen(int num)
{
return floor(log(num) / log(10)) + 1;
}
>>56166101
take the logarithm base 10 of it and then add one to that,
here's something i did in python.
def digits(x):
print int(log(x,10)) + 1
soo... yeah... idk how you'd do it in c, good luck though, pal.
also since you're a dropout who "can't do math" I want to share a story with you (i'll reply to this post with it)
thanks guise
>>56166377
fuck, i typed it all out and the page refreshed.
tl;dr: look into number theory. I got obsessed with prime numbers and number theory my junior year of high school, my gpa jumped 1.5 points, i got 3 full rides, and now i can appreciate some of the beautiful, elegant world of pure mathematics.
>>56166449
thx senpai :)
>>56166376
Just because it's shorter doesn't mean it's more efficient. The fyl2x instruction on a nehalem processor takes about 80 clock cycles. You use it twice. Then you have to cast your int to float and then back to int. Division catches up to this after about 9-10 digits which is pretty close to the limit of int on most environments. CMP and JMP is 1 cycle so there may be some sense in just if/esle if/else if efficiency is the goal.