Article View: comp.std.c
Article #3740Re: Casting float to int - standard bug?
From: James Kanze US/ESC 60/3/141 #407...
Date: Wed, 16 Aug 1995 11:43
Date: Wed, 16 Aug 1995 11:43
49 lines
2734 bytes
2734 bytes
In article <40rjdn$jof@solutions.solon.com> seebs@solutions.solon.com
(Peter Seebach) writes:
|> It is undefined behavior to cast a float into an int, if the value contained
|> in that float is larger than INT_MAX, correct?
|> How are we to test this? There exists (on at least one machine) at least
|> one float greater than INT_MAX which compares less than (float) INT_MAX;
|> in particular, on a 68000 system I use, INT_MAX is 2147483647, but
|> (float) INT_MAX is 2147483648. So, 2147483647.5 would compare less
|> than INT_MAX, but would (in fact) be too large.
On the machine in question, of course, casting both values to double
should work. This is *not* guaranteed, however. (Consider a machine
with 64 bit int's and 64 bit double's, for example.)
|> I'm sure there exist machines where this disparity can be pushed further.
|> Can anyone suggest a *safe* way to test for this?
Portably, or non-portably.
I'm pretty sure that I could come up with a non-portable solution for
any given architecture, but I don't know of a portable (conforming)
way off hand.
|> 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"?
I generally just use ``sizeof( long ) * CHAR_BIT + 2''. One character
per bit is enough for *any* base, and the +2 accounts for the sign and
the terminating '\0'. Of course, in most cases, this is overkill. If
you are calculating the size dynamically, then you could use
``(int)floor( log10( (double)( INT_MAX ) ) ) + 1'', or some such;
IMHO, this is just a waste of CPU time.
Another alternative would be to attact the problem from the other
side:
assert( i < 1000 && i > -100) ; /* Only 3 char's in target field */
Again, I can hardly think of an application where this would be useful.
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
Technical Details
Message-ID:
<KANZE.95Aug16134320@slsvhdt.lts.sel.alcatel.de>Path:
arc1-ca.pugleaf.net!xlink.net!slsv6bt!newsReferences:
<40rjdn$jof@solutions.solon.com>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!xlink.net!slsv6bt!news From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) Subject: Re: Casting float to int - standard bug? In-Reply-To: seebs@solutions.solon.com's message of 15 Aug 1995 20:59:19 -0500 Message-ID: <KANZE.95Aug16134320@slsvhdt.lts.sel.alcatel.de> Sender: news@lts.sel.alcatel.de Organization: SEL References: <40rjdn$jof@solutions.solon.com> Date: 16 Aug 1995 11:43:20 GMT