🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3741

Re: Casting float to int - standard bug?

#3741
From: Philip Homburg
Date: Wed, 16 Aug 1995 12:46
42 lines
1795 bytes
In article <40rjdn$jof@solutions.solon.com>,
Peter Seebach <seebs@solutions.solon.com> 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 don't understand this. If 2147483647.5 and 2147483648 are both values that
can be represented exactly by a variable of type float, then why do you convert
the integer 2147483647 to the float 2147483648.0 since 2147483647.5 would
be closer to 2147483647?
Are you sure that a float can represent the value 2147483647.5?

I tried the following program on a sun3 and on a sun4:

#include <stdio.h>
int main()
{
        int i= 2147483647;
        double d= 2147483647.5;
        float fi, fd;

        fi= (float)i;
        fd= (float)d;

        printf("%d -> %f, %f -> %f\n", i, (double)fi, d, (double)fd);
        return 0;
}

The result was:
2147483647 -> 2147483648.000000, 2147483647.500000 -> 2147483648.000000

I guess that (float)2147483647.5 should compare equal to (float)INT_MAX.




						Philip Homburg
Technical Details
Message-ID: <DDEM6B.7tL.0.-s@cs.vu.nl>
Path: arc1-ca.pugleaf.net!cs.vu.nl!philip
References: <40rjdn$jof@solutions.solon.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!cs.vu.nl!philip
From: philip@cs.vu.nl (Philip Homburg)
Subject: Re: Casting float to int - standard bug?
Nntp-Posting-Host: centaur.cs.vu.nl
References: <40rjdn$jof@solutions.solon.com>
Sender: news@cs.vu.nl
Organization: Fac. Wiskunde & Informatica, VU, Amsterdam
Date: Wed, 16 Aug 1995 12:46:59 GMT
Message-ID: <DDEM6B.7tL.0.-s@cs.vu.nl>