Date: Tue, 8 Apr 2003 04:09:34 +0400 From: Alex Semenyaka <alexs@ratmir.ru> To: Peter Pentchev <roam@ringlet.net>, Alex Semenyaka <alexs@ratmir.ru>, freebsd-hackers@freebsd.org Subject: Re: /bin/sh and BIG NUMBERS Message-ID: <20030408000934.GB14719@snark.ratmir.ru> In-Reply-To: <20030407085309.GC527@straylight.oblivion.bg> References: <20030405030629.GA2669@snark.ratmir.ru> <20030406032450.GC4130@gothmog.gr> <20030407085309.GC527@straylight.oblivion.bg>
next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Apr 07, 2003 at 11:53:09AM +0300, Peter Pentchev wrote: > >> -int arith(char *); > >> +long long arith(char *); > >> [snip rest of long-long using patch] >> Nice idea, but we should probably ask the -standards people if we >> can/should make this use uint64_t and %jd instead of `long long' > intmax_t might be a better choice, if %jd is used :) Ok, and then one more issue: what is The Right Way to estimate the maximal length of the resulting number? Since there is the replacement of fmtstr(p, 12, "%qd", result); with fmtstr(p, 21, "%qd", result); in my patch. If we are going to handle the max integer type automatically as with intmax= _t and %jd we need to fix that part as well :) 1) Sure I can replace if with #define ARITH_LEN (int)(2+log10(UMAXINT_MAX)) =2E.. fmtstr(p, ARITH_LEN, "%jd", result); but then we will need to link libm.so library that does not look like good idea. 2) Also we can make a some number of the nested #if's like this: #if UMAXINT_MAX <=3D UINT_MAX #define ARITH_LEN 10 #elif UMAXINT_MAX <=3D ULONGLONG_MAX #define ARITH_LEN 19 =2E.. That's far from ideal as well. 3) I can allocate a LARGE buffer there but it just the stupid waste of memo= ry. 4) Also we can have special file to be compiled and run during in the build= ing sh time such as arith_len.c: ---------->------------>----------- #include <stdio.h> #include <math.h> #include <limits.h> int main(int ac, char *av[]) { printf("#ifndef ARITH_LEN\n#define ARITH_LEN %d\n#endif\n", (int)(log10(UI= NT_MA X))); exit(0); } ----------<------------<----------- and generate file arith_len.h on-the-fly. OR, 5) we can add corresponding constants to the machine/_stdint.h or machine/_inttypes.h. The last way looks like the smartest one for me. Also Stefan E=F1er <se@freebsd.org> wrote me that he did the same job for /bin/expr 3 years ago and suggested to take a look on his job. He added=20 64-bit support to expr as well as overflow checks. I do think that it will be nice to have optional overflow control but optional but nice to have. Have any objections if I will add new switch for /bin/sh like -O which will makes /bin/sh to complain to stderr about overflows? I will prepare the final patch as soon as those two questions will be resol= ved. Thank you all for the participating in this discussion, it is very useful f= or me! SY, Alex
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030408000934.GB14719>