home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / mac / programm / 20169 < prev    next >
Encoding:
Text File  |  1992-12-22  |  1.8 KB  |  39 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!mojo.eng.umd.edu!russotto
  3. From: russotto@eng.umd.edu (Matthew T. Russotto)
  4. Subject: Re: abs(-32768) = -32768 in Th C
  5. Message-ID: <1992Dec22.021829.6976@eng.umd.edu>
  6. Date: Tue, 22 Dec 92 02:18:29 GMT
  7. Organization: Project GLUE, University of Maryland, College Park
  8. References: <Dec.21.20.22.18.1992.22767@remus.rutgers.edu>
  9. Keywords: Think C
  10. Lines: 27
  11.  
  12. In article <Dec.21.20.22.18.1992.22767@remus.rutgers.edu> ficara@remus.rutgers.edu (ken ficara) writes:
  13. >I'm having a problem with the library function abs in Think C 5.0.4.
  14. >It's supposed to accept an int as argument. INT_MIN is defined as
  15. >-32768. But when I take the abs of -32768, I get -32768 back. I
  16. >realize that this is because the two's complement of 0x8000 is 0x8000
  17. >with 2-byte ints. But shouldn't a function that receives an int deal
  18. >with any possible in that could be passed to it? I mean, this is a
  19. >classic boundary case.
  20.  
  21. It only has to deal with numbers in its domain.  The domain of abs can
  22. be defined as -32767..32767.  More to the point, since 'abs' returns
  23. an int and not a long, its range doesn't include 32768, so it couldn't
  24. possibly work correctly.
  25.  
  26. >Corllary: when I say "printf("%d\n",INT_MIN);" I get "-32768." But
  27. >when I open up <limits.h> and look at it, it says "#define INT_MIN
  28. >(~32767)". Huh?
  29.  
  30. Think C's parser is a teensy bit brain damaged.  It can't represent
  31. the constant -32768 as a 2-byte int.  So it uses (~32767) to get the
  32. same bit pattern. (It can't use 0x8000 because that is of type
  33. unsigned int)
  34. -- 
  35. Matthew T. Russotto    russotto@eng.umd.edu    russotto@wam.umd.edu
  36. Some news readers expect "Disclaimer:" here.
  37. Just say NO to police searches and seizures.  Make them use force.
  38. (not responsible for bodily harm resulting from following above advice)
  39.