rounding numbers in C

Discussion in 'Programmer's Corner' started by hondabones, Apr 10, 2010.

  1. hondabones

    Thread Starter Active Member

    Sep 29, 2009
    123
    1
    I want to perform math equations in C Programming.

    the numbers used by the script are like this:

    123.456789

    I want the script to only use the number to 3 decimal places, like this:

    123.456

    how can I do this?

    Jim
     
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    floor(123.456789*1000)/1000
     
  3. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,383
    495
    Do you want three decimal places in the printf statements?
     
  4. hondabones

    Thread Starter Active Member

    Sep 29, 2009
    123
    1
    No. I want three decimal places within the program, to use for math problems and determining equality.
     
  5. hondabones

    Thread Starter Active Member

    Sep 29, 2009
    123
    1
    Thanks, I will try this. For some reason I didn't pick this up when I was researching on how to do it. It makes more sense to me now.
     
  6. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
  7. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    The usual dodge for this is to add 0.5:

    floor(123.456789*1000 + 0.5)/1000

    But that might not be the way you want to round. For my work, I prefer to use the algorithm to truncate if the last digit is even and round up if the last digit is odd. That gives you more even statistical properties in the long run (but probably only matters to us anal retentives :p).

    John's suggestion about a Round function is fine, but often we work on systems that don't have fancy libraries. You might even be on an embedded system that doesn't have a math library; thus, you can't even use the floor function. Sometimes you have to develop your own library routines, perhaps based on string routines. This is sometimes not bad, as it can produce sharper programmers, but it's extra work.

    Dealing with floating point numbers is harder than it looks. You've got to thoroughly think about your code and write lots of test cases. If you don't, your calculations will fail and you won't be aware of it.

    Thus, for example, the OP should worry about what happens if the number you want to round is negative. You should investigate what the floor() function does in this case and decide if it's what you want. Or wrap things in a fabs() before doing your work.

    Oh, one other tip from the trenches -- if you need to compare floating point numbers for equality, never use ==. Instead, always define a precision epsilon > 0 and make the comparison

    fabs(a - b) > epsilon

    It's a little more work but your code will be more robust. I use this method constantly in test code with asserts.
     
  8. hondabones

    Thread Starter Active Member

    Sep 29, 2009
    123
    1
    This is what I came up with:

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <math.h>
    4.  
    5. int main(void)
    6. {
    7.      double var
    8.      double round_var
    9.      
    10.      round_var = round(var * 1000)/1000;
    11.      
    12.      return(0);
    13. }
    14.  
    Thanks to all who contributed to my learning.
     
  9. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Just to remind you, you initially wanted it to do
    123.456789 -> 123.456
    the function you have does
    123.456789 ->123.457
     
  10. hondabones

    Thread Starter Active Member

    Sep 29, 2009
    123
    1
    Yes, I noticed. This will be fine. The reason I wanted it in the first place was because I was trying to show equality.

    For example:

    123.456789 == 123.456788

    for all intensive purposes the above is true, however the program sees 123.456789 as being greater. Which is also true. I don't need that kind of precision. The code I wrote will determine resonant circuits. Though the two reactances vary by a small amount, the circuit is still at resonant.

    Thank You, kubeek for the heads up. :)
     
Loading...