How to generate two variables at the same time? - specman

I have a struct which has two fields X and Y, I have several keeps on them, and i would like to generate them several times at the same time inside the struct itself.
My need is more complicated but this is the main issue i'm facing.
How can we generate two fields at the same time?
Example:
struct example {
X : int;
Y : int;
keep X < Y;
keep Y < 10;
keep X+Y > 5;
do_something(cnt : int) is {
for i from 1 to cnt {
gen X and Y;
print X, Y;
};
};
};

Since gen me doesn't work, you can just declare a variable of type example in do_something(...) and generate that:
do_something(cnt : int) is {
for i from 1 to cnt {
var obj : example;
gen obj;
X = obj.X;
Y = obj.Y;
print X, Y;
};
};

Related

How to calculate SquareRoot of Integer without using inbuilt function in Swift Language? I tried below code & searched but didn't get better solution

Just an exercise:
func mySqrt(_ x: Int) -> Int {
if x<2 { return x }
var y = x
var z = (y + (x/y)) / 2
while Double(abs(y - z)) >= 0.00001 {
y = z
z = (y + (x/y)) / 2
}
return z
}
I went through many answers in StackOverflow but couldn't get a better solution, e.g.
Best way to calculate the square root of any number in ios , Objective C and Swift
Finding square root without using sqrt function?
Take input x = 4 or any perfect square, it works fine. Now, take something like x = 8, It Time out.
Please help me out, what I am doing wrong.
The problem is you are trying to use integers for all of the calculations.
The square root of 8 is not an integer. You need to use Double for all of your variables (except optionally leave parameter as an Int). The return value needs to be a Double if you want a meaningful answer.
Here's your code using Double where needed:
func mySqrt(_ x: Int) -> Double {
if x < 2 { return Double(x) }
var y = Double(x)
var z = (y + (Double(x)/y)) / 2
while (abs(y - z)) >= 0.00001 {
y = z
z = (y + (Double(x)/y)) / 2
}
return z
}
print(mySqrt(8))
This gives the correct result.

How to get n power ( square of a number or cube etc.) of a number in flutter?

I have two values one for base that is X and one for power N, how can a get X to the power of N ans.
any code will be appreciated.
You are looking for this:
https://api.dartlang.org/stable/2.5.0/dart-math/pow.html
so:
pow(X,N)
If you want to implement it, you can have a look at here:
https://coflutter.com/challenges/dart-how-to-implement-exponential-function-power/
This boils down to this loop:
int power(int x, int n) {
int retval = 1;
for (int i = 0; i < n; i++) {
retval *= x;
}
return retval;
}
This only works well for integer n-s.
For all of these examples with pow you need the following import:
import 'dart:math';
8²
final answer = pow(8, 2); // 64
Notes:
If you are only squaring, then it's probably easier to do this:
final answer = 8 * 8;
answer is inferred to be of type num, which could be an int or double at runtime. In this case the runtime type is int, but in the following two examples it is double.
Fourth root of 256
final answer = pow(256, 1/4); // 4.0
0.2^(-3)
final answer = pow(0.2, -3); // 124.99999999999999
That's basically the same as five cubed.
btw Its working fine for double also
double power() {
double x= double.parse(t1.text);
int y= int.parse(t2.text);
double power = 1;
for (double i = 0; i < y; i++) {
power *= x;
} ;
return power;
}

Octave - how to operate with big numbers

I work on RSA algorithm in octave, but it isn't working in proper way. Problem appears while i try to use "^" function. Check my example below:
>> mod((80^65), 133)
terminal gives me:
ans = 0
I cannot fix this stuff, it's funny becouse even my system calculator return correct number (54)
to calculate this in correct way you can use fast power-modulo algorithm.
In c++, check function below where ->
a^b mod m:
int power_modulo_fast(int a, int b, int m)
{
int i;
int result = 1;
int x = a % m;
for (i=1; i<=b; i<<=1)
{
x %= m;
if ((b&i) != 0)
{
result *= x;
result %= m;
}
x *= x;
}
return result;
}

pass by value and pass by reference for x and y

Given the pseudo code below, what will be the output of the program, when the two parameters x and y are passed by value, and when they are passed by reference?
int x = 1;
int y = 1;
int count = 0;
while count < 3{
addnumbers(x, y);
println (x);
count++;
}
addnumbers(int x, int y){
x = x + y;
println (x);
}
Pass by Value: In pass by value, when you pass a variable into a function, the function will use a copy of the variable, so the original is not affected.
2
1
2
1
2
1
Pass by Reference: In pass by reference, you pass the reference of the variable, so since both int x and int x inside the function reference the same thing, if you change one, the other one changes too.
2
2
3
3
4
4

How can I convert C# code to MATLAB?

I have this C# code and I am trying to convert it to MATLAB code.
float randomFloat()
{
return (float)rand() / (float)RAND_MAX;
}
int calculateOutput(float weights[], float x, float y)
{
float sum = x * weights[0] + y * weights[1] + weights[2];
return (sum >= 0) ? 1 : -1;
}
I don't think we can use float and int in MATLAB. How do I change the code?
the first one is simply: rand()
the second function can be written as:
if ( [x y 1]*w(:) >=0 )
result = 1;
else
result = -1;
end
The built-in function rand() already does what you're trying to do with randomFloat().
For calculateOutput you can use something fairly similar to what you've got, but as you say you don't need to declare types:
function result = calculateOutput (weights, x, y)
s = x * weights(1) + y * weights(2) + weights(3);
if s >= 0
result = 1;
else
result = -1;
end
end
Note that matlab vectors are one-based, so you need to adjust the indexing.
If you want to generalise this to arbitrary vectors it would make sense to "vectorize" it, but for this simple case a straight translation like this should be fine.