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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.


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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now