Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
20707 Discussions

calculate pi in verilog

Altera_Forum
Honored Contributor II
3,364 Views

Im trying to develop a system which will calculate pi using verilog and using the Gregory Leibniz series. this is the equation  

 

pi = 4 x ( (-1)^n / 2n+1 ); where n = 0 to infinity  

 

ive tried my best but cant seem to go anywhere. i couldnt find any verilog operator that calculates the power of a number.  

another question is, can verilog execute any calculations except binary? for example fractional numbers. if no then how will i implement the above equation? im suposed to implement this into quartus for hardware simulation. in simulation i only saw an output if my calculation was in binary.i tried to multiply 2 decimal numbers (10 & 12) but i got 0 in output. for binary 10&10 i got 100.  

 

please help me. if any one can provide me with the coding, that will be great or at least the coding of the equation. 

 

thanks
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
2,383 Views

Hi, 

 

The formula you are after is actually a summation of the term you mentioned for n = O to infinity.  

 

The (-1)^n is not difficult since you can use -1 if n is odd ot +1 if n is even. Or since you need to increment n every time, you can toggle the sign for every n. 

 

The biggest issue you will have is the division.  

 

If you need a bright new idea to calculate pi, don't start with Liebniz, since 

 

 

--- Quote Start ---  

Calculating π to 10 correct decimal places using Leibniz' formula requires over 10,000,000,000 mathematical operations, and will take longer for most computers to calculate than calculating π to millions of digits using more efficient formulas. 

 

--- Quote End ---  

 

 

 

Stefaan
0 Kudos
Altera_Forum
Honored Contributor II
2,382 Views

svhb, 

thanks for the reply. i am aware of the facts you pointed out however my assignment is to use the Liebniz series so cant help it. 

 

any idea on the division? what about calculations of fractional numbers in verilog?can it be done? what i mean is that isn't there any syntax or operator in verilog which i can use to directly implement the equation like i do in matlab?  

 

thanks
0 Kudos
Altera_Forum
Honored Contributor II
2,383 Views

naamhin, 

 

You can try to get a look at the LPM_DIVIDE megafunction for the division. 

 

AFAIK, it is not possible to use the divide '/' operator in verilog directly for creating logic. In a simulator I think it should work.  

 

Be careful with the number of bits you use for the calculations. You should investigate how much bits you need for a particular precision of your resulting PI value. 

 

Stefaan
0 Kudos
Altera_Forum
Honored Contributor II
2,383 Views

Stefaan, 

 

Thanks a lot. Ive tried the altfp_div megafunction for the floating point division part and it works great. I now have to implement the rest of the Gregory series. 

 

Thanks for all the help. 

 

Naamhin
0 Kudos
Reply