home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume24 / yabbawhap / part02 / percent.h < prev    next >
C/C++ Source or Header  |  1991-10-09  |  970b  |  25 lines

  1. /* Placed into the public domain by Daniel J. Bernstein. */
  2.  
  3. /* This is one of those functions that everyone needs occasionally */
  4. /* but nobody wants to write: computing what percentage x/y is, where */
  5. /* x and y are unsigned longs. Sure, you can do it in floating point, */
  6. /* but would you bet your sister on the accuracy of the result? Sure, */
  7. /* you can just compute ((100 * x) + (y/2)) / y, but what are you */
  8. /* going to do about overflow? percent() is the answer. Unless I've */
  9. /* flubbed something, percent() will never overflow, will always */
  10. /* return a properly rounded value, and will never take too long. */
  11.  
  12. #ifndef PERCENT_H
  13. #define PERCENT_H
  14.  
  15. long percent();
  16.  
  17. /* long percent(a,b,limit) unsigned long a; unsigned long b; long limit; */
  18. /* returns the correctly rounded value of 100a/b */
  19. /* returns 100 if b is 0 */
  20. /* returns limit if the result is limit or larger */
  21. /* limit must be at least 100 */
  22. /* note that halves are rounded up */
  23.  
  24. #endif
  25.