View Single Post
  #9   Spotlight this post!  
Unread 04-02-2009, 09:07
Mike Mahar Mike Mahar is offline
Registered User
FRC #0138
 
Join Date: Jan 2007
Location: Amherst, NH
Posts: 64
Mike Mahar will become famous soon enough
Re: Potentiometer and Motor Trouble

I'll assume that this is just an example of the equation and not the actual code you are trying to compile.

This is not a compiler error. It is a characteristic of integer arithmetic. Your code at the top has a slightly different expression.
(a - b)/c
assume
a = 20
b = 9
c = 10
a - b is equal to 11
11/ 10 is equal to 1. Remember that you are working with integers

(a/c) + (b/c) with the same values is
20/10 is equal to 2
9/10 is equal to 0
2 - 0 is equal to 2

This sort of problem will happen with floats as well because floats are just approximations of real numbers. It is considered an error to test for equality in floats. If you aren't testing for equality than I recommend that you do your math in floats and only convert the end result into integers at the end.

Quote:
Originally Posted by Mr.Macdonald View Post
I found a compiler error.

based upon my motors behavior, I believe that upon compilation I get a math error.

if (a+b)/c = d
and (a/c) + (b/c) = e
then d != e

Last edited by Mike Mahar : 04-02-2009 at 09:08. Reason: correct spelling