home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / ldiv.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  1KB  |  54 lines

  1. /***
  2. *ldiv.c - contains the ldiv routine
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Performs a signed divide on longs and returns quotient
  8. *       and remainder.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <stdlib.h>
  14.  
  15. /***
  16. *ldiv_t div(long numer, long denom) - do signed divide
  17. *
  18. *Purpose:
  19. *       This routine does an long divide and returns the results.
  20. *       Since we don't know how the Intel 860 does division, we'd
  21. *       better make sure that we have done it right.
  22. *
  23. *Entry:
  24. *       long numer - Numerator passed in on stack
  25. *       long denom - Denominator passed in on stack
  26. *
  27. *Exit:
  28. *       returns quotient and remainder in structure
  29. *
  30. *Exceptions:
  31. *       No validation is done on [denom]* thus, if [denom] is 0,
  32. *       this routine will trap.
  33. *
  34. *******************************************************************************/
  35.  
  36. ldiv_t __cdecl ldiv (
  37.         long numer,
  38.         long denom
  39.         )
  40. {
  41.         ldiv_t result;
  42.  
  43.         result.quot = numer / denom;
  44.         result.rem = numer % denom;
  45.  
  46.         if (numer < 0 && result.rem > 0) {
  47.                 /* did division wrong; must fix up */
  48.                 ++result.quot;
  49.                 result.rem -= denom;
  50.         }
  51.  
  52.         return result;
  53. }
  54.