x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 2657

# How to do rounding without using Math.h lib

Do you know how to make a rounding function without using the math.h lib which not using floor() or ceil() function?

If it is posible is it posible use mathematical formula to calculate this rounding function.? using C
0
kkwangl
• 2
• 2
• 2
• +1
1 Solution

Commented:
If you want to round a double number down, you can use this:
double res = (double)(int)argument;

If you want to round it up:
double res = (double)(int)(argument + 0.5);

0

Commented:
Of course this has the problem that int usually has a smaller range of allowable values than int...
0

Commented:
It also has the problem of not working for negative arguments
0

Commented:
I agree with that.

ozo,
It depends on the definition of "rounding".
Of course you can modify that according to your needs.

0

Author Commented:
For the rounding, I did have the problem is when I do rounding of the value of the answer will be differrent from what I expoxt when I type cast the value.
If I assign my answer into a double and do the calculation, the result may be differnet and not get the exect result.
e.g 1.255000 + 1.255000
the result should be 2.510  but when I trancate the value I may get the value like this   2.509 before I can continue to use this value to do the next calculation.

Do any solution for this or is it any rounding source (mathematic algorighm) using string to implement the rounding function.

0

Commented:
Bonev, what definition of "rounding" would round -2.4 to -1.0?

kkwangl, why do you want to truncate a value like 1.255000 + 1.255000 = 2.5099999999999998?
If you wanted a string with the value rounded to 3 decimal places, you could use
sprintf(string,"%.3f",1.255000 + 1.255000);
If you wanted to round to the nearest integer (and to round up when it's 1/2 way in between) then I'm not sure why you don't want to use floor(argument+0.5)
0

Commented:
kkwangl you know binary right

e.g.
1 binary means 1 Base 10
10 binary means 2 Base 10
11 binary means 3 Base 10

Reason for rounding error is computer represents double/float as binary fractions
e.g.
0.01 binary means 0.25 base 10
0.10 binrary means 0.50 base 10
0.11 binary means 0.75 base 10

The reason for these "rounding" errors is because you can't represent the _all_ same fractions in binary as you can in base 10.  Think about 1/3 in base 10 it's .33333..etc.. and will always have a rounding error, same thing with binary numbers.

If you want to totally avoid rounding errors then you need to work in base 10 (e.g. some financial apps do this), which basically means roll your own types (easy in C++, hard to do with good notation in C).  Two typical approaches are
i. BCD (binary code decimal) arithmetic.  Basically you represent the number as a base 10 number including fractions (two base 10 digits per byte)
or
ii. Use integers and multiply up by some factor (say 1000 or 100).  Only immediately before output divide everything by the factor.  For example if the US you might work in cents rather than \$.  The problem with this approach is you might run out of precision (because the range of integers is typically less than for doubles)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.