unexpected order of evaluation

General FreeBASIC programming questions.
dodicat
Posts: 8168
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: unexpected order of evaluation

Post by dodicat »

Could you check your pi, here is 100 places compared to the internet pi

Code: Select all

 
#cmdline "-gen gcc -Wc -O3"

Function pie(n As Long) As String
    Dim As String pi
    #macro AddDigit(d)
    pi+=Str(d)
    #endmacro
    Var  _len = 20*n\4
    Dim As long a(_len),j,i,k
    Var nines = 0
    Var Digit = 0
    For j  = 0 To _len-1:a(j) = 2:Next
        For j = 1 To n
            Var   q = 0
            For i = _len To 1 Step -1
                Var x   = 10*a(i-1) + q*i
                Var m=(i Shl 1 - 1)
                a(i-1) = x Mod m
                q= x\m
            Next i
            a(0) = q Mod 10
            q =q \ 10
            If q = 9 Then
                nines += 1
            Else
                If q = 10 Then
                    AddDigit((Digit+1))
                    If nines > 0 Then
                        For k = 1 To nines:AddDigit(0):Next
                        End If
                        Digit = 0
                        nines = 0
                    Else
                        AddDigit(Digit)
                        Digit = q
                        If nines <> 0 Then
                            For k = 1 To nines:AddDigit(9):Next
                                nines = 0
                            End If
                        End If
                    End If
                Next j
                Return "3."+Ltrim(pi,"03")
    End Function
            
            
            function getdecimals(s as string)as long
              return len(mid(s,Instr(s,".")+1))
             end function   
     
            var t=timer
            Print pie(102)
            print timer-t;" seconds"
            'internet pi below
     print "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"      
            Sleep
srvaldez
Posts: 3542
Joined: Sep 25, 2005 21:54

Re: unexpected order of evaluation

Post by srvaldez »

dodicat
your function agrees with that of mine, when set to 100 digits of precision it prints
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214807461e+00000
relative error -3.788932442673339572 E-108
dodicat
Posts: 8168
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: unexpected order of evaluation

Post by dodicat »

That's OK then, all digits are correct.
The problem with asm was expounded by deltarho recently with his random numbers.
I believe he now sticks to gcc optimizing standard basic code, which includes these builtin functions I suppose.
Anyway, thank you srvaldez.
srvaldez
Posts: 3542
Joined: Sep 25, 2005 21:54

Re: unexpected order of evaluation

Post by srvaldez »

dodicat
I was curious about the performance of your program vs mine, so I ran your program with Print pie(10002)
your time 4.68 seconds
mine was 4.53 seconds with -O2 but with -O3 the time was 2.73 seconds
however if I change the compile command in your program to #cmdline "-arch native -gen clang -Wc -O3" the time is 3.13 seconds
BigFloat-05 has some asm left in it so it won't compile with clang
srvaldez
Posts: 3542
Joined: Sep 25, 2005 21:54

Re: unexpected order of evaluation

Post by srvaldez »

@dodicat
what algorithm for Pi did you use ?
dodicat
Posts: 8168
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: unexpected order of evaluation

Post by dodicat »

Hi srvaldez, the algorithm I picked up years ago, It might have been in a pascal forum, I don't remember
I tried to speed it up and made a function out of it, so it is not original code from me, but it is a digit by digit pie.
I always thought it was a bit slow, but with #cmd optimisations it seems to have got a bit faster over the years.
srvaldez
Posts: 3542
Joined: Sep 25, 2005 21:54

Re: unexpected order of evaluation

Post by srvaldez »

dodicat, is it like the following ?

Code: Select all

Dim As Double eps, Pi, x, y
x = 3
y = 1
Pi = x
eps = 1e-15

While (x > eps)
	x = x * y / ((y + 1) * 4)
	y += 2
	Pi += x / y
Wend

Print Pi
Sleep
dodicat
Posts: 8168
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: unexpected order of evaluation

Post by dodicat »

I think that one uses a series type pi.
I have one also using Taylor series for atan.
Neither are digit by digit but an approximation converging within fb double.

Code: Select all


#cmdline "-gen gcc -Wc -O3"
function pi as double
Dim As Double eps, Pii, x, y
x = 3
y = 1
Pii = x
eps = 1e-15
While (x > eps)
	x = x * y / ((y + 1) * 4)
	y += 2
	Pii += x / y
Wend
return pii
end function

Function pie As Double
    const y_=1.198422490593031e-005,f=262144
    Dim As Double xx=y_*y_,term,p=y_,temp1=y_,temp2
    Dim As long c=1,counter,sign
    Do
        c+=2
        counter+=1
        p*=xx
        term=p/c
        If (counter And 1) Then sign=-1 Else sign=1
        temp2=temp1
        temp1+=sign*term
    Loop Until temp1=temp2
    Return f*temp1
End Function

dim as double p
dim as double t
for k as long=1 to 10
dim as long limit=1000000
t=timer
for n as long=1 to limit
p= Pi
next
print p,timer-t," srvaldez"
t=timer
for n as long=1 to limit
p= Pie
next
print p,timer-t," dodicat"
print
next k

Sleep
 
Post Reply