## PI : many digits try

General FreeBASIC programming questions.
bluatigro
Posts: 598
Joined: Apr 25, 2012 10:35
Location: netherlands

### PI : many digits try

what is the good way for this ?

[ translated from vb2005 code ]

Code: Select all

`const as integer n = 100sub array_mul( byref a() as integer , f as integer )  dim as integer c = 0 , p , h  for p = n to 0 step -1    h = a( p ) * f + c    c = int( h / 10 )    a( p ) = h mod 10  next pend subsub array_div( byref a() as integer , f as integer )  dim as integer b = 0 , p , h  for p = n to 0     h = a( p ) + b * 10    a( p ) = int( h / f )    b = h mod f  next pend subsub array_add( byref a() as integer , byref b() as integer )  dim as integer c = 0 , p , h  for p = n to 0 step -1    h = a( p ) + b( p ) + c    c = int( h / 10 )    a( p ) = h mod 10  next pend sub sub array_sub( byref a() as integer , byref b() as integer )  dim as integer b = 0 , p , h  for p = n to 0 step -1    h = a( p ) - b( p ) + 10    b = int( h / 10 )    a( p ) = h mod 10  next pend subfunction is_zero( a() as integer ) as integer  dim as integer p  for p = 0 to n    if a( p ) then return 0  next p  return 1end functionsub arctan( byref t() as integer , byref s() as integer , div as integer )  dim as integer w , i  s( 0 ) = 1  i = 1  w = div  array_div s() , w  array_add t() , s()  do    array_mul s() , i    w = div * div    array_div s() , w    i += 2    w = i    array_div s() , w    array_sub t() , s()    array_mul s() , i    w = div * div    array_div s() , w    i += 2    w = i    array_div s() , w    array_t() , s()  loop until is_zero( s() )end subfunction find_pi() as string  dim as string uit = "pi = 3."  dim as integer i , f , s( n + 2 ) , t( n + 2 )  f = 2  acrtan t() , s() , f  f = 3   arctan t() , s() , f  array_mul t() , 4  for i = 1 to n - 1    uit += str( t( i ) )  next i  return uitend functionprint find_pi()`
fxm
Posts: 9307
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: PI : many digits try

It would be better to provide the original vb2005 code.
srvaldez
Posts: 2152
Joined: Sep 25, 2005 21:54

### Re: PI : many digits try

@bluatigro
remove all byref from array arguments, also you can't have scalar and array variable of the same name, there are too many mistakes in your code to make it work
Last edited by srvaldez on Nov 06, 2019 14:42, edited 1 time in total.
srvaldez
Posts: 2152
Joined: Sep 25, 2005 21:54

### Re: PI : many digits try

after fixing numerous mistakes here's your working code

Code: Select all

`const as integer n = 100sub array_mul( a() as integer , f as integer )  dim as integer c = 0 , p , h  for p = n to 0 step -1    h = a( p ) * f + c    c = h \ 10    a( p ) = h mod 10  next pend subsub array_div( a() as integer , f as integer )  dim as integer b = 0 , p , h  for p=0 to n '<-- you had it wrong    h = a( p ) + b * 10    a( p ) = h \ f    b = h mod f  next pend subsub array_add( a() as integer , b() as integer )  dim as integer c = 0 , p , h  for p = n to 0 step -1    h = a( p ) + b( p ) + c    c = h \ 10    a( p ) = h mod 10  next pend sub sub array_sub( a() as integer , b() as integer )  dim as integer b1 = 0 , p , h  for p = n to 0 step -1    h = a( p ) - b( p ) + 10    b1 = h \ 10    a( p ) = h mod 10    If (b1= 0) Then a(p - 1) -= 1 '<-- you forgot this line  next pend subfunction is_zero( a() as integer ) as integer  dim as integer p  for p = 0 to n    if a( p ) then return 0  next p  return 1end functionsub arctan( t() as integer , s() as integer , div as integer )  dim as integer w , i  s( 0 ) = 1  i = 1  w = div  array_div s() , w  array_add t() , s()  do    array_mul s() , i    w = div * div    array_div s() , w    i += 2    w = i    array_div s() , w    array_sub t() , s()    array_mul s() , i    w = div * div    array_div s() , w    i += 2    w = i    array_div s() , w    array_add t() , s() '<-- you forgot the add  loop until is_zero( s() )end subfunction find_pi() as string  dim as string uit = "pi = 3."  dim as integer i , f , s( n + 2 ) , t( n + 2 )  f = 2  arctan t() , s() , f  f = 3   arctan t() , s() , f  array_mul t() , 4  for i = 1 to n - 1    uit += str( t( i ) )  next i  return uitend functionprint find_pi()`
srvaldez
Posts: 2152
Joined: Sep 25, 2005 21:54

### Re: PI : many digits try

hello bluatigro
just for fun, I modified the code to calculate pi in base 2 to 16

Code: Select all

`pi in base 10 = 3.1415926535897932385pi in base 16 = 3.243F6A8885A308D3132pi in base 2 = 11.0010010000111111100`

Code: Select all

`Declare Function find_pi(Byval n As Integer, Byval nbase As Integer) As StringPrint find_pi(20, 10)Print find_pi(20, 16)Print find_pi(20, 2)Function convert(Byval n As Ulongint, Byval nbase As Integer) As String   If nbase<2 Or nbase>16 Then Return "base bust be between 2 and 16"   Dim As String sn   Dim As Ulongint r   Static As String*2 h(15)={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}   If n=0 Then      Return "0"   Elseif n<0 Then      Return "positive number only"   End If   sn=""   While n>=nbase      r=n Mod nbase      n\=nbase      sn=h(r)+sn   Wend   sn=h(n)+sn   Return snEnd FunctionSub array_mul( a() As Integer , f As Integer, Byval n As Integer, Byval nbase As Integer )   Dim As Integer c = 0 , p , h   For p = n To 0 Step -1      h = a( p ) * f + c      c = h \ nbase      a( p ) = h Mod nbase   Next pEnd SubSub array_div( a() As Integer , f As Integer, Byval n As Integer, Byval nbase As Integer )   Dim As Integer b = 0 , p , h   For p=0 To n      h = a( p ) + b * nbase      a( p ) = h \ f      b = h Mod f   Next pEnd SubSub array_add( a() As Integer , b() As Integer, Byval n As Integer, Byval nbase As Integer )   Dim As Integer c = 0 , p , h   For p = n To 0 Step -1      h = a( p ) + b( p ) + c      c = h \ nbase      a( p ) = h Mod nbase   Next pEnd Sub Sub array_sub( a() As Integer , b() As Integer, Byval n As Integer, Byval nbase As Integer )   Dim As Integer b1 = 0 , p , h   For p = n To 0 Step -1      h = a( p ) - b( p ) + nbase      b1 = h \ nbase      a( p ) = h Mod nbase      If (b1= 0) Then a(p - 1) -= 1   Next pEnd SubFunction is_zero( a() As Integer, Byval n As Integer ) As Integer   Dim As Integer p   For p = 0 To n      If a( p ) Then Return 0   Next p   Return 1End FunctionSub arctan( t() As Integer , s() As Integer , div As Integer, Byval n As Integer, Byval nbase As Integer )   Dim As Integer w , i   s( 0 ) = 1   i = 1   w = div   array_div s() , w , n , nbase   array_add t() , s() , n  , nbase   Do      array_mul s() , i , n  , nbase      w = div * div      array_div s() , w , n  , nbase      i += 2      w = i      array_div s() , w , n  , nbase      array_sub t() , s() , n  , nbase      array_mul s() , i , n  , nbase      w = div * div      array_div s() , w , n  , nbase      i += 2      w = i      array_div s() , w , n  , nbase      array_add t() , s() , n  , nbase   Loop Until is_zero( s() , n  )End SubFunction find_pi(Byval n As Integer, Byval nbase As Integer) As String   If nbase<2 Or nbase>16 Then Return "base bust be between 2 and 16"   If n<2 Then n=2   'Dim As Double tm = Timer   Dim As String digit, uit = "pi in base "+Str(nbase)+" = "+convert(3, nbase)+"."   Dim As Integer i , f , s( n + 2 ) , t( n + 2 )   f = 2   arctan t() , s() , f , n  , nbase   f = 3    arctan t() , s() , f , n  , nbase   array_mul t() , 4 , n  , nbase   'tm=timer-tm   For i = 1 To n - 1      digit=convert( t( i ), nbase )      uit += digit   Next i   'Print "elapsed time = ";tm;" seconds"   Return uitEnd Function`