Go to Post Baker, you're making my head hurt and it's only August!!!! - Rich Kressly [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 09-01-2008, 01:52
JohnC's Avatar
JohnC JohnC is offline
my other name is nigel
FRC #0360 (360 Revolution)
Team Role: Programmer
 
Join Date: Mar 2005
Rookie Year: 2005
Location: user_routines.c
Posts: 100
JohnC is a jewel in the roughJohnC is a jewel in the roughJohnC is a jewel in the roughJohnC is a jewel in the rough
Send a message via AIM to JohnC
Re: How does division work?

If you add 0.5, it will "round" to the nearest integer:

int x = (int)(40/9 + 0.5); // 40/9 = 4.444 repeating and rounds down to 4

int y = (int)(40/6 + 0.5); // 40/6 = 6.666 repeating, rounds up to 7
__________________
What place are we at? ... TODAI!
  #2   Spotlight this post!  
Unread 09-01-2008, 10:41
dcbrown dcbrown is offline
Registered User
AKA: Bud
no team
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Hollis,NH
Posts: 236
dcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud of
Re: How does division work?

Quote:
If you add 0.5, it will "round" to the nearest integer:

int x = (int)(40/9 + 0.5); // 40/9 = 4.444 repeating and rounds down to 4

int y = (int)(40/6 + 0.5); // 40/6 = 6.666 repeating, rounds up to 7
Tried this in C18 and the compile still comes up with the wrong answer.


Code:
174:                   var1 = (int)((40/6)+0.5);
 04464    0E06     MOVLW 0x6                                   << compiler gens 6
 04466    0101     MOVLB 0x1
 04468    6FE6     MOVWF 0xe6, BANKED
 0446A    6BE7     CLRF 0xe7, BANKED
Instead the following will generate the expected results:

Code:
175:                   var1 = (int)((40+(6/2))/6);
 0446C    0E07     MOVLW 0x7                                     << compiler gens 7
 0446E    6FE6     MOVWF 0xe6, BANKED
 04470    6BE7     CLRF 0xe7, BANKED
The above is the same as adding .5 to the end result, just before the actual division.

If you try this with variables in the formula instead of constants, then the compiler can't figure out the answer at compile-time and generates the necessary code to figure this out at run-time. Adding 0.5 may cause conversion into and out of floating point which are really slow operations.

A more correct integer based answer would be:

Code:
    var1 = ((var2*10)+(divisor*10)/2))/(divisor*10);
This changes the floating point .5 into an integer 5 and preserves precision. Or you can roll your own integer divide routine and add in rounding based upon the remainder being larger than half the divisor.

However, in the case of an odd divisor such as 9, the remainder can only be 4 or 5 - not 4.5. So this suggests the simplier form below of should yield the correct answer:

Code:
    var1 = ((var2+(divisor/2)) / divisor;
In the examples:
(40+(9/2)) / 9 = 44 / 9 = 4
(40+(6/2)) / 6 = 43 / 6 = 7

or did I mess up... again...

Last edited by dcbrown : 09-01-2008 at 10:46.
  #3   Spotlight this post!  
Unread 09-01-2008, 10:58
Jon Stratis's Avatar
Jon Stratis Jon Stratis is offline
Electrical/Programming Mentor
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Minnesota
Posts: 3,753
Jon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond repute
Re: How does division work?

Quote:
Originally Posted by JohnC View Post
If you add 0.5, it will "round" to the nearest integer:

int x = (int)(40/9 + 0.5); // 40/9 = 4.444 repeating and rounds down to 4

int y = (int)(40/6 + 0.5); // 40/6 = 6.666 repeating, rounds up to 7
Nope - In C (and most languages), integer division will result in immediate truncation, not just truncation at the end. since the dividend and the divisor are both integers, it will calculate 40/9 to be 4, then add 0.5, then truncate as you put it into the integer, giving you 4 for the answer. 40/6 will calculate to 6, add 0.5 and truncate again and you'll get 6.

Instead, what you can try (for your example) is the following:

int x = (int)(40.0/9.0 + 0.5); // 40/9 = 4.444 repeating and rounds down to 4

int y = (int)(40.0/6.0 + 0.5); // 40/6 = 6.666 repeating, rounds up to 7

by adding the ".0" to the divisor and the dividend, you turn the division into a floating point operation, instead of an integer operation. That means that it won't truncate the result of the division, only the final result of the equation as you stick it into an integer. To accomplish this using variables instead of straight numbers, by far the easiest way is to typecast the variables:

int x = (int)(((float)dividend)/((float)divisor) + 0.5);

That tells the compiler that, for this one instance, you want it to treat the variables as floats instead of integers, meaning that it won't truncate any results in mid-computation.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How does it work? Ryan O Control System 5 12-03-2006 19:15
How does Nationals work? coldfusion1279 Championship Event 29 30-03-2005 16:47
How Does a Winch Work? Aaron Lussier Technical Discussion 23 20-01-2004 21:14
how does crab drive work? Soukup Technical Discussion 13 25-04-2003 11:31
How does Nationals work?? LunchBox Championship Event 16 18-03-2003 15:55


All times are GMT -5. The time now is 23:18.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi