🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3736

Casting float to int - standard bug?

#3736
From: Peter Seebach
Date: Wed, 16 Aug 1995 01:59
24 lines
1476 bytes
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'm sure there exist machines where this disparity can be pushed further.

Can anyone suggest a *safe* way to test for this?

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"?

-s
-- 
Peter Seebach - seebs@solon.com || seebs@intran.xerox.com --- C/Unix
proto-wizard -- C/Unix questions? Send mail for help.  No, really!
Copyright 1995 Peter Seebach.  Not for distribution through Microsoft Network.
The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq
Technical Details
Message-ID: <40rjdn$jof@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: seebs@solutions.solon.com (Peter Seebach)
Newsgroups: comp.std.c
Subject: Casting float to int - standard bug?
Date: 15 Aug 1995 20:59:19 -0500
Organization: Usenet Fact Police (Undercover)
Message-ID: <40rjdn$jof@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com