Shop OBEX P1 Docs P2 Docs Learn Events
IF condition w/ math — Parallax Forums

IF condition w/ math

AGCBAGCB Posts: 327
edited 2015-03-04 15:58 in Propeller 1
I'm working on a thermostat program and would like to turn on the heat at 1/2 degree below the set point. When I try to add the math (-1/2)to this condition, the heat does not come on when inside temp is below set point
if  IS_Temp < (setTemp-1/2) AND (heatflag==0)  '1/2° below set temp and not already on
      outa[16]~~   'turn on heat relay                           
      heatflag:=1  'set flag

I'd like not to have to use floating point math.

Is my IF condition written correctly?

Sometimes a seemingly simple thing racks my brain.

Thanks
Aaron

Comments

  • Dr_AculaDr_Acula Posts: 5,484
    edited 2015-03-04 14:18
    Just a guess here, but assuming that variables setTemp and IS_Temp are integers, then subtracting half may result in the same number. The half will get rounded to zero and then you subtract zero so setTemp is the same.
    If you want to avoid floating point, one trick is to multiply everything by ten. So 70 degrees (F) might be 700. Then half will become 5 and you can do all the maths as integers.
  • AGCBAGCB Posts: 327
    edited 2015-03-04 14:23
    Thanks
    I'll give that a try
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-03-04 14:26
    I'm with Mike: if you're wanting to deal with fractional values in an integer world then you must express all values in the fractional domain. Your code becomes
    if (istemp < (settemp - 0_5))
        outa[HT_RELAY] := 1
        heatflag := 1
    


    One of the things I like about the Spin compiler is that it allows the underscore character in numbers; I use this in place of a decimal point in situations like this.
  • AGCBAGCB Posts: 327
    edited 2015-03-04 14:28
    Regardless how far the IS_temp is below the setTemp, the heat never comes on when that math is a part of the condition. Only if I remove the math does it come on. I have tried several ways of writing it all to no avail.
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-03-04 14:29
    Because of operator precedence "setTemp-1/2" is the same as "setTemp-(1/2)". The (1/2) will be computed as zero. You could do "IS_TEMP*2 < setTemp*2-1", but logically you could just use "IS_TEMP < setTemp" and get the same result.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-03-04 14:30
    Make sure that you've multiplied istemp and settemp by 10 to be in your fractional domain.
  • AGCBAGCB Posts: 327
    edited 2015-03-04 14:45
    This works
    if  (IS_Temp < setTemp) AND (heatflag==0)
    

    this doesn't
    if  (IS_Temp < (setTemp - 0_5)) AND (heatflag==0)
    

    or using decimal point
  • AGCBAGCB Posts: 327
    edited 2015-03-04 14:47
    OK let me decipher some of these posts. Time out for me.

    Thanks for the replies and I'll be back
  • tonyp12tonyp12 Posts: 1,951
    edited 2015-03-04 14:57
    >if (IS_Temp < (setTemp - 0_5))

    ALL the numbers have to be 10x
    if (IS_Temp*10 < (setTemp*10 - 0_5))

    But best is to just read/store/write all temperature vars as 10x to start with.
    You know that there is a xx.x decimal point in there, but if you don't tell math operation it will be our secret.
    If ever needed to display the value you would force a char(".") in before that last digit.
  • AGCBAGCB Posts: 327
    edited 2015-03-04 15:48
    Great! It works. It took Tonyp12's simple example for my dumb old noggin to see it, even though others said the same.

    Thanks for all the replies. You guys are great!

    Aaron
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-03-04 15:58
    tonyp12 wrote: »
    If ever needed to display the value you would force a char(".") in before that last digit.

    I've posted a "DecPoint" method to the forum several times. It will insert the decimal point for you.


    There are several other methods floating around which do the same thing.
Sign In or Register to comment.