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