Article View: comp.sys.powerpc
Article #3141Re: Debate: PowerMac "trounces" Pentium....???
From: Kevin Brown
Date: Tue, 20 Feb 1996 08:11
Date: Tue, 20 Feb 1996 08:11
85 lines
3206 bytes
3206 bytes
In article <nevin-1902962045260001@150.135.1.79>,
Nevin ":-]" Liber <nevin@CS.Arizona.EDU> wrote:
>In article <kevinbDMv1Br.2C1@netcom.com>, kevin@frobozz.sccsi.com (Kevin
>Brown) wrote:
>
>> You've got to be kidding. This is kiddie stuff...
>> In case you were wondering, the magic "241 / 100" in the BUFLEN
>> expression comes from log(256), which is the number of base-10 digits
>> required to represent a single byte.
>
>Sorry Kevin, but a "byte" under ANSI C is *not* guaranteed to be 8
>bits.
Ooops....you're right. Guess I should have looked that one up, eh?
Fortunately, there seems to be a resolution...
>For a bunch of people (Dan Pop, yourself, etc.) claiming that it is
>trivial to write portable ANSI C code,
I don't think I made that claim, but I will say that writing portable
ANSI C code isn't a *terribly* difficult thing to do...limiting, yes,
but not difficult.
>you are certainly having a very
>tough time getting this code correct.
Well, hang on...that was just a first attempt, so it's hard to say
that I'm "having a very tough time", particularly since I knocked up
the first version in about 5 minutes...
Try this version instead:
#include <limits.h>
#include <stdlib.h>
/*
* 31/100 ~== log(2) == number of base 10 digits required to store a binary
* digit.
*/
#define BUFLEN ((sizeof(int) + 1) * CHAR_BIT * 31 / 100 + 3)
char *itoa ( int x )
{
char *buf, *p, *p2;
int negative = 0;
static char nums[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9' };
if ( ! ( buf = malloc(BUFLEN) ) )
return NULL;
p = buf + BUFLEN - 1;
if ( x < 0 )
{
x = -x;
negative = 1;
}
*p-- = 0;
do
*p-- = nums[x % 10];
while ( x /= 10 );
if ( negative )
*p = '-';
else
p++;
if ( p != buf )
{
p2 = buf;
do
*p2++ = *p;
while ( *p++ )
}
return buf;
}
This one should be unportable only if CHAR_BIT isn't guaranteed by the
ANSI standard to be defined in limits.h. I've even taken care of the
issue of non-ASCII character sets.
--
Kevin Brown kevin@frobozz.sccsi.com
This is your .signature virus: < begin 644 .signature (9V]T8VAA(0K0z end >
This is your .signature virus on drugs: <>
Any questions?
Technical Details
Message-ID:
<kevinbDn2Eqp.4MG@netcom.com>Path:
arc1-ca.pugleaf.net!netcom.com!kevinbReferences:
<nevin-0602962253080001@150.135.1.79> <4fqdta$rhh@meaddata.lexis-nexis.com> <kevinbDMv1Br.2C1@netcom.com> <nevin-1902962045260001@150.135.1.79>Show Full Headers
Complete Article Headers
Newsgroups: comp.sys.powerpc,comp.os.linux.advocacy,comp.sys.mac.advocacy,comp.os.os2.advocacy,comp.sys.amiga.advocacy,comp.sys.intel Path: arc1-ca.pugleaf.net!netcom.com!kevinb From: kevin@frobozz.sccsi.com (Kevin Brown) Subject: Re: Debate: PowerMac "trounces" Pentium....??? Message-ID: <kevinbDn2Eqp.4MG@netcom.com> Sender: kevinb@netcom20.netcom.com Reply-To: kevin@frobozz.sccsi.com (Kevin Brown) Organization: Frobozzco International References: <nevin-0602962253080001@150.135.1.79> <4fqdta$rhh@meaddata.lexis-nexis.com> <kevinbDMv1Br.2C1@netcom.com> <nevin-1902962045260001@150.135.1.79> Date: Tue, 20 Feb 1996 08:11:13 GMT X-Original-Newsgroups: comp.sys.powerpc,comp.os.linux.advocacy,comp.sys.mac.advocacy,comp.os.os2.advocacy,comp.sys.amiga.advocacy,comp.sys.intel