if (jjj == i) ------> when I use this, it’s working fine.
But, if ( j == i**(1/3) ------> is giving me the wrong outputs while trying to print only perfect cubes out of randmoized i with constraint i=>1 and i=<1000.
So how do I declare cube roots or square roots or exp of 2/3?
In reply to Shashank Gurijala:
In integral arithmetic, 1/3 is 0. You cannot use a fractional exponent in a constraint.
In reply to dave_59:
Here is the code:
class packet;
randc int i;
constraint i_range {i >= 1; i <=1000;}
endclass:packet
module cubes;
int j;
initial
begin
packet p = new;
repeat(1000)
begin
p.randomize();
for(j=1;j<=10;j++)
begin
if (j*j*j == p.i) //I want to use if( j == (p.i)^(1/3)) or anyother expression that uses cube root of p.i instead of cube of j
$display ("cube is %d and its root is %d",p.i,j);
end
end
end
endmodule: cubes
In reply to Shashank Gurijala:
I am just curious, why do you want to use “any other expression that uses cube root instead of cube” ? What are you after ?
In reply to Shashank Gurijala:
Your intended approach does not work. 1/3 as a fractional real number cannot be represented accurately in binary. There are algorithms to search for cube roots, but not in an expressions.
In reply to Shashank Gurijala:
Use post_randomize.
class Simple;
real j;
rand byte unsigned i;
function void post_randomize();
j = i**(1.0/3);
endfunction
endclass