home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!stanford.edu!agate!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!sbbrown
- From: sbbrown@magnus.acs.ohio-state.edu (Stephen B. Brown)
- Subject: Re: the abs() function in C
- Message-ID: <1992Nov20.162807.9120@magnus.acs.ohio-state.edu>
- Sender: news@magnus.acs.ohio-state.edu
- Nntp-Posting-Host: top.magnus.acs.ohio-state.edu
- Organization: The Ohio State University Radio Observatory
- References: <pebi.722223424@zephyr.aem.umn.edu> <maurer.722246649@magellan.stanford.edu>
- Date: Fri, 20 Nov 1992 16:28:07 GMT
- Lines: 31
-
- >In <pebi.722223424@zephyr.aem.umn.edu> pebi@aem.umn.edu (Peter A. Bidian) writes:
- >>Isn't abs(-2)=2 in C?
- >>I always get 0.
-
- In article <maurer.722246649@magellan.stanford.edu> maurer@nova.stanford.edu (Michael Maurer) writes:
- >you probably want to say abs(-2.0), if the abs() function expects a
- >floating point argument. or abs((double)-2).
-
- Actually, on all the systems I've experience with, the abs() function
- works on integers and it's the fabs() function which works on floating
- point. I suspect that Peter's problem is actually that he's expecting
- abs(-2.0)==2.0
- to work, and it won't, but
- fabs(-2.0)==2.0
- will. fabs() should probably be called dabs() since it takes a double
- argument and returns a double value, but the distinction is moot in
- many dialects of C.
-
- Of course, if he's using ANSI style function prototype declarations,
- then the compiler can complain about
- abs(-2.0)
- so then he's got another problem. It was actually this example which
- convinced me to convert to using function prototype several years
- ago, as I had some kind of blind spot about fabs() and in many
- situations a K&R compiler won't complain. E.g.
- double f;
- f = sqrt(abs(-2.0));
- will pass, but f is nothing like 1.414...
- --
- Steve Brown, N8HFI sbbrown@magnus.acs.ohio-state.edu
- Chief Engineer, The Ohio State University Radio Observatory ("Big Ear")
-