home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16882 < prev    next >
Encoding:
Text File  |  1992-11-20  |  1.9 KB  |  44 lines

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