在SQL Monte Carlo方法中估计Pi

时间:2018-09-18 01:10:05

标签: mysql sql pi

我正在尝试显示SQL中PI的估计值。我希望我的估算值是数字(12,11),但它总共只能显示4位数字,精度为3。如何格式化此代码?


declare @fNum as float
declare @yNum as float
declare @pi as float

declare @loopNum as int
declare @inCount as int = 0
declare @totalCount as int = 0
set @loopNum = 1000
declare @oDist as numeric

while @loopNum > 0

begin
set @totalCount +=1 
 set @fNum = rand () * 100 + 1
 set @yNum =  rand() * 100 +1 


    set @oDist = POWER(@fNum, 2.0) + Power(@yNum, 2.0) 

   if sqrt(@oDist)  <= 100.00
        set @inCount +=1

set @loopNum -= 1
set @pi = (4.0 *@inCount / @totalCount)

 end

select @inCount AS 'IN', @totalCount as 'ALL',/* @fNum as 'f', @yNum as     'y', sqrt(@oDist) as 'Origin'*/ cast(@pi as numeric(7,6)) as 'Pi'
 go

0 个答案:

没有答案