home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / cephes / cmath / round.c < prev    next >
Encoding:
Text File  |  1992-11-17  |  996 b   |  67 lines

  1. /*                            round.c
  2.  *
  3.  *    Round double to nearest or even integer valued double
  4.  *
  5.  *
  6.  *
  7.  * SYNOPSIS:
  8.  *
  9.  * double x, y, round();
  10.  *
  11.  * y = round(x);
  12.  *
  13.  *
  14.  *
  15.  * DESCRIPTION:
  16.  *
  17.  * Returns the nearest integer to x as a double precision
  18.  * floating point result.  If x ends in 0.5 exactly, the
  19.  * nearest even integer is chosen.
  20.  * 
  21.  *
  22.  *
  23.  * ACCURACY:
  24.  *
  25.  * If x is greater than 1/(2*MACHEP), its closest machine
  26.  * representation is already an integer, so rounding does
  27.  * not change it.
  28.  */
  29.  
  30. /*
  31. Cephes Math Library Release 2.1:  January, 1989
  32. Copyright 1984, 1987, 1989 by Stephen L. Moshier
  33. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  34. */
  35.  
  36.  
  37. double round(x)
  38. double x;
  39. {
  40. double y, r;
  41. double floor();
  42.  
  43. /* Largest integer <= x */
  44. y = floor(x);
  45.  
  46. /* Fractional part */
  47. r = x - y;
  48.  
  49. /* Round up to nearest. */
  50. if( r > 0.5 )
  51.     goto rndup;
  52.  
  53. /* Round to even */
  54. if( r == 0.5 )
  55.     {
  56.     r = y - 2.0 * floor( 0.5 * y );
  57.     if( r == 1.0 )
  58.         {
  59. rndup:
  60.         y += 1.0;
  61.         }
  62.     }
  63.  
  64. /* Else round down. */
  65. return(y);
  66. }
  67.