adccommunitymod (AutomationDirect) asked a question.

Comparing negative numbers

Created Date: August 17,2004

Created By: timyoung

**** This post has been imported from our legacy forum. Information in this post may be outdated and links contained in the post may no longer work.****

I have an application where an alarm trigger can be from -20 deg C to 10 deg C based on operator input from a touch screen. I figured the Compare Real Number instruction whould do the trick. Problem is the screen (Red Lion) only does BCD or signed decimal. So I'm trying to get this signed decimal into a real number format and I feel like I'm jumping through hoops. The BTOR instruction does not handle signed decimal. Here's what I have but is there a better way? //Load in the trip point in signed decimal LD V2205 //If it is negative then take the 2's compliment AND B2205.15 INV ADDB K1 //Output the value into a holding register //This is hoop jumping because the INV instruction is 32 bit while any ADD instructions are 16 bit. STR SP1 OUT V3506 LDD V3506 //Convert to real number format BTOR OUTD V3504 //If the signed decimal value is negative //then make the real number negative RST B3505.15 AND B2205.15 SET B3505.15 //Now I can do my comparison STR SP1 LDR V3502 CMPR V3504 See what I mean?


  • adccommunitymod (AutomationDirect)

    Created Date: August 17,2004

    Created by: timyoung

    I have an application where an alarm trigger can be from -20 deg C to 10 deg C based on operator input from a touch screen. I figured the Compare Real Number instruction whould do the trick. Problem is the screen (Red Lion) only does BCD or signed decimal.

    So I'm trying to get this signed decimal into a real number format and I feel like I'm jumping through hoops. The BTOR instruction does not handle signed decimal. Here's what I have but is there a better way?

    //Load in the trip point in signed decimal

    LD V2205

    //If it is negative then take the 2's compliment

    AND B2205.15

    INV

    ADDB K1

    //Output the value into a holding register

    //This is hoop jumping because the INV instruction is 32 bit while any ADD instructions are 16 bit.

    STR SP1

    OUT V3506

    LDD V3506

    //Convert to real number format

    BTOR

    OUTD V3504

    //If the signed decimal value is negative

    //then make the real number negative

    RST B3505.15

    AND B2205.15

    SET B3505.15

    //Now I can do my comparison

    STR SP1

    LDR V3502

    CMPR V3504

    See what I mean?

    Expand Post
  • adccommunitymod (AutomationDirect)

    Created Date: August 17,2004

    Created by: franji1

    I would think what you have would work. Please use Data View and tell me the values you are seeing for V3506 as signed decimal (it should equal the absolute value of V2205).

    Likewise, tell me what you are seeing when you display V3504 as a REAL - it should equal the ACTUAL value of V2205 (as a floating point #).

    One possibility - what is the ACTUAL instruction in the next to last instruction. You have LDR V3502 - this is impossible because LDR is only for loading constant real values (e.g. R3.14159). If you have LDR R3502 , then you are always comparing the temperature to 3502.0 (not the value in V3502). If you have LD V3502, you are comaring it to the IEEE floating point representation of 0000(V3502) in the accumulator. You MUST use LDD V3502 ( load double ) to load a REAL value into the accumulator. Just a shot...

    Expand Post
  • adccommunitymod (AutomationDirect)

    Created Date: August 17,2004

    Created by: timyoung

    Hi fanji1,

    I was hoping you'd answer ;-) Sorry for the confusion. What I have works. I was just wondering if there's an easier way. I usually don't have to go through such contortions to convert a number format.

    Tim

  • adccommunitymod (AutomationDirect)

    Created Date: August 18,2004

    Created by: Andy

    Would it work to just add a offset to the number to keep it out of the negative range, perform the check and then remove the offset if needed?

    AndyT

  • adccommunitymod (AutomationDirect)

    Created Date: August 18,2004

    Created by: franji1

    Andy's idea is a good one. The result of

    A> B

    is always the same as

    (A + K)> (B + K)

    The same is true for subtracting on both sides, but NOT always true when multiplying or dividing (remember your algebra, when you multiply or divide an inequality by a NEGATIVE number you must reverse the inequality).

    So like Andy recommends, if your range is -20 to +10, just offsetting by 20 (or even 100 or 1000) would make the comparison work. Oooh - you could offset by 273 and use Kelvin http://forum1.automationdirect.com/board/wink.gif

    Expand Post
  • adccommunitymod (AutomationDirect)

    Created Date: August 18,2004

    Created by: Tom Jenkins

    In situations like this I usually convert the temperature to absolute degrees and do all of my comparisons and ratios and so on in that. For °F add 460 to get °R. For °C add 273 to get °K.

  • adccommunitymod (AutomationDirect)

    Created Date: August 18,2004

    Created by: timyoung

    Thanks guys,

    I like the offset idea. It's an easier hoop to jump through ;-)

    Tim

  • adccommunitymod (AutomationDirect)

    Created Date: August 23,2004

    Created by: JimHoward

    Here's the logic I came up with to convert a signed decimal (V5000) to a real (V5004). It's just about as much "hoop jumping " as Tim's logic !

    If the signed decimal is negative,

    load the signed decimal (V5000)

    Multiply it by -1 (FFFF = -1)

    Mask of the upper 16 bits with an ANDD

    Now convert it to REAL

    Multiply the positive real number by -1

    The result is a negative real number (V5004)

    B5000.15

    ----| |---------------| LD V5000 |

    | MULB KFFFF |

    | ANDD KFFFF |

    | BTOR |

    | MULR R-1 |

    | OUTD V5004 |

    If the signed decimal is positive,

    load the signed decimal (V5000)

    Convert it to REAL

    The result is a positive real number (V5004)

    B5000.15

    ----|/|---------------| LD V5000 |

    | BTOR |

    | OUTD V5004 |

    Jim

    Expand Post