Solved

How to do rounding without using Math.h lib

Posted on 1998-09-21
7
1,239 Views
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
0
Comment
Question by:kkwangl
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Accepted Solution

by:
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);

0
 
LVL 8

Expert Comment

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

Expert Comment

by:ozo
ID: 1252943
It also has the problem of not working for negative arguments
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:Bonev
ID: 1252944
answers2000,
I agree with that.

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

0
 

Author Comment

by:kkwangl
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.


0
 
LVL 84

Expert Comment

by:ozo
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)
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1252947
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

679 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