Two warnings "This local variable has the same name as a global variable" and "ELSE with no matching IF" - variables

I'm getting two weird warnings.
One is "This local variable has the same name as a global variable",
referring to the wx, wy, ww, wh.
Another is "ELSE with no matching IF", referring to the two if-else statements.
Here's the whole script.
#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
screen_scaling_factor := A_ScreenDPI/96
^p::mouse_move_win(200,300)
mouse_move_win(x,y,horizontal:="left",vertical:="top",mouse_click:=""){
wingetpos,wx,wy,ww,wh,a
global screen_scaling_factor
if horizontal="left"{
x1 := wx + x * screen_scaling_factor
}else{
x1 := wx + ww - x * screen_scaling_factor
}
if vertical="top"{
y1 := wy + y * screen_scaling_factor
}else{
y1 := wy + wh - y * screen_scaling_factor
}
DllCall("SetCursorPos", int, x1, int, y1)
}
When I add local as the first line of the function to enable the "force-local mode", however, the first warning disappears.
When I add a parentheses to the if condition or remove the curly braces following it, as shown below, the second warning disappears.
if (horizontal="left"){
x1 := wx + x * screen_scaling_factor
}else{
x1 := wx + ww - x * screen_scaling_factor
}
or
if horizontal="left"
x1 := wx + x * screen_scaling_factor
else
x1 := wx + ww - x * screen_scaling_factor
Any idea why this is happening?

If you want to use the legacy style if statement (please don't) you can't start your braces from the same line as the if statement. You'd have to drop the starting brace { down one line.
But please just use the modern expression style if-statement like so:
mouse_move_win(x,y,horizontal:="left",vertical:="top",mouse_click:=""){
wingetpos,wx,wy,ww,wh,a
global screen_scaling_factor
if (horizontal="left"){
x1 := wx + x * screen_scaling_factor
}else{
x1 := wx + ww - x * screen_scaling_factor
}
if (vertical="top"){
y1 := wy + y * screen_scaling_factor
}else{
y1 := wy + wh - y * screen_scaling_factor
}
DllCall("SetCursorPos", int, x1, int, y1)
}
Also, personally I'd call that brace style disgusting, but of course it's just personal preference haha.
But just in case you didn't know, you can omit braces from one liner if/else statements:
mouse_move_win(x,y,horizontal:="left",vertical:="top",mouse_click:="")
{
wingetpos,wx,wy,ww,wh,a
global screen_scaling_factor
if (horizontal="left")
x1 := wx + x * screen_scaling_factor
else
x1 := wx + ww - x * screen_scaling_factor
if (vertical="top")
y1 := wy + y * screen_scaling_factor
else
y1 := wy + wh - y * screen_scaling_factor
DllCall("SetCursorPos", int, x1, int, y1)
}
EDIT: oh seems you edited your post. I started typing this before you edited it, but then I had to go do something.
Anyway, my answer should answer your questions.

Use mouse_move_win(200, 300) instead of mouse_move(200,300)
When you define horizontal:="left", it means Left is its default value, meaning if no value is supplied it assumes it's left. So
if horizontal=left
is unnecessary.
So I guess your code should be other way around with the if and else combos.
^p::mouse_move_win(200, 300)
mouse_move_win(x, y, horizontal:="left", vertical:="top", mouse_click:="") {
wingetpos,wx,wy,ww,wh,a
global screen_scaling_factor
if horizontal=right
x1 := wx + ww - x * screen_scaling_factor
else
x1 := wx + x * screen_scaling_factor
if vertical=bottom
y1 := wy + wh - y * screen_scaling_factor
else
y1 := wy + y * screen_scaling_factor
DllCall("SetCursorPos", int, x1, int, y1)
}
This worked perfectly for me

Related

How to simplify logical operation in C

I have this following logic which needs simplifing to look clearer and consise:
if (x1 < y1)
return 1;
else if (x1 == y1)) {
if (x2 < y2)
return 1;
else if (x2 == y2) {
if (x3 < y3)
return 1;
} else
return 0;
}
} else
return 0;
In order to solve this above problem, I have applied logical expressions to further simplify these few lines of conditions:
if (x1 < y1 || (x1 == y1 && x2 < y2) || (x1 == y1 && x2 == y2 && x3 < y3))
return 1;
else
return 0;
I am not sure how to simplify further from here. Can anyone help me?
Update:
For further simplifying, I tried applying boolean algebra to this expression, but no luck! I have come up with this:
A + (A1 * B) + (A1 * B1 * C)
Where A denotes to x1 < y1 and A1 denotes to x1 == y2
IMO the cleanest way to do this sort of chained relation in C is with ?::
return x1 == y1 ? x2 == y2 ? x3 < y3 : x2 < y2 : x1 < y1;
As noted in the question comments, if you really care about performance, and the values have limited range, you can use bit twiddling to combine them into a single value for comparison. If the fields are all uint16_t (unsigned 16-bit values), for example, you can do:
uint64_t x = ((uint64_t)x1 << 32) + ((uint64_t)x2 << 16) + x3;
uint64_t y = ((uint64_t)y1 << 32) + ((uint64_t)y2 << 16) + y3;
return x < y;
but this is probably less clear and probably a premature optimization.

AMPL Variables in subscripts are not yet allowed

