home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 13060 < prev    next >
Encoding:
Internet Message Format  |  1992-09-01  |  1.2 KB

  1. Path: sparky!uunet!usc!sdd.hp.com!uakari.primate.wisc.edu!ames!agate!benrg
  2. From: benrg@ocf.berkeley.edu (Ben Rudiak-Gould)
  3. Newsgroups: comp.lang.c
  4. Subject: How can I multiply and divide 32-bit fixed-point numbers?
  5. Date: 1 Sep 1992 10:40:53 GMT
  6. Organization: U.C. Berkeley Open Computing Facility
  7. Lines: 27
  8. Message-ID: <17vhblINNi9d@agate.berkeley.edu>
  9. NNTP-Posting-Host: tornado.berkeley.edu
  10.  
  11. I'm using ints as fixed-point numbers with 16 bits of integer and 16
  12. bits of fraction.  What's the best way to multiply and divide two of
  13. these and return a third?  For multiplication, I could do
  14.  
  15. (a>>8) * (b>>8)
  16.  
  17. but that loses (ignores) the lower eight bits of precision of both
  18. variables (which might be significant).  Division is even worse; the best
  19. I can come up with is
  20.  
  21. (a / (b>>8)) << 8
  22.  
  23. but then not only do I lose eight bits of b, I also always end up with a
  24. result that is truncated to 8 bits of fraction, far less than the 1/65536
  25. precision I should be able to get.
  26.  
  27. What's most annoying about this is that I know that the uP I'm using
  28. (i486) returns 64-bit results to 32-bit multiplies and divides, but
  29. C won't let me access them.
  30.  
  31. Is there any solution short of inline assembly?
  32.  
  33. Thanks.
  34.  
  35. --
  36. Ben Rudiak-Gould
  37. benrg@ocf.berkeley.edu
  38.