PI : many digits try

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

PI : many digits try

Post by bluatigro »

what is the good way for this ?

[ translated from vb2005 code ]

Code: Select all

const as integer n = 100
sub 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 p
end sub
sub 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 p
end sub
sub 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 p
end 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 p
end sub
function 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 1
end function
sub 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 sub
function 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 uit
end function

print find_pi()
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: PI : many digits try

Post by fxm »

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

Re: PI : many digits try

Post by srvaldez »

@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: 3379
Joined: Sep 25, 2005 21:54

Re: PI : many digits try

Post by srvaldez »

after fixing numerous mistakes here's your working code

Code: Select all

const as integer n = 100
sub 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 p
end sub

sub 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 p
end sub

sub 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 p
end 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 p
end sub

function 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 1
end function

sub 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 sub
function 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 uit
end function

print find_pi()
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: PI : many digits try

Post by srvaldez »

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.1415926535897932385
pi in base 16 = 3.243F6A8885A308D3132
pi in base 2 = 11.0010010000111111100

Code: Select all

Declare Function find_pi(Byval n As Integer, Byval nbase As Integer) As String

Print 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 sn
End Function

Sub 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 p
End Sub

Sub 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 p
End Sub

Sub 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 p
End 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 p
End Sub

Function 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 1
End Function

Sub 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 Sub

Function 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 uit
End Function
Post Reply