[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

what's the most sensible way to find the number of digits

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 11
Thread images: 1

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 >>56166327

int 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.
Thread posts: 11
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

If you need a post removed click on it's [Report] button and follow the instruction.
If you like this website please support us by donating with Bitcoin at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties. Posts and uploaded images are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that website. If you need information about a Poster - contact 4chan. This project is not affiliated in any way with 4chan.