|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
Re: Floating Points and rounding
Quote:
JBot |
|
#2
|
||||
|
||||
|
Re: Floating Points and rounding
Quote:
The answer has already been provided here. If you need 1/10 accuracy, just multiply all of your numbers by 10. Also, if you're trying to multiply something by a fraction, but the result only needs to be an integer, keep this trick in mind. If you're multiplying by a fraction, you can multiply by the numerator first and then divide by the denominator afterwards to avoid floating point (just be careful of overflows). So when someone here was talking about multiplying by 180/127, do this instead: (x * 180) / 127 |
|
#3
|
|||
|
|||
|
Re: Floating Points and rounding
The compiler can do some of the floating point calculations you might like to do keep your code readable. Note I said compiler, not the controller. The calculations are done once at compile time, and the resulting long, short, char value is used in the code.
Here is an example using an encoder to control distance moved in autonomous code: In xxx.h the following macros are defined: #define COUNTS_PER_REVOLUTION 100.0 #define PI 3.14159 #define WHEEL_DIAMETER 5.25 #define INCHES_TO_COUNTS( inches ) ( inches / ( PI * WHEEL_DIAMETER ) ) * COUNTS_PER_REVOLUTION enum MoveDirection { MoveForward, MoveRotateCW, MoveRotateCCW, MoveReverse }; void Move( enum MoveDirection directionParam, unsigned char speedParam, unsigned short distanceParam ); In yyy.c is a section of a switch statement used to implement a state machine for autonomous mode case 3: // move forward 45 inches Move( MoveForward, 0x20, INCHES_TO_COUNTS( 45.0 ) ); break; The macro INCHES_TO_COUNTS allows the compiler to do the floating point math once at compile time to determine how many encoder counts are required for the wheel to move 45 inches. Now assuming a wheel base of 18 inches how would you code the macro for DEGREES_TO_COUNTS so that we could write: case 4: // turn right 90 degrees Move( MoveRotateCW, 0x20, DEGREES_TO_COUNTS( 90.0 ) ); break; Last edited by charrisTTI : 16-01-2007 at 09:16. Reason: expand example |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Rounding numbers | teh_pwnerer795 | Programming | 13 | 17-12-2006 12:16 |
| pic: Team 228 and the floating tetra | artdutra04 | Extra Discussion | 8 | 02-05-2005 19:58 |
| pic: YMTC: 150 points or 100 points? | CD47-Bot | Extra Discussion | 4 | 25-03-2004 01:53 |
| 5 points I make..(and a little excess) | archiver | 1999 | 7 | 23-06-2002 22:18 |
| Awards and points... | Ian W. | Rules/Strategy | 4 | 24-03-2002 13:30 |