I really hope someone can help with this...
This is what i have in the .mod file
set I := 1..10;
set J := 1..10;
set K := 1..2;
set W := 1..20;
param v{K, W};
param d{I, J};
var x1, integer;
var y1, integer;
var x2, integer;
var y2, integer;
var assist{W}, binary;
and this is the code generating error:
minimize nome: sum{w in W} (if (assist[w] == 0) then
(if (x1 >= v[1,w]) then
(if (y1 >= v[2,w]) then
(d[x1 - v[1,w],y1 - v[2,w]])....
where the error regards the last line and says:
Variables in subscripts are not yet allowed.
context: (d[x1 - v[1,w],y1 - >>> v[2,w]] <<< )
this is one of constraints (others are just the same):
subject to rangex1:
x1 > 0 && x1 <= 10;
As the error message says, you can't use decision variables within a subscript in AMPL. In this case x1 and y1 are decision variables, so d[x1 - v[1,w],y1 - v[2,w]] is not allowed. You'll need to reformulate the problem in a way that avoids this issue.

Error code not working

Hello I am trying to implement the gate MiniALU but the howard simulator give me this error: "has no source pin". I would be happy if you can help me solve this.
my code-
CHIP MiniALU {
IN
x[16], y[16], // 16-bit inputs
zx, // zero the x input?
zy, // zero the y input?
f; // compute out = x + y (if f == 1) or out = x & y (if == 0)
OUT
out[16]; // 16-bit output
PARTS:
// Zero the x input and y input
Mux16(a[0..15]=x, b[0..15]=false, sel=zx, out[0..15]=x1);
Mux16(a[0..15]=y, b[0..15]=false, sel=zy, out[0..15]=y1);
// Perform f
And16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xandy);
Add16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xaddy);
Mux16(a[0..15]=xandy, b[0..15]=xaddy, sel=f, out[0..15]=out);
}
You are connecting x2 and y2 to the inputs of And16 and Add16 but x2 and y2 are not defined anywhere.
You need to replace x2 and y2 with x1 and y1 in the connections to And16 and Add16.
Correct code:
Mux16(a=x, b=false, sel=zx, out=x1);
Mux16(a=y, b=false, sel=zy, out=y1);
And16(a=x1, b=y1, out=xandy);
Add16(a=x1, b=y1, out=xaddy);
Mux16(a=xandy, b=xaddy, sel=f, out=out);
Your problem was that you wrote y2 and x2 instead of y1 and x2.
Also, there is not need in [0..15]
You wrote:
And16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xandy);
Add16(a[0..15]=x2, b[0..15]=y2, out[0..15]=xaddy);
instead of:
And16(a[0..15]=x1, b[0..15]=y1, out[0..15]=xandy);
Add16(a[0..15]=x1, b[0..15]=y1, out[0..15]=xaddy);

Quaternion addition like 3ds/gmax does with it's quats

A project I'm working on needs a function which mimics 3ds/gmax's quaternion addition. A test case of (quat 1 2 3 4)+(quat 3 5 7 9) should equal (quat 20 40 54 2). These quats are in xyzw.
So, I figure it's basic algebra, given the clean numbers. It's got to be something like this multiply function, since it doesn't involve sin/cos:
const quaternion &operator *=(const quaternion &q)
{
float x= v.x, y= v.y, z= v.z, sn= s*q.s - v*q.v;
v.x= y*q.v.z - z*q.v.y + s*q.v.x + x*q.s;
v.y= z*q.v.x - x*q.v.z + s*q.v.y + y*q.s;
v.z= x*q.v.y - y*q.v.x + s*q.v.z + z*q.s;
s= sn;
return *this;
}
source
But, I don't understand how sn= s*q.s - v*q.v is supposed to work. s is a float, v is vector. Multiply vectors and add to float?
I'm not even sure which terms of direction/rotation/orientation these values represent, but if the function satisfies the quat values above, it'll work.
Found it. Turns out to be known as multiplication. Addition is multiplication. Up is sideways. Not confusing at all :/
fn qAdd q1 q2 = (
x1=q1.x
y1=q1.y
z1=q1.z
w1=q1.w
x2=q2.x
y2=q2.y
z2=q2.z
w2=q2.w
W = (W1 * W2) - (X1 * X2) - (Y1 * Y2) - (Z1 * Z2)
X = (W1 * X2) + (X1 * W2) + (Y1 * Z2) - (Z1 * Y2)
Y = (W1 * Y2) + (Y1 * W2) + (Z1 * X2) - (X1 * Z2)
Z = (W1 * Z2) + (Z1 * W2) + (X1 * Y2) - (Y1 * X2)
return (quat x y z w)
)
Swapping q1 & q2 yields different results, quite neither like addition nor multiplication.
source

Generating a random Gaussian double in Objective-C/C

I'm trying to generate a random Gaussian double in Objective-C (the same as random.nextGaussian in Java). However rand_gauss() doesn't seem to work. Anyone know a way of achieving this?
This link shows how to calculate it using the standard random() function.
I should note that you'll likely have to make the ranf() routine that converts the output of random() from [0,MAX_INT] to be from [0,1], but that shouldn't be too difficult.
From the linked article:
The polar form of the Box-Muller transformation is both faster and more robust numerically. The algorithmic description of it is:
float x1, x2, w, y1, y2;
do {
x1 = 2.0 * ranf() - 1.0;
x2 = 2.0 * ranf() - 1.0;
w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 );
w = sqrt( (-2.0 * ln( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;