rounding numbers in C

Thread Starter

hondabones

Joined Sep 29, 2009
123
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
 

someonesdad

Joined Jul 7, 2009
1,583
Surely the floor() function will have the effect of truncating the number to 3 decimal places?
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.
 

Thread Starter

hondabones

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

Rich (BB code):
#include <stdio.h>
#include <math.h>

int main(void)
{
     double var
     double round_var
     
     round_var = round(var * 1000)/1000;
     
     return(0);
}
Thanks to all who contributed to my learning.
 

kubeek

Joined Sep 20, 2005
5,795
Just to remind you, you initially wanted it to do
123.456789 -> 123.456
the function you have does
123.456789 ->123.457
 

Thread Starter

hondabones

Joined Sep 29, 2009
123
Just to remind you, you initially wanted it to do
123.456789 -> 123.456
the function you have does
123.456789 ->123.457
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. :)
 
Top