943,948 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 15241
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 15th, 2006
0

Re: round off number

Click to Expand / Collapse  Quote originally posted by WaltP ...
I'm with bumsfeld on this one. You want as much precision as you can get throughout the program's calculations and only round for the display. You don't even want to round the number itself, just the display.

If you round prematurely, all your calculations will be slightly off. And the more calculations you do, the more inaccurate your answer.
It may be quite possible that the data rounded off is not used for calculations or used in calcultions which requires only the rounded off values. It could also be written to a file and used only for data analysis purposes, or maybe the OP had some other idea in his mind or maybe it was part of an assignment given at the college to test his coding skills, you never know.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Oct 15th, 2006
0

Re: round off number

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
It may be quite possible that the data rounded off is not used for calculations or used in calcultions which requires only the rounded off values. It could also be written to a file and used only for data analysis purposes, or maybe the OP had some other idea in his mind or maybe it was part of an assignment given at the college to test his coding skills, you never know.
True -- but with all the if...when...maybe's that get posted here (and you pointed out in this post), noone would get any answers because few would want to post a response based on all the if...when...maybe's to cover all bases. I will respond to what is directly posted and may make an educated guess as to the usage. If I guess wrong, then the OP obviously didn't communicate his problem/needs well enough. His fault, not mine.


So listen up, noobs! Post enough information that we don't have to guess at what you need to know. You need to tell us! That way us folks that are trying to help you pass your classes won't get into arguments like this one!
:p
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,741 posts
since May 2006
Oct 16th, 2006
0

Re: round off number

Click to Expand / Collapse  Quote originally posted by WaltP ...
So listen up, noobs! Post enough information that we don't have to guess at what you need to know. You need to tell us! That way us folks that are trying to help you pass your classes won't get into arguments like this one!
:p ;)
lol.. :D I agree with you on that one, old buddies fighting over beginners is indeed not a good sight :)
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Oct 16th, 2006
0

Re: round off number

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
lol.. I agree with you on that one, old buddies fighting over beginners is indeed not a good sight
im sorry guys if i make all of you confuse.... i want it to be round of in the function
because i'll use the returned value for another function

  1. unsigned int CalculatePercentage(int TC,int PC){
  2. return 100.0f*PC/TC;
  3. }
will the code be enough? even if i use unsigned int will i still get a decimal value and round it off?
Reputation Points: 16
Solved Threads: 0
Light Poster
vbcielle is offline Offline
26 posts
since Sep 2006
Mar 18th, 2010
-1
Re: round off number
Click to Expand / Collapse  Quote originally posted by vbcielle ...
how will i round off the return value to decimal place with tis function?

  1. unsigned int CalculatePercentage(int TotalComponent, int PartialComponent){
  2. return int((PartialComponent/TotalComponent)100);
  3. }

Use floor fun. call which was defined in math.h

ex:
double x=1.5
printf("%ld",floor(x) );

o/p will be 2
instead of 1.5
Reputation Points: 10
Solved Threads: 0
Newbie Poster
navien.kumar is offline Offline
1 posts
since Mar 2010
Mar 18th, 2010
0
Re: round off number
To resurrect a 4 year old post with completely wrong information is pretty bad.
  1. double x=1.5
  2. printf("%ld",floor(x) );
will output 1, not 2.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,741 posts
since May 2006
Mar 18th, 2010
1
Re: round off number
Always keeping as much precision as possible is definately not a bad option, however there are others.

For instance there is no point in keeping 15 decimal places of precision if the value represents a real world item with a fixed quantum of precision, for example cash. I can have £6.349999 because cash is not available in that quantum, I must have either £6.34 or £6.35.

The problem is I see cash as having decimal places and force that perception onto the computer where it does not need it. Instead of holding the value in pounds I can hold it in pence, 635p as an integer. Now the physical limit on the quantum change of the value is matched by the computers limit on the quantum change of the variable, 1 in both cases.

Back to the original problem. You are holding a percentage as an integer and want it rounded off to 1 decimal place. Everyone is saying use a float and perform werid calculations to try to round it that are bound to not work quite correctly at some point due to the non-exact nature of floats.

Why not hold it in an integer in units of 1/10 of 1%?

I am not saying this is the right way. That is dependent on your program. What I am saying is if you really want to limit the number of decimal places you are using this is the way to do it. However if in fact limiting the number of decimal places you are using is not that important then as WaltP says above, maintain maximum precision until the very last second you need to, i.e. on display.


One final advantage of using scaled integers instead of floats. if you happen to be programming for a low powered micro-controller it may not even have a floating point library but if it does it will certainly be very costly in processor cycles to use compared to simple integer arithmetic.


EDIT: Sorry I hadn't noticed this was an ancient thread when I post this reply.
Last edited by Banfa; Mar 18th, 2010 at 10:31 am.
Reputation Points: 445
Solved Threads: 72
Posting Pro
Banfa is offline Offline
506 posts
since Mar 2010
Mar 18th, 2010
0
Re: round off number
Click to Expand / Collapse  Quote originally posted by Banfa ...
EDIT: Sorry I hadn't noticed this was an ancient thread when I post this reply.
When answering threads you need to read them first. Especially the last post made. There is no point answering if what you're going to say has already been said. And by reading the thread you might have read my post.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,741 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: aol instant messenger working???????
Next Thread in C Forum Timeline: How do I use a pointer to an array of structures...?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC