Jump to content

Getting Absolute value of a variable


Recommended Posts

10 hours ago, hart2hart said:

Is there a way to use the binary variable functions to And, Or, or Xor your way to the absolute value of a variable without using an If to determine if - or + before doing bit manipulation?

Repeat while var<0
    var *= -1
Repeat 1 times

.....

Link to comment

I cannot remember the exact logic but IIRC you XOR a number with 11111111111b or FFFFFFF and then add 1
...but you still need a test to see if it needs it or you will make a positive variable negative.

Edited by larryllix
Link to comment
I cannot remember the exact logic but IIRC you XOR a number with 11111111111b or FFFFFFF and then add 1
...but you still need a test to see if it needs it or you will make a positive variable negative.

Thanks. XOR and then +1 as negatives are stored as two’s complements is what I recalled but it does require an If to determine if negative. I think what I vaguely remember was some operand in assembly language on a VAX. It was do something and branch if in one instruction. Now that I think about it, it was macro-assembler so it was likely implemented in multiple machine language instructions. At least after the move from CISC to RISC processors.

I just created another program so I could determine if negative and multiply by -1.
Link to comment
4 hours ago, hart2hart said:


Thanks. XOR and then +1 as negatives are stored as two’s complements is what I recalled but it does require an If to determine if negative. I think what I vaguely remember was some operand in assembly language on a VAX. It was do something and branch if in one instruction. Now that I think about it, it was macro-assembler so it was likely implemented in multiple machine language instructions. At least after the move from CISC to RISC processors.

I just created another program so I could determine if negative and multiply by -1.

See my inline method in the post above.

Link to comment
Repeat while var<0
    var *= -1
Repeat 1 times
.....
Thanks.  I missed Repeat While being released in V5 FW likely because I didn't move over quickly.  I looked Repeat While up the cookbook and it appears there is no construct like brackets to say what is included in the repeated loop. From your example the work around is to include another Repeat 1 times that ends the loop.  In my case, if the repeat will be inline with additional statements before and after loop, then having the Repat 1 times will just execute those after normally -- correct?
 
difference = 0
difference += some value
difference -= another value
Repeat while difference <0
    Difference *= -1
Repeat 1 times
Run Program 'some program' If
Run Program 'another program' If
 
 I'll try it out this evening but thanks for bringing to my attention! 
  • Like 1
Link to comment
9 minutes ago, hart2hart said:

Thanks.  I missed Repeat While being released in V5 FW likely because I didn't move over quickly.  I looked Repeat While up the cookbook and it appears there is no construct like brackets to say what is included in the repeated loop. From your example the work around is to include another Repeat 1 times that ends the loop.  In my case, if the repeat will be inline with additional statements before and after loop, then having the Repat 1 times will just execute those after normally -- correct?
 
difference = 0
difference += some value
difference -= another value
Repeat while difference <0
    Difference *= -1
Repeat 1 times
Run Program 'some program' If
Run Program 'another program' If
 
 I'll try it out this evening but thanks for bringing to my attention! 

Correct.

BTW: I feel the "Repeat 1 times" construct is an incorrect syntax as "Repeat 1 times" should  mean to do the enclosed line twice (the first time and then the 1 repeat) , NOT a total of once as the repeat parameter number indicates.

"Do  1 times" would have been more syntactically correct.

  • Like 1
Link to comment

Works great. Thanks. I’ve usually got a background project for cold winter days to clean stuff up so I’ll add this to list to look for.

I’m curious what happens if someone creates an infinite loop. Does it run until reboot or is eISY looking for it?

Link to comment
13 hours ago, hart2hart said:

Works great. Thanks. I’ve usually got a background project for cold winter days to clean stuff up so I’ll add this to list to look for.

I’m curious what happens if someone creates an infinite loop. Does it run until reboot or is eISY looking for it?

Yes, many programs use infinite looping to create timed counters and cycle furnace fans and humidifiers etc...

It will run until a power down or interrupted somehow, usually a human editing the program. Many schemes to restart these infinite loops have been used for power up conditions.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...