|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Re: How does division work?
Quote:
|
|
#2
|
|||
|
|||
|
Re: How does division work?
Quote:
Still, I must agree that with embedded systems where there is extremely limited CPU/Memory that stuff does count. |
|
#3
|
||||
|
||||
|
Re: How does division work?
Just to throw in my two cents here:
first for the OP - integer division ALWAYS truncates (at least it does it most of the major languages in use today). If you need more significant digits, by far the easiest solution is to typecast your variables (both the divisor and the dividend) - int a=1; int b=2; float c=((float) a)/((float) b); gives you c = 0.5. Second, to the discussion on bitwise operations and performance: For these robots, you really don't need to be that concerned with performance. If you follow the KISS (keep it simple, stupid) strategy, you're code won't be long enough of complicated enough to make performance an issue. When it comes to the robot controller, the difference between doing something one way or another might be a few milliseconds, but the human operator is only operating on the scale of seconds, so the level of performance you might gain is going to be lost, unless you've made your code overly complex. Last year, our code was probably two-three times more complex than it needed to be, because we were using it as a teaching tool more than anything else, and the same thing will be true this year. Even with that, though, there was absolutely no detectable performance degradation to the robot. Yes, in some systems performance really does matter - i remember back in school making an AI for class and fine tuning everything, down to the point where each game state was sized to exactly fit into the processor cache and using optimizations for the specific brand/model of the processor being used, and in that case it did pay off in noticeable quantities. It's good as a programmer to be aware of performance, and even to be aware of the performance characteristics of the libraries you're using, but you also need to know when you need to apply that knowledge and when you don't. |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
|||
|
|||
|
Re: How does division work?
Quote:
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 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 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); 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; (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. |
|
#6
|
||||
|
||||
|
Re: How does division work?
Quote:
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. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |