[Added] Power of (X^Y)

Ideas for new features & functions

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

[Added] Power of (X^Y)

Post by Grovkillen » Sat Oct 17, 2020 5:03 am

It would be great if I could do this:

Code: Select all

Let>VALUE=12^2
//VALUE=144
Last edited by Grovkillen on Fri Mar 08, 2024 2:49 pm, edited 1 time in total.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7376
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Power of (X^Y)

Post by Marcus Tettmar » Sat Oct 17, 2020 8:57 am

You can:

Code: Select all

Let>value={12^2}
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Power of (X^Y)

Post by Grovkillen » Sat Oct 17, 2020 7:56 pm

Aha! Didn't know.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Power of (X^Y)

Post by Grovkillen » Fri Aug 13, 2021 9:38 am

I do not find information in the documentation on how to use the "mod" arithmetic operators so I thought I'd post it here...

Code: Select all

Let>Number=4
Let>Remaining={%Number% mod 3}
//Remaining=1
Let>Remaining={%Number% mod 2}
//Remaining=0
Together with the "div" arithmetic operator we can get the fractions of a value instead of decimal value:

Code: Select all

Let>Number1=4
Let>Number2=3
Let>Remaining={%Number1% mod %Number2%}
Let>Whole={%Number1% div %Number2%}
MDL>%Number1%/%Number2% = %Whole% + %Remaining%/%Number2%
These operators can be really useful and should be documented...
Last edited by Grovkillen on Fri Aug 13, 2021 11:36 am, edited 1 time in total.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Power of (X^Y)

Post by Grovkillen » Fri Aug 13, 2021 10:21 am

Here's another example; on how to use the or/and operators to compute bitwise operators of a binary value. It's good way of storing settings or errors in a single variable:

Code: Select all

Let>Result=0

//{1 shl 0} = //binary = 000 000 001 //decimal = 1
//Let>Error=1 would also work for this bit...
Let>Error={1 shl 0}
Let>Result={%Result% or %Error%}
//result = 000 000 001

//{1 shl 1} = //binary = 000 000 010 //decimal = 2
Let>Error={1 shl 1}
Let>Result={%Result% or %Error%}
//result = 000 000 011

//{1 shl 2} = //binary = 000 000 100 //decimal = 4
Let>Error={1 shl 2}
Let>Result={%Result% or %Error%}
//result = 000 000 111

//... add any bit as a 1 for any new error, just remember to shift it to the left to add it to the unique bit

//{1 shl 7} = //binary = 010 000 000 //decimal = 128
Let>Error={1 shl 7}
Let>Result={%Result% or %Error%}
//result = 010 000 111

//to act on a given error, let's say error if bit 2 is a 1
Let>ErrorCode={1 shl 2}
Let>ShouldIAct={%Result% and %ErrorCode%}
//ShouldIAct is set to 4
If>ShouldIAct=ErrorCode
  //if the return value = error code the error bit has been set...
Endif>
IfNot>ShouldIAct=0
  //if this is more logical for you... then use this one
Endif>

//to act on a given error, let's say error if bit 5 is a 1
Let>ErrorCode={1 shl 5}
Let>ShouldIAct={%Result% and %ErrorCode%}
//we haven't set the fifth bit to 1, ShouldIAct=0

//to act on a given error, let's say error if bit 0 is a 1
Let>ErrorCode={1 shl 0}
Let>ShouldIAct={%Result% and %ErrorCode%}
//ShouldIAct=1
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Power of (X^Y)

Post by Grovkillen » Fri Aug 13, 2021 11:04 am

A word of caution:

Code: Select all

//maximum of 62 error to be stored, MS uses double and thus the biggest value is 64 bit long
Let>MaximumBit=62
Let>Result={0 shl %MaximumBit%}
Let>Error={1 shl 0}
Let>Result={%Result% or %Error%}
Let>Error={1 shl 1}
Let>Result={%Result% or %Error%}
Let>Error={1 shl 2}
Let>Result={%Result% or %Error%}
Let>Error={1 shl 7}
Let>Result={%Result% or %Error%}
ArrayDim>ERRORS,MaximumBit
Let>k=-1
While>k<MaximumBit
  Let>k=k+1
  Let>Temp={1 shl %k%}
  Let>Temp={%Result% and %Temp%}
  If>Temp>0
    Let>ERRORS_%k%=1
  Else>
    Let>ERRORS_%k%=0
  Endif>
EndWhile>
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1347
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Power of (X^Y)

Post by Dorian (MJT support) » Wed Jun 07, 2023 2:24 pm

Added to v15.0.23
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
Grovkillen
Automation Wizard
Posts: 998
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Power of (X^Y)

Post by Grovkillen » Thu Jun 08, 2023 5:20 am

Let>ME=%Script%

Running: 15.0.24
version history

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts