Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Sort of trivial code challenge - may be interesting to you anyway
Date: Thu, 05 Mar 2026 13:49:49 -0800
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <86cy1ic5fm.fsf@linuxsc.com>
References: <10n80sc$3soe4$1@dont-email.me> <10o6vat$258sq$1@raubtier-asyl.eternal-september.org> <10o7438$2737q$2@dont-email.me> <10o92e1$2r6rf$1@raubtier-asyl.eternal-september.org> <10obb37$3jler$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Thu, 05 Mar 2026 21:49:54 +0000 (UTC)
Injection-Info: dont-email.me; posting-host="e10200766ffc466ebfe6a0b9c1907eec"; logging-data="177772"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19raMaVXbNfJC3a3Y0WvoxQ1niMLV0n6tA="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:tPl7t91IhlHkEuhuLCf76DLQl5g= sha1:JTuLiyca8nt9wiiI8n47yAp88mA=
Xref: csiph.com comp.lang.c:396796
scott@slp53.sl.home (Scott Lurndal) writes:
> DFS writes:
>
>> On 3/4/2026 5:44 AM, Bonita Montero wrote:
>>
>> int colwidth = sprintf(cw,"%d",max) + 1;
>
> using ciel(log10(rows*columns)) performs better than snprintf
> in this application.
>
>
> $ printf '%f\n' $(( ceil(log10( 8*8)) ))
> 2.000000
Using snprintf() is more correct and also more obviously correct.
#include
#include
int
main(){
printf( " cutoff snprintf ceil(log)\n" );
printf( " ------ -------- -----------\n" );
for( signed cutoff = 0; cutoff < 12; cutoff++ ){
int a = snprintf( 0, 0, "%d", cutoff );
int b = ceil(log10(cutoff));
printf( " %6d %8d %11d\n", cutoff, a, b );
}
}
gives
cutoff snprintf ceil(log)
------ -------- -----------
0 1 -2147483648
1 1 0
2 1 1
3 1 1
4 1 1
5 1 1
6 1 1
7 1 1
8 1 1
9 1 1
10 2 1
11 2 2
Also the snprintf() way doesn't need -lm. :)