Jump to content
View in the app

A better way to browse. Learn more.

Universal Devices Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Getting Absolute value of a variable

Featured Replies

Posted

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?

Edited by hart2hart

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

.....

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

  • Author
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.
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.

  • Author
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! 
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.

  • Author

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?

On 7/16/2024 at 5:45 AM, larryllix said:

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

"Repeat 0 times" ?

11 hours ago, carealtor said:

"Repeat 0 times" ?

That used to not function at all.

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.

On 7/17/2024 at 6:54 PM, larryllix said:

That used to not function at all.

Ah.  Obviously I never tried it. 

Guest
This topic is now closed to further replies.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.