Integer Versus Floating Point

In another thread, the user required accuracy and got mired down in the implementation of floating point arithmetic for a processor which has no native floating point processing ability.

On the VEX controller, FP operations can take many hundreds (or many thousands) of machine cycles to execute.

I’d like to show everyone how proper integer arithmetic can be very useful…

Original (floating point) code:

#include "Main.h"

void OperatorControl ( unsigned long ulTime )
{
      int EncoderTimer; 
      int YAxisDrive; 
      int XAxisDrive; 
      int RF = 2; // drive motor
      int RB = 4; // drive motor
      int LF = 1; // drive motor
      int LB = 3; // drive motor
      int DifferenceEncoders; 
      float Circumfrence = 12.566370; 
      float revolutions; 
      float distance; 
      int TerminalWindowTimer; 

      StartTimer ( 1 ) ;
      StartTimer ( 2 ) ;
      PresetTimer ( 1 , 0 ) ;
      PresetTimer ( 2 , 0 ) ;
      while ( 1 )
      {
            YAxisDrive = GetRxInput ( 1 , 2 ) ;
            XAxisDrive = GetRxInput ( 1 , 1 ) ;
            Arcade4 ( 1 , 2 , 1 , LF , RF , LB , RB , 1 , 1 , 1 , 1 ) ;
            EncoderTimer = GetTimer ( 1 ) ;
            TerminalWindowTimer = GetTimer ( 2 ) ;
            RightQuadEncoder = GetQuadEncoder ( 1 , 5 ) ;
            LeftQuadEncoder = GetQuadEncoder ( 2 , 6 ) ;
            // calculating distance traveled 
            if ( TerminalWindowTimer >= 1000 )
            {
                  revolutions = ((RightQuadEncoder + LeftQuadEncoder) / 2) / 90;
                  distance = Circumfrence * revolutions  ;
                  PrintToScreen ( "distance =%d\n" , distance ) ; //I tried all directives %f, etc...
                  //I do not need a rounded off answer, rather I need an accurate one!
                  PresetTimer ( 2 , 0 ) ;
            }

etc.......

An integer math alternative:

#include "Main.h"

// Distance per tick is 100 * PI (22/7) * diameter of wheel (4) / counts per revolution (2 * 90)
// This should be accuarate to 0.04% or about +/-0.06" in 12 feet.
// The result is in 100th of an inch (2000 --> 20 inches)
// If the result is stored in a signed int, the max value is 327 inches before rollover.

#define DISTANCE_PER_TICK_NUMERATOR`    440
#define DISTANCE_PER_TICK_DENOMINATOR   63

void OperatorControl ( unsigned long ulTime )
{
      int EncoderTimer; 
      int YAxisDrive; 
      int XAxisDrive; 
      int RF = 2; // drive motor
      int RB = 4; // drive motor
      int LF = 1; // drive motor
      int LB = 3; // drive motor
      int DifferenceEncoders; 
      int distance;
      int encoder_ticks;
      int TerminalWindowTimer; 

      StartTimer ( 1 ) ;
      StartTimer ( 2 ) ;
      PresetTimer ( 1 , 0 ) ;
      PresetTimer ( 2 , 0 ) ;
      while ( 1 )
      {
            YAxisDrive = GetRxInput ( 1 , 2 ) ;
            XAxisDrive = GetRxInput ( 1 , 1 ) ;
            Arcade4 ( 1 , 2 , 1 , LF , RF , LB , RB , 1 , 1 , 1 , 1 ) ;
            EncoderTimer = GetTimer ( 1 ) ;
            TerminalWindowTimer = GetTimer ( 2 ) ;
            RightQuadEncoder = GetQuadEncoder ( 1 , 5 ) ;
            LeftQuadEncoder = GetQuadEncoder ( 2 , 6 ) ;
            // calculating distance traveled 
            if ( TerminalWindowTimer >= 1000 )
            {
                  encoder_ticks = RightQuadEncoder + LeftQuadEncoder;
                  distance = (encoder_ticks * DISTANCE_PER_TICK_NUMERATOR) / DISTANCE_PER_TICK_DENOMINATOR;
                  PrintToScreen ( "distance =%d\n" , distance ) ; //I tried all directives %f, etc...
                  //I do not need a rounded off answer, rather I need an accurate one!
                  PresetTimer ( 2 , 0 ) ;
            }

etc.......

Lesson over.

Regards,

Mike

I saw that same message and was going to reply with something sort of similar… but it was in a forum I couldn’t reply in. The lack of float (or being forced to use int) isn’t a big deal, it isn’t like your robot is going to move to an accuracy of a few millimeters. Just scale up your int by 10 or 100 and you get .1 or .01 range of accuracy. Plenty for this kind of stuff.

Wouldn’t want it doing surgery or something :slight_smile:

Just visually scanning your code, I see a number of missing () and ; but I realize you are trying to provide an example rather than functional code.

One of the key points is these two lines:

#define DISTANCE_PER_TICK_NUMERATOR` 440
#define DISTANCE_PER_TICK_DENOMINATOR 63

Mike has done some pre-work and figured out the relationships between the robot motion and the tick counters. By using these constants the fractional parts of the calculations are no longer important, but can be discarded.

The point about multiplying up is valid, but if you start multiplying by 1000 or 10000 to make it work then you’ve just moved the problem. From the comment “If the result is stored in a signed int, the max value is 327 inches before rollover.” So it’s still simple small number of bytes math.

Factoring over by 10 or 100 works but taking the next step like Mike did is what makes the code fly.

I found one of each and edited them. Thanks.