Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 20 Mar 1999 00:39:05 +1000
From:      Greg Black <gjb@comkey.com.au>
To:        spinner.rflab@t-online.de (Steffen Hein)
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: gnu C compiler 
Message-ID:  <19990319143905.18703.qmail@alpha.comkey.com.au>
In-Reply-To: <36F24371.37ACAF58@t-online.de>  of Fri, 19 Mar 1999 12:30:41 GMT
References:  <36EFA9ED.5126E95C@t-online.de> <19990317210724.9018.qmail@alpha.comkey.com.au> <36F24371.37ACAF58@t-online.de> 

next in thread | previous in thread | raw e-mail | index | archive | help
> > > How can you explain me that the same gnu C compiler ( release 2.7.2.1 )
> > > in FreeBSD apparently does not handle the long double format correctly
> > > while in Linux it does ?
> 
> > Learn to use gcc's warnings (at the very least -Wall), and
> > you'll see where your code is incorrect.
> 
> > printf( "\n LDBL_MIN     = % Le ", LDBL_MIN );
> > 
> > This is wrong, as gcc would have told you with -Wall: you are
> > passing a `double' to printf() but telling it you are passing a
> > `long double'.  If you lie to the compiler, then it will get its
> > revenge.
> > 
> > If you cast LDBL_MIN and friends to the correct type, then your
> > program will run to completion.
> 
> The comment merely states that the gnu C compiler does not comply here
> with the ANSI standard, according to which the format instructions are
> legitimate.

You don't understand the Standard.  In your code, you have used
the "%Le" conversion, which specifies that the relevant argument
will be a `long double'.  However, the argument you provide, in
this particular example `LDBL_MIN', is -- despite its name -- not
a `long double' but a plain `double'.  The only way to make it
the correct `long double' type is with a cast, e.g.:

    printf( "\n LDBL_MIN     = % Le ", (long double) LDBL_MIN );

> ( Cf. chapter 'The ANSI Runtime Library' in Darnell & Margolis, ISBN
> 0-387-97389-3, p.453, Springer )

I use the Standard, since other people's interpretations are
frequently wrong.

> - At any rate, Linux cooperates correctly.

I have never seen the Linux <float.h> file, so I don't know how
it defines `LDBL_MIN'.  In any case, the Standard leaves the
definition somewhat open: it's an implementation-defined
expression whose value must be equal to or less than 1e-37.  The
FreeBSD header has:

    #define DBL_MIN	2.2250738585072014E-308
    #define LDBL_MIN	DBL_MIN

Those two lines comply with the Standard.  Note that, if the
second line was:

    #define LDBL_MIN	2.2250738585072014E-308L

your code would have been correct since, in this imaginary
definition, `LDBL_MIN' is a `long double'.  But, since the real
definition has it as a `double', your code is wrong under
FreeBSD (and unwise under any implementation).

It's a mistake to imagine that the C Standard aims to be fully
prescriptive about the details of all these values.  You have to
actually check them for each implementation, because the details
are specified as `implementation-defined'.

-- 
Greg Black <gjb@acm.org>



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990319143905.18703.qmail>