float functions

Wayne Mesard mesard at bbn.com
Thu Aug 4 06:35:32 AEST 1988


>From article <2890 at calmasd.GE.COM>, by wlp at calmasd.GE.COM (Walter L. Peterson, Jr.):
> In article <7441 at cit-vax.Caltech.Edu>, gtchen at tybalt.caltech.edu (George T. Chen) writes:
>> 
>> How do I get c to recognize a function as returning a float and not
>> a double?  It seems the moment I declare something as a function, the
>> compiler cast it as double.  I am primarily using sizeof to determine
>> the type.
> [stuff about sizeof deleted]
>
> So, don't worry. If you declare f1() as returning a float, it will
> return a float.

No no no!  All floating point arithmetic is done in double-preceision.
See K&R 6.2.  Floats are converted to doubles in expressions wherever
they appear (including return statments).

The following program should convince you that functions declared as
float and double always return double and if necessary are cast to float
in the calling routine.  (K&R doesn't address this specifically, but it
can be inferred from 6.2, from page 69 and by analogy to the char-int
conversion rules.)

==================SNIP=========================

float f()
{
    return(1.234567890123456789012345678901234567890);
}


double d()
{
    return(1.234567890123456789012345678901234567890);
}


main()
{
    float vff, vfd;
    double vdf, vdd;
    printf("%.32f\n%.32f\n", f(), d()); /* Same here */

    vff = vdf = f();
    vfd = vdd = d();
    printf("\n%.32f\n%.32f\n", vff, vdf); /* Different here */
    printf("\n%.32f\n%.32f\n", vfd, vdd); /* And here */
    
}

-- 
unsigned *Wayne_Mesard();        MESARD at BBN.COM           BBN, Cambridge, MA



More information about the Comp.lang.c mailing list