Date: Mon, 30 Aug 1999 01:17:51 +0100 From: Ben Smithurst <ben@scientia.demon.co.uk> To: Mark Ovens <mark@ukug.uk.freebsd.org> Cc: chat@freebsd.org Subject: Re: Correct casting in ANSI C Message-ID: <19990830011750.A37445@lithium.scientia.demon.co.uk> In-Reply-To: <19990830001201.C265@marder-1> References: <19990830001201.C265@marder-1>
next in thread | previous in thread | raw e-mail | index | archive | help
Mark Ovens wrote: > To achieve *strict* ANSI compliance, what is the correct way to > use cast(s) in the following code. > > gcc doesn't complain, even with ``-Wall'', Or `-Wall -ansi -pedantic'. > but Sun's ANSI C compiler > gives a warning like "the semantics of SCONV change in ANSI C; use > an explicit cast". Do I need to cast each int (& the literal 1000), > or just cast the whole expression? > > int i = 123, j = 57, k = 500; > double d; > > d = i / j + k / 1000; Hmm. I think you can just cast one part of each division, to make it do it in floating point, or something. How about d = (double)i / j + (double)k / 1000; or, somewhat cleaner, d = (double)i / j + k / 1000.0; Otherwise it will just do an integer division (or so it appears -- I'm not an expert at this sort of thing). It seems you need to get some floating point into each division. You might like to go and ask this in comp.lang.c, where all the ANSI C experts hang out. Incidentally, your mail came through with lines padded out to 80 characters with trailing spaces, did you break something? I don't think it's my end, I haven't noticed it on anything else. -- Ben Smithurst | PGP: 0x99392F7D ben@scientia.demon.co.uk | key available from keyservers and | ben+pgp@scientia.demon.co.uk To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-chat" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990830011750.A37445>