C/C++ quality of implementation: floating-point constant conversion

Nelson H. F. Beebe beebe@math.utah.edu
Tue Feb 24 17:31:00 GMT 1998


Background: A local user reported yesterday that gcc did not catch an
instance of a too-large floating-point constant.  Investigations this
morning on numerous systems showed that this is indeed the case, but
that other compilers than gcc also exhibit this behavior.

It is my belief that, with a good-quality compiler, the test file
below should ALWAYS elicit a warning because of loss of range in
conversion, and then print an infinity.

The actual test results recorded below are surprisingly varied.
Adding -Wall or -pedantic to the gcc compilations does not produce any
warning.

RECOMMENDATION:
	C and C++ compilers should check that constants to be
	converted from decimal to binary do not overflow or underflow;
	if they do, then a compile-time warning should be issued. In
	the event of overflow on hosts with IEEE 754 arithmetic, an
	infinity should be stored.  For an underflow below the
	smallest representable floating-point number (a denorm in IEEE
	754 arithmetic), a zero should be stored.

/***********************************************************************

gcc and g++ are all version 2.8.0, except on IBM AIX 4.1, where it is
Cygnus egcs-2.90.23 980102 (egcs-1.0.1 release) (similar to gcc
2.8.0).

lcc is version 4.0, except on the NeXT system, where it is 1.9.

Vendor-provided native compilers are those distributed with the
indicated O/S level.

------------------------------------------------------------------------
Vendor O/S		Compiler	Warning?	Output
------------------------------------------------------------------------
DEC Alpha OSF/1 3.2	c89		No		3.40282e+38
DEC Alpha OSF/1 3.2	cc		No		3.40282e+38
DEC Alpha OSF/1 3.2	cxx -x cxx	Yes		0.00000e+00
DEC Alpha OSF/1 3.2	g++		No		3.40282e+38
DEC Alpha OSF/1 3.2	gcc		No		3.40282e+38
DEC Alpha OSF/1 3.2	lcc		core dump	no executable produced


DECstation ULTRIX 4.3	cc		No		Infinity
DECstation ULTRIX 4.3	g++		No		Infinity
DECstation ULTRIX 4.3	g++		No		Infinity
DECstation ULTRIX 4.3	gcc		No		Infinity
DECstation ULTRIX 4.3	gcc		No		Infinity
DECstation ULTRIX 4.3	lcc		No		Infinity


IBM AIX 4.1		c89		No		INF
IBM AIX 4.1		g++		No		INF
IBM AIX 4.1		gcc		No		INF
IBM AIX 4.1		xlC		No		INF
IBM AIX 4.1		xlc		No		INF


HP 9000/735 HP-UX 10.01	CC		Yes		+.+0000e+01
HP 9000/735 HP-UX 10.01	c89		Yes		+.+0000e+01
HP 9000/735 HP-UX 10.01	cc		Yes		+.+0000e+01
HP 9000/735 HP-UX 10.01	g++		No		+.+0000e+01
HP 9000/735 HP-UX 10.01	gcc		No		+.+0000e+01
HP 9000/735 HP-UX 10.01	gcc -Wall	No		+.+0000e+01


NeXT Mach 3.3		cc (gcc 2.5.8)	No		+Infinity
NeXT Mach 3.3		cc++ (gcc 2.5.8) No		+Infinity
NeXT Mach 3.3		g++		No		+Infinity
NeXT Mach 3.3		gcc		No		+Infinity
NeXT Mach 3.3		lcc		No		+Infinity


SGI IRIX 5.3 		CC		Yes		Inf
SGI IRIX 5.3 		cc		Yes		Inf
SGI IRIX 5.3 		g++		No		inf
SGI IRIX 5.3 		gcc		No		inf
SGI IRIX 5.3 		lcc		No		Inf


SGI IRIX 6.4 		CC		Yes		Inf
SGI IRIX 6.4 		cc		Yes		Inf
SGI IRIX 6.4 		g++		No		inf
SGI IRIX 6.4 		gcc		No		inf


Sun Solaris 2.5		CC		No		Inf
Sun Solaris 2.5		cc		Yes		no executable produced		
Sun Solaris 2.5		g++		No		Inf
Sun Solaris 2.5		gcc		No		Inf
Sun Solaris 2.5		lcc		No		Inf


Sun Solaris 2.6		CC		No		Inf
Sun Solaris 2.6		c89		Yes		no executable produced		
Sun Solaris 2.6		cc		Yes		no executable produced		
Sun Solaris 2.6		g++		No		Inf
Sun Solaris 2.6		gcc		No		Inf


Sun SunOS 4.1.3		CC		No		Inf
Sun SunOS 4.1.3		acc		Yes		Inf
Sun SunOS 4.1.3		cc		No		Inf
Sun SunOS 4.1.3		g++		No		Inf
Sun SunOS 4.1.3		gcc		No		Inf
Sun SunOS 4.1.3		lcc		No		Inf
------------------------------------------------------------------------

***********************************************************************/

#include <stdio.h>

int
main()
{
    float x;

    x = 9.99e99;
    printf("x = %15.5e\n", x);

    return (0);
}

----------------------------------------------------------------------------
- Nelson H. F. Beebe                  Tel: +1 801 581 5254                 -
- Center for Scientific Computing     FAX: +1 801 581 4148                 -
- University of Utah                  Internet e-mail: beebe@math.utah.edu -
- Department of Mathematics, 105 JWB                   beebe@acm.org       -
- 155 S 1400 E RM 233                                  beebe@ieee.org      -
- Salt Lake City, UT 84112-0090, USA  URL: http://www.math.utah.edu/~beebe - 
----------------------------------------------------------------------------



More information about the Gcc-bugs mailing list