News:

  • June 30, 2026, 11:02:02 PM

Login with username, password and session length

Author Topic: MATH with signed and unsigned values  (Read 7513 times)

ERokc

  • Hero Member
  • *****
  • Posts: 118
MATH with signed and unsigned values
« on: March 28, 2015, 05:46:52 PM »
I'm not getting the results I want from a MATH box, why?

-58250+$CTRIO_000_C1F1.iReg2 stored in D3 = -58145  This is correct

(-58250+$CTRIO_000_C2F1.iReg2)-V19   (V19=3570) result stored in D14 = 0 This is incorrect

The MATH box puts the parentheses in, not me.

Are these incompatible values?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3833
    • Host Engineering
Re: MATH with signed and unsigned values
« Reply #1 on: March 28, 2015, 06:38:10 PM »
I'm not getting the results I want from a MATH box, why?

-58250+$CTRIO_000_C1F1.iReg2 stored in D3 = -58145  This is correct

(-58250+$CTRIO_000_C2F1.iReg2)-V19   (V19=3570) result stored in D14 = 0 This is incorrect

The MATH box puts the parentheses in, not me.

Are these incompatible values?
One is using C1, the other is using C2.  I'm guessing the 2nd one should be using $CTRIO_000_C1F1.iReg2 also (not $CTRIO_000_C2F1.iReg2)

ERokc

  • Hero Member
  • *****
  • Posts: 118
Re: MATH with signed and unsigned values
« Reply #2 on: March 29, 2015, 02:00:39 PM »
Both counters are stepped by the same quad encoder. C2F1 is offset by V19 at various positions.
I don't understand why the MATH box is computing a zero result.  Just asking if signed and unsigned numbers are both handled by the MATH.

At the start of the run the counters are the same C1F1=C2F1

Just looking for the cause of the zero result.

Does using an unsigned variable cause the result to be unsigned?

Update: I changed the MATH box, removing -V19  They now are identical except the result are in different locations.
Must have something to do with the MATH being in the same rung maybe?

-58250+$CTRIO_000_C1F1.iReg2 stored in D3 = -58145  This is correct The CTRIO counter will vary around 50.
-58250+$CTRIO_000_C1F1.iReg2 stored in D14 = 0  This is not correct

I will split it into different rungs and see what happens.
Cancel that.  I found a different method to get it done.

I'm curious why two MATH boxes with the same expression in the same rung give different results.
« Last Edit: March 29, 2015, 06:50:14 PM by ERokc »

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3833
    • Host Engineering
Re: MATH with signed and unsigned values
« Reply #3 on: March 29, 2015, 07:41:41 PM »
You can mix sign, unsigned, and even real in one MATH.  Not sure what your issue might be.

Some other possibilities:
Check your Cross Reference and make sure nothing else is writing to D14.
Make sure you don't have any HMI writing to D14.
If you use array references as output in some instruction, e.g. writing to D[V0], make sure you are not doing anything to cause an "indirect write" to D14 (i.e. when V0 equals 14).

The best thing to do is to stick that rung at the top of $Main, stick an END coil just below it, and make sure that if those are the only two instructions being ran in the whole PLC, that you still get the wrong (or right) answer.

FYI: The parentheses are put in there so that the user knows what the PLC is going to actually do.  It doesn't really change the behavior, but just clarifies the order of operation (not everybody knows operator precedence rules in a MATH expression).