Solved

How to do rounding without using Math.h lib

Posted on 1998-09-21
7
879 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
Comment Utility
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
Comment Utility
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
Comment Utility
It also has the problem of not working for negative arguments
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:Bonev
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 use strings and some functions related to them 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.

744 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

16 Experts available now in Live!

Get 1:1 Help Now