|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Re: Floating Points and rounding
If you're dealing with the FRC controller, you can't deal with signed or floats (unless something's changed), just integers. You'll have to multiply your numbers by 10^n, n of the number of decimals you want. Please, correct me if I'm wrong, this is my first official year as a programmer.
Last edited by chapmatr : 15-01-2007 at 19:50. |
|
#2
|
|||||
|
|||||
|
Re: Floating Points and rounding
Quote:
JBot |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
Re: Floating Points and rounding
you can most certainly deal with floating point numbers! just be careful about typecasting, because PIC C defaults to integers.... so you have to typecast everything.
multiplying by 180/127 is not a good idea... first of all because the range on the servo is not 180... its either 160 or 210 (i forgot). multiply by the decimal... you wil run into less problems. otherwise... float PAN_DEGREE = (160.0f / 127.0f); TRUE_PAN_DEGREE = (float)PAN_SERVO * PAN_DEGREE; notice the forced decimals and the f. Last edited by Uberbots : 15-01-2007 at 20:07. Reason: wrong ratio |
|
#6
|
|||||
|
|||||
|
Re: Floating Points and rounding
Hmm, I use (for example):
Code:
double PAN_DEGREE = whatever I didn't know about the "f" after the numbers after, but I'd seen that at some point...can't remember where. Thanks, I'll use some of that in my code! JBot |
|
#7
|
||||
|
||||
|
Re: Floating Points and rounding
Quote:
|
|
#8
|
|||
|
|||
|
Re: Floating Points and rounding
If you are only going up to 180 i don't see why a float is really necessary??? why not just multiply by 100? it'll still get you two digit accuracy
i.e: 180.00 = 18000 as for rounding a floating point number down to four decimals... it is not too hard to do, but why would you need to do it? it's not like reducing the number of decimal points will reduce the processing time more than the cost it takes to round the number. I think the easiest way to round to four decimals would probably be to mask off the bits of the less significant digits using "&" and a masking variable (look up bitwise operators) this may also be of use to u: http://en.wikipedia.org/wiki/IEEE_754 |
|
#9
|
||||
|
||||
|
Re: Floating Points and rounding
floats and doubles are both 32bit variables in this compiler!
|
![]() |
| 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 |