# Cube root

**URL:** <https://verificationacademy.com/forums/t/cube-root/40300>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, uvm-sv-constraints, logic, systemverilog-Arrays-logics\
**Created:** [April 21, 2022, 6:25am UTC](https://verificationacademy.com/forums/t/cube-root/40300 "2022-04-21T06:25:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShashankGurijala](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@ShashankGurijala](https://verificationacademy.com/forums/u/ShashankGurijala)\
**Post date:** [April 21, 2022, 6:25am UTC](https://verificationacademy.com/forums/t/cube-root/40300/1 "2022-04-21T06:25:42Z")

</div>

if (j_j_j == 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?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [April 21, 2022, 7:46am UTC](https://verificationacademy.com/forums/t/cube-root/40300/2 "2022-04-21T07:46:14Z")

</div>

_In reply to [Shashank Gurijala](https://verificationacademy.com/forums/t/cube-root/40300):_

In integral arithmetic, 1/3 is 0. You cannot use a fractional exponent in a constraint.

---

<div class="post-metadata">

**Author:** ![ShashankGurijala](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@ShashankGurijala](https://verificationacademy.com/forums/u/ShashankGurijala)\
**Post date:** [April 21, 2022, 8:12am UTC](https://verificationacademy.com/forums/t/cube-root/40300/3 "2022-04-21T08:12:52Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/cube-root/40300/2):_

Here is the code:

```verilog
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

```

---

<div class="post-metadata">

**Author:** ![KillSteal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@KillSteal](https://verificationacademy.com/forums/u/KillSteal)\
**Post date:** [April 21, 2022, 2:46pm UTC](https://verificationacademy.com/forums/t/cube-root/40300/4 "2022-04-21T14:46:57Z")

</div>

_In reply to [Shashank Gurijala](https://verificationacademy.com/forums/t/cube-root/40300/3):_

I am just curious, why do you want to use “any other expression that uses cube root instead of cube” ? What are you after ?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [April 21, 2022, 8:02pm UTC](https://verificationacademy.com/forums/t/cube-root/40300/5 "2022-04-21T20:02:00Z")

</div>

_In reply to [Shashank Gurijala](https://verificationacademy.com/forums/t/cube-root/40300/4):_

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.

---

<div class="post-metadata">

**Author:** ![Rahulkumar2](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/rahulkumar2/32/3675_2.png) [@Rahulkumar2](https://verificationacademy.com/forums/u/Rahulkumar2)\
**Post date:** [May 28, 2022, 2:59pm UTC](https://verificationacademy.com/forums/t/cube-root/40300/6 "2022-05-28T14:59:15Z")

</div>

_In reply to [Shashank Gurijala](https://verificationacademy.com/forums/t/cube-root/40300/5):_

Use post\_randomize.

```verilog

    class Simple;
        real j;
        rand byte unsigned i; 

        function void post_randomize();
                j = i**(1.0/3);
        endfunction 

   endclass

```

* * *

[https://www.linkedin.com/in/patel-rahulkumar/](https://www.linkedin.com/in/patel-rahulkumar/)
