Article View: comp.std.c
Article #3946Re: Casting float to int - standard bug?
From: halasz
Date: Thu, 07 Sep 1995 13:31
Date: Thu, 07 Sep 1995 13:31
17 lines
1384 bytes
1384 bytes
In article <40rjdn$jof@solutions.solon.com> seebs@solutions.solon.com (Peter Seebach) writes <> On a related note, how can one determine the maximum number of digits one <> will need to print an integer? All modern programmers are, of course, <> making sure that sprintf() destination strings have enough characters. <> Is there a portable way to define "enough characters"? Well, log 10(2) = .30103-. If we hav an 'int'-variant 'T', this is enough: (sizeof(T) * CHAR_BIT + 2) / 3 + 2 . Goind from binarie to decimal one doth better to make the ratio greater than the logarithm, 1/3 (or 28/93 = .301075+). From decimal to binarie 3/10 (that is, 10/3) is enough. But when I know that CHAR_BIT is 8, I uze (sizeof(T) * 5 + 1) / 2 + 2 , for 5/2 is enoughlie greater than (3/10) x 8 = 12/5 . Theze work just as well for 'float's in "E"-format, except that one needs to add more than 2, for the "E" and the signs and the uprounding for, in effect, twain integers and not one.
Technical Details
Message-ID:
<42ms8c$ltc@caip.rutgers.edu>Path:
arc1-ca.pugleaf.net!caip.rutgers.edu!not-for-mailReferences:
<40rjdn$jof@solutions.solon.com> <KANZE.95Aug16134320@slsvhdt.lts.sel.alcatel.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!caip.rutgers.edu!not-for-mail From: halasz@caip.rutgers.edu Newsgroups: comp.std.c Subject: Re: Casting float to int - standard bug? Date: 7 Sep 1995 09:31:56 -0400 Organization: Rutgers University Message-ID: <42ms8c$ltc@caip.rutgers.edu> References: <40rjdn$jof@solutions.solon.com> <KANZE.95Aug16134320@slsvhdt.lts.sel.alcatel.de> NNTP-Posting-Host: caip.rutgers.edu