🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3756

Re: Casting float to int - standard bug?

#3756
From: Mark Brader
Date: Thu, 17 Aug 1995 00:27
24 lines
1439 bytes
First, the original statement that 

| It is undefined behavior to cast a float into an int, if the value
| contained in that float is larger than INT_MAX, correct?

is wrong.  It should say, "is larger than INT_MAX by at least 1".
If INT_MAX is 2147483647, then 2147483647.5 in a floating-point type
*can* be safely converted.

As to the question of comparing an int and a float possibly greater
than INT_MAX, Lars Wirzenius (wirzeniu@cc.Helsinki.FI) writes:

> Couldn't sprintf+strtol be used for this?  strtol has well-defined behaviour
> for overflows.  (It's not elegant, of course.)
 
Which I like.  Another variant would be to first check the signs, then
determine suitable field widths, sprintf() the float *and* the int, and
strcmp() them!
-- 
Mark Brader             |  "I like to think of [this] as self-explanatory."
msb@sq.com              |  "I hope *I* think of [it] that way."
SoftQuad Inc., Toronto  |             -- Donald Westlake: "Trust Me On This"

This article is in the public domain.
Technical Details
Message-ID: <1995Aug17.002724.21144@sq.com>
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!torn!sq!msb
References: <40rjdn$jof@solutions.solon.com> <KANZE.95Aug16134320@slsvhdt.lts.sel.alcatel.de> <40t18j$e50@kruuna.helsinki.fi>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!torn!sq!msb
From: msb@sq.com (Mark Brader)
Subject: Re: Casting float to int - standard bug?
Message-ID: <1995Aug17.002724.21144@sq.com>
Organization: SoftQuad Inc., Toronto, Canada
References: <40rjdn$jof@solutions.solon.com> <KANZE.95Aug16134320@slsvhdt.lts.sel.alcatel.de> <40t18j$e50@kruuna.helsinki.fi>
Date: Thu, 17 Aug 1995 00:27:24 GMT