home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18255 < prev    next >
Encoding:
Text File  |  1992-12-12  |  3.3 KB  |  101 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gatech!news.ans.net!cmcl2!kramden!stanleyr
  3. From: stanleyr@kramden.nyu.edu (Rick Stanley)
  4. Subject: Re: Rounding floats to X significant digits
  5. Message-ID: <stanleyr.724176076@kramden>
  6. Keywords: rounding float significant digits
  7. Sender: notes@cmcl2.nyu.edu (Notes Person)
  8. Nntp-Posting-Host: kramden.acf.nyu.edu
  9. Organization: New York University
  10. References: <1992Dec11.171533.2643@ncar.ucar.edu> <1992Dec11.172414.3449@ncar.ucar.edu>
  11. Distribution: usa
  12. Date: Sat, 12 Dec 1992 16:01:16 GMT
  13. Lines: 86
  14.  
  15.  
  16. ethan@earl.scd.ucar.edu (Ethan Alpert) writes:
  17.  
  18. >I apologize if this appears in a FAQ.
  19.  
  20. >I was wondering if anyone has any interesting techniques for rounding
  21. >floating point numbers to a certain number of significant digits?
  22.  
  23. >I'm not talking a certain number of characters but a certain number
  24. >of significant digits, decimal places.
  25.  
  26. >There doesn't appear to be a standard C library routine for this.
  27.  
  28. >Sprintf only does it for digits *left* of the decimal place and not
  29. >for the entire number.
  30.  
  31. >Here's what I came up with and I'm still not sure it works.
  32.  
  33. >There has to be a better way to do it. 
  34.  
  35. >#include <stdio.h>
  36. >#include <math.h>
  37.  
  38. >float roundit(float a, int sig_digit)
  39. >{
  40. >    char buf[20];
  41. >    char format[20];
  42. >    float exp;
  43. >    float tmp1;
  44. >    
  45.  
  46. >    sprintf(format,"%%.%df",sig_digit);
  47. >    if(a > 1.0) {
  48. >        exp = (float)log10((double)a);
  49. >        exp = (float)ceil((double)exp);
  50. >        tmp1 = (float)pow((double)10.0,(double)exp);
  51. >        sprintf(buf,format,a/tmp1);
  52. >        return((float)atof(buf)*tmp1);
  53. >    } else {
  54. >        sprintf(buf,format,a);
  55. >        return((float)atof(buf));
  56. >    }
  57. >} 
  58.  
  59. How about the following, which works well for me.
  60.  
  61. /* -----------------------------------------------------------------------
  62. FILE: Round.cpp          AUTHOR: Rick Stanley             CREATED: 10/10/91
  63. DESCRIPTION: Round a double number to n decimal places.  for use in rounding
  64. off dollar amounts to 2 decimal places for increased accuracy in calculations
  65. and display.  I can't trust the accuracy of printf() using floats.
  66. --------------------------------------------------------------------------
  67. Copyright (C) 1991, Rick Stanley (212) 569-9304  All Rights Reserved
  68. -------------------------------------------------------------------        */
  69. #include <stdio.h>
  70. #include <math.h>
  71.  
  72. double round(double value, int precision);
  73.  
  74. double round(double value, int precision)
  75. {
  76.     double result, power;
  77.     long ltemp;
  78.     power = pow(10.0, (double) precision);
  79.     result = (value * power + .5);
  80.     ltemp = (long) result;
  81.     result = ((double) ltemp) / power;
  82.  
  83.     return result;
  84. }
  85.  
  86. /* Where precision is the number of decimal places to round for,
  87.    dollars = round(dollars, 2); to round to 2 decimal places.  */
  88.  
  89. I've seen this reduced to one line of code, but prefer to do it this way.
  90. I believe this will work using negative precision thus rounding the
  91. left side of the decimal point.  I have not used it that way.  Test it!
  92.  
  93. -------------------------------------+-----------------------------------
  94. Rick Stanley                         | R.S. Consultants  (212) 569-9304
  95. stanleyr@acf14.nyu.edu               | 
  96. Information Technologies Institute   |        C++ & C Language
  97. New York University   NYC, NY  USA   | Programming, Training, Consulting
  98. -------------------------------------+-----------------------------------
  99.  
  100.  
  101.