Article View: comp.std.c
Article #3747Re: Casting float to int - standard bug?
From: Steve Clamage
Date: Wed, 16 Aug 1995 17:07
Date: Wed, 16 Aug 1995 17:07
19 lines
1403 bytes
1403 bytes
In article jof@solutions.solon.com, seebs@solutions.solon.com (Peter Seebach) writes: > >... 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. The implementation of type float usually does not have enough precision to represent 2147483647.5 exactly. You are thus comparing pre-rounded values which differ only beyond the point where rounding occurs. Try using a floating-point type which has enough precision to represent the values you are comparing. Type long double in a conforming implementation will be big enough. Type double might be big enough. All bets are off if the implementation does not conform to the standard. (The terms "portable" and "standard-conforming" are unrelated.) --- Steve Clamage, stephen.clamage@eng.sun.com
Technical Details
Message-ID:
<40t8kp$ab@engnews2.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!usenetReferences:
<40rjdn$jof@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!usenet From: clamage@Eng.Sun.COM (Steve Clamage) Newsgroups: comp.std.c Subject: Re: Casting float to int - standard bug? Date: 16 Aug 1995 17:07:37 GMT Organization: Sun Microsystems Inc. Message-ID: <40t8kp$ab@engnews2.Eng.Sun.COM> References: <40rjdn$jof@solutions.solon.com> Reply-To: clamage@Eng.Sun.COM NNTP-Posting-Host: taumet.eng.sun.com