From owner-freebsd-chat Sun Aug 29 18:44: 3 1999 Delivered-To: freebsd-chat@freebsd.org Received: from scientia.demon.co.uk (scientia.demon.co.uk [212.228.14.13]) by hub.freebsd.org (Postfix) with ESMTP id 17AB714DD3 for ; Sun, 29 Aug 1999 18:43:57 -0700 (PDT) (envelope-from ben@scientia.demon.co.uk) Received: from lithium.scientia.demon.co.uk ([192.168.0.3] ident=exim) by scientia.demon.co.uk with esmtp (Exim 3.032 #1) id 11LF8y-000AJ2-00; Mon, 30 Aug 1999 01:17:52 +0100 Received: (from ben) by lithium.scientia.demon.co.uk (Exim 3.032 #1) id 11LF8x-0009lB-00; Mon, 30 Aug 1999 01:17:51 +0100 Date: Mon, 30 Aug 1999 01:17:51 +0100 From: Ben Smithurst To: Mark Ovens Cc: chat@freebsd.org Subject: Re: Correct casting in ANSI C Message-ID: <19990830011750.A37445@lithium.scientia.demon.co.uk> References: <19990830001201.C265@marder-1> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.95.6i In-Reply-To: <19990830001201.C265@marder-1> Sender: owner-freebsd-chat@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org 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