🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3758

Re: Casting float to int - standard bug?

#3758
From: Thad Smith
Date: Thu, 17 Aug 1995 05:00
26 lines
1670 bytes
In article <40rjdn$jof@solutions.solon.com>,
seebs@solutions.solon.com (Peter Seebach) wrote:
>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.

I suspect that your statement that there exists a float value greater
than INT_MAX and less than (float) INT_MAX is false (at least in the
referenced implementation).  If floats are 32 bits with 24 or so
mantissa bits, 2147483647.5F is not exactly representable.  In
IEEE-754 single format (23 bit significand = 24 bit mantissa) the
largest value < 2147483648 is 2147483520, which is definitely less
than INT_MAX.

If a float value x is > (float)INT_MIN and < (float)INT_MAX, it is in
range for conversion to int.  If it is < (float)INT_MIN or >
(float)INT_MAX, it is out of range.  If it is equal to (float)INT_MIN
or (float)INT_MAX, I don't know how to portably test it for conversion
without invoking undefined behavior.  

Thad
Technical Details
Message-ID: <aoiMwQ9yttBF084yn@csn.net>
Path: arc1-ca.pugleaf.net!teal.csn.net!not-for-mail
References: <40rjdn$jof@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!teal.csn.net!not-for-mail
From: thads@csn.net (Thad Smith)
Newsgroups: comp.std.c
Subject: Re: Casting float to int - standard bug?
Date: 16 Aug 1995 23:00:10 -0600
Organization: T3 Systems
Message-ID: <aoiMwQ9yttBF084yn@csn.net>
References: <40rjdn$jof@solutions.solon.com>
Reply-To: ThadSmith@acm.org
NNTP-Posting-Host: 199.117.27.22