negitive float math


hello,

i having trouble program during subtraction , answer should negative.  the variables float because use trig find angles, made 'math' variables float, not concerned time.  

i using whisker sensors , storing time hit in variables w3t, w5t, etc.  then subtracting times find how long difference between them.  the whole prog seems work except when should negative tdiffh.  i me werid value... example:

w3t = 228721876
w4t = 228376604
and value came (w4t-w3t)
tdiffh = 4294621952
where should -345272

code: [select]
if (w3t == 0 || w5t == 0) { //bad sensor or bad hit try calc
   serial.println("who missed me or you?");
   if (w3t == 0 && w5t == 0) {
     serial.println("edges 0 no angle");//something went wrong
     tdiffh = 0;
   }
   else if (w3t == 0) { // "0 7 9", "0 5 5", "0 7 5"
     tdiffh = w5t - w4t;
     serial.println("1");
   }
   else {// "5 7 0", "5 5 0", "9 7 0"
     tdiffh = w4t - w3t;
     serial.println("2");
    }
 }
 else if (w3t <= w5t) { // "5 7 9" or "5 5 5"
   tdiffh = w4t - w3t;
   serial.println("3");
 }
 else if (w3t > w5t) { //"9 7 5"
   tdiffh = w5t - w4t;
   serial.println("4");
 }
 else {
   serial.println("something happend wrong");//something went wrong... ran out of ifs
 }


i don't result, when did basic program float values , subtraction gave negative numbers fine.  
i enjoying new serial print float numbers in 18 though.  

you don't show declration of variables (w3t, w5t), assuming they're floats may have issue following statement:

code: [select]
if (w3t == 0 || w5t == 0)

generally not considered practice compare float equality. rather should test see if value within approximate range. such as:

code: [select]
if (((w3t >= -0.5) && (w3t <= 0.5))  || ((w5t >= -0.5) && (w5t <= 0.5)))

the problem equality operator requires exact match. though 2 variables differ small fraction not qualify equality.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > negitive float math


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums