🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3737

Re: Casting float to int - standard bug?

#3737
From: Paul Eggert
Date: Wed, 16 Aug 1995 02:28
27 lines
1403 bytes
seebs@solutions.solon.com (Peter Seebach) writes:

> How are we to test this?

I'm afraid there's no way to test whether a floating value exceeds
INT_MAX that works on all conceivable conforming implementations.

You might try comparing ((long double) f) to ((long double) INT_MAX);
this comparison works on all implementations I know about.

> how can one determine the maximum number of digits one will need
> to print an integer?

If you don't need the exact maximum, but can settle for a conservative
estimate that may be slightly too large, then you can use the following idea,
which I first saw in code written by Arthur David Olson.  Again, it's
not guaranteed on all conceivable conforming implementations,
but it works on all implementations I know about.

/*
** 302 / 1000 is log10(2.0) rounded up.
** Subtract one for the sign bit;
** add one for integer division truncation;
** add one more for a minus sign.
*/
#define INT_STRLEN_MAXIMUM(type) \
	((sizeof(type) * CHAR_BIT - 1) * 302 / 1000 + 2)
Technical Details
Message-ID: <40rl4b$3ah@light.twinsun.com>
Path: arc1-ca.pugleaf.net!in2.uu.net!twinsun!not-for-mail
References: <40rjdn$jof@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!twinsun!not-for-mail
From: eggert@twinsun.com (Paul Eggert)
Newsgroups: comp.std.c
Subject: Re: Casting float to int - standard bug?
Date: 15 Aug 1995 19:28:27 -0700
Organization: Twin Sun Inc, El Segundo, CA, USA
Message-ID: <40rl4b$3ah@light.twinsun.com>
References: <40rjdn$jof@solutions.solon.com>
NNTP-Posting-Host: light.twinsun.com