Path: utzoo!utgpu!attcan!uunet!husc6!bbn!bbn.com!mesard From: mesard@bbn.com (Wayne Mesard) Newsgroups: comp.lang.c Subject: Re: float functions Message-ID: <27903@bbn.COM> Date: 3 Aug 88 20:35:32 GMT References: <2890@calmasd.GE.COM> Sender: news@bbn.COM Lines: 51 From article <2890@calmasd.GE.COM>, by wlp@calmasd.GE.COM (Walter L. Peterson, Jr.): > In article <7441@cit-vax.Caltech.Edu>, gtchen@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@BBN.COM BBN, Cambridge, MA