View Single Post
  #14   Spotlight this post!  
Unread 17-12-2006, 12:16
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: Rounding numbers

A few notes:
  • IIRC, integer vs float division is decided by the operands, not the target. Meaning that
    Code:
    int i = 5.0 / 3.0 * 4.0
    will be 6, while
    Code:
    float i = 5 / 3 * 4
    will be 4.
  • For counters and times, and any other values that don't go negative, you should be declaring them as "unsigned". That is, "unsigned int", "unsigned long", etc. This doubles the upper bound and causes it to wrap to 0, not a negative number.
  • remember the data types: char = 1B, int = 2B, short long = 3B, long = 4B
  • Any time you think a calculation will over flow, cast one of the operands to a long. "((i * 262L) / 10000L)" should work fine for you.

Also, I find a series of typedef's helpful:
Code:
typedef signed char SCHAR;
typedef unsigned char UCHAR;
typedef signed int SINT;
typedef unsigned int UINT;
typedef signed short long SSHORT;
typedef unsigned short long USHORT;
typedef signed long SLONG;
typedef unsigned long ULONG;
Put that in a "types.h", and include it basically everywhere. A good place to define a "bool" type as well.