how do i typeshift <class 'sympy.sets.sets.FiniteSet'> to int - sympy

I want to typeshift a value from a sympy solveset to be an int.
I try this:
from sympy import *
x = symbols('x')
a = solveset(Eq(x+radii[0],2*(-x+radii[-1])),x)
a = int(a)
I get this error:
int(gearAdjust(radii))
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'FiniteSet'
my desired result is for this code to just make a an int.

a is an object of type FiniteSet. You can convert it to a list and extract its element:
int(list(a)[0])

Related

Applying operations after type conversion error

I have converted using a string into the int. But it has two classes That doesn't have any arithematic or logical operation associated.
string x = "42";
int|error y = int:fromString(x);
y=y+1;
This is by design. When you perform string to int conversion, there can be errors. There is no way the compiler can know beforehand the resulting type. Therefore, the int:fromString() function returns a union type consisting int and the error type.
In Ballerina, there are a couple of ways to handle this. The easiest way is to use the check expression, which is used to early return the error value if an error occurred, and if there's not an error, assign the value to the intended type. Look at the following code:
public function main() returns error? {
string x = "42";
int y = check int:fromString(x);
y += 1; // This is as same as the `y = y + 1;`
}
In the above function, if there's an error in converting the string value to an int value, the error is returned. Otherwise the value is assigned to the variable y. Note that the function has the return type error?, which is a must to use the check expression here.
You can have an error check and do the necessary arithmetic operations in the else block as follows,
string x = "42";
int|error y = int:fromString(x);
if(y is error) {
io:println("Error occurred in conversion");
} else {
y = y + 1;
io:println(y);
}

Getting Integer value from Instance in Z3

My code is
def test():
s = Solver()
a = Int('x')
b = Int('y')
s.add(a*b==22)
print s.check()
return s.model()[a], s.model()[b]
This will print numbers, but when you see type(s.model()[a]) or type(s.model()[b]) it will give <type 'instance'> . How to cast it in a way it will return <type 'int'> ? I cannot use the return type into my code any further as it returned instance even though if you print s.model()[a] it will look like a integer but it isn't.
Simply use the as_long method. That is, replace the last line in your function with:
return s.model()[a].as_long(), s.model()[b].as_long()

ADTs and values

In Rascal, say I have the code:
value x = 2;
data Exp = con(int n);
Is there a way to call con(x), while x is a value (but actually an integer), without knowing on beforehand what the type of con's first argument is supposed to be (thus without explicitly casting it to an int)?
Why is it possible to call a function, say int something(int n) = n, with an integer defined as a value (e.g. value y = 2) passed into its first argument, while it gives me an error when I try to do the same with user-defined ADTs?
When you call a function in Rascal it actually is doing a pattern match on the arguments. So, if you define int something(int n) = n;, and then call something(x), it matches x with int n, sees that x is actually an int (so it can bind the value to n), and calls the function.
If you were to define value x = 2.5 instead and then call something(x) you would get an error since it cannot bind the value 2.5 to int n. You could overload something with a second definition that takes a real instead, like int something(real r) = toInt(r);, and it would then work. Two items to note here, though: something needs to return the same type in both cases, and you need to import util::Math to get access to toInt.
When you are using a constructor, like con(x), it doesn't do a pattern match for you automatically. The type that you give it has to match the type it expects. If you know that x will always be an int, it would be best to just declare it as such. Another option would be to create a function like Exp makeCon(int n) = con(n); which you could then use as you would like, i.e., Exp myExp = makeCon(x);. It would be best in this case to include a default version of the function, just in case you give it something unexpected, like default Exp makeCon(value x) { throw "Unexpected value <x>"; }, this way if you ever try to create a con with something that isn't an int you will get an error that you can handle, with the ability to create your own error message, add additional error handling versus just showing a message, see the value causing the problem, etc, versus just having the interpreter give an error (which may not give you all the info you want).

Typecasting DXVA2_Fixed32 to Float in C++

DXVA2_Fixed32 a = DXVA2_Fixed32OpaqueAlpha();
float f = (float)a;
This is throwing a compilation error
"error C2440: Cannot convert from "DXVA2_Fixed32 " to Float.
My purpose is to assign "a"'s value to "f".
Can anyone kindly let me know How to assign DXVA2_Fixed32 type variable "a" to "a float variable "f".
Thanks in advance.
You can't do it with a typecast like that. The DXVA2_Fixed32 type is a struct containing two fields with fractional and integer parts of the number.
You need to call DXVA2FixedToFloat to perform the conversion.
float f = DXVA2FixedToFloat(a);
If ever you need to go in the opposite direction you can use the predictably named DXVA2FloatToFixed.

C++ help with error : cannot convert parameter 1 from 'float' to 'float [][2]

and I was doing a program, that isn't so important. Apparently, I cant send parameters to a float function.
the code look something like this
float myfunction(float array[1][2])
{
// ...
return 0;
}
int main()
{
float array[1][2];
int foo = 0;
// assigning values to the array
foo = myfunction(array[1][2]);
return 0;
}
when I try to compile, I get the error "cannot convert parameter 1 from 'float' to 'float [][2]"
What is wrong? And how can I solve it?
Just pass array, without the indexes:
foo = myfunction(array);
A couple of things.
First, a function prototyped float myfunction(float array[1][2]) is confusing (you), since what it actually mean is: float myfunction(float array[][2]) or float myfunction(float (*array)[2]). The function accepts a pointer to (one or more) array(s) of two floats.
Second, the error you get is because the function accepts a pointer to an array, while you're tryinmg to pass it single float - element [1][2] of the two-dimensional array float array[1][2]. Perhaps you meant to pass the entire array to the function?
You've defined the variable: float array[1][2];
Then, you call the function in this way: foo = myfunction(array);
As a parameter, only you have to set the variable name. You shouldn't do this: foo = myfunction(array[1][2]);
When you make a function, do it in this way: type myfunction(float Array[][w]) , being type the type of function (void, float...) and "w" a constant integer.
You are passing a certain cell and not the array