home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / lib / scaled-num.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-14  |  1.6 KB  |  76 lines

  1. /* scaled-num.c: conversions on ``scaled'' numbers, which are a 32-bit
  2.    word with 16 bits of fraction.
  3.  
  4. Copyright (C) 1992 Free Software Foundation, Inc.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "config.h"
  21.  
  22. #include "scaled-num.h"
  23.  
  24.  
  25. #define SCALED_UNITY (1 << 16)    /* 2^16, represents 1.00000.  */
  26.  
  27.  
  28. /* This prints a scaled number, rounded to five digits.  */
  29.  
  30. void
  31. print_scaled (scaled s)
  32. {
  33.   scaled delta;
  34.  
  35.   if (s < 0)
  36.     {                /* Print sign, if negative. */
  37.       putchar ('-');
  38.       s = -s;
  39.     }
  40.  
  41.   printf ("%u", s / SCALED_UNITY);   /* Print integer part. */
  42.   putchar ('.');
  43.  
  44.   s = 10 * (s % SCALED_UNITY) + 5;
  45.   delta = 10;
  46.  
  47.   do
  48.     {
  49.       if (delta > SCALED_UNITY)
  50.         s += 0100000 - (delta / 2);    /* Round the last digit. */
  51.  
  52.       printf ("%c", '0' + (s / SCALED_UNITY));
  53.       s = 10 * (s % SCALED_UNITY);
  54.       delta *= 10;
  55.     }
  56.   while (s > delta);
  57. }
  58.  
  59.  
  60. const real
  61. scaled_to_real (scaled s)
  62. {
  63.   real r = (real) s / SCALED_UNITY;
  64.  
  65.   return r;
  66. }
  67.  
  68.  
  69. const scaled
  70. real_to_scaled (real r)
  71. {
  72.   scaled s = r * SCALED_UNITY;
  73.  
  74.   return s;
  75. }
  76.