How to do rounding without using Math.h lib

Posted on 1998-09-21
Last Modified: 2012-08-13
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
Question by:kkwangl
  • 2
  • 2
  • 2
  • +1

Accepted Solution

Bonev earned 20 total points
ID: 1252941
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);


Expert Comment

ID: 1252942
Of course this has the problem that int usually has a smaller range of allowable values than int...
LVL 84

Expert Comment

ID: 1252943
It also has the problem of not working for negative arguments
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.


Expert Comment

ID: 1252944
I agree with that.

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


Author Comment

ID: 1252945
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.

LVL 84

Expert Comment

ID: 1252946
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)

Expert Comment

ID: 1252947
kkwangl you know binary right

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
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)
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)


Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question