It is currently Fri Nov 15, 2019 1:40 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Bug or not bug
PostPosted: Fri May 03, 2013 9:35 am 
Offline
User
User

Joined: Mon Aug 17, 2009 10:48 pm
Posts: 49
Location: France
Bug ou non

Voici 3 procédures de calcul d’une factorielle.
Deux sont des procédures récursives.
La troisième utilise une procédure itérative pour contrôle
Parmi les deux procédures récursives l’une (fact_m) donne à partir de n=> 9 des résultats erronés comme si le résultat provenait d’un type .long et non d’un type flottant double précision
Par contre la procédure fact donne des résultats OK
La seule différence entre les deux procédures est l’ordre du produit
n*fact(n-1) et fact(n-1)*n alors que fact_m() et fact() sont définies en flottant double précision et n dans la procédure fact_m est aussi défini en type flottant double précision

A+
Bug or not

Here are three procedures for calculating a factor.
Both are recursive procedures.
The third uses an iterative procedure to control
Of the two recursive procedures one (fact_m) gives from n => 9 erroneous if the result came as a type results. long and not a double-precision floating type
In fact against the procedure is working OK
The only difference between the two procedures is the order of the product
n * fact (n-1) and fact (n-1) * n while fact_m () and fact () are defined in double precision floating and n in fact_m procedure is defined as double precision floating-point

A +
Code:
Procedure.d Fact(n.l)
  If n<2
    ProcedureReturn 1.0
  Else
    ProcedureReturn Fact(n-1)*n
  EndIf
EndProcedure

Debug Fact(10)

Procedure.d Fact_M(n.d)
  If n<2
    ProcedureReturn 1.0
  Else
    ProcedureReturn n*Fact_m(n-1)
  EndIf
EndProcedure

Debug Fact_m(10)

Procedure.d Factoriel(valeur.l)
  Resultat.d=1
  For n=2 To valeur
    Resultat*n
  Next
  ProcedureReturn Resultat
EndProcedure

For n=1 To 12
  Debug Str(n)+"! = "+StrD(Factoriel(n))
  Debug Str(n)+"! = "+StrD(Fact(n))
  Debug Str(n)+"! = "+StrD(Fact_m(n))
  nd.d=n
  Debug Str(n)+"! = "+StrD(Fact_m(nd))
 
Next



A+


Top
 Profile  
Reply with quote  
 Post subject: Re: Bug or not bug
PostPosted: Fri May 03, 2013 1:10 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3693
Location: Italy
Code:
Procedure.d Fact_M1(n.d)
  If n<2
    ProcedureReturn 1.0
  Else   
    ProcedureReturn n * Fact_M1(n-1)
  EndIf
EndProcedure

Procedure.d Fact_M2(n.d)
  If n<2
    ProcedureReturn 1.0
  Else
    ProcedureReturn Fact_M2(n-1) * n
  EndIf
EndProcedure

Debug Fact_M1(10)
Debug Fact_M2(10)


Looks like a bug to me

_________________
[ My little PureBasic review ]


Top
 Profile  
Reply with quote  
 Post subject: Re: Bug or not bug
PostPosted: Fri May 03, 2013 1:36 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 13627
Location: France
It's a bug.


Top
 Profile  
Reply with quote  
 Post subject: Re: Bug or not bug
PostPosted: Fri May 03, 2013 1:36 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat Sep 20, 2003 3:57 pm
Posts: 216
Location: Germany
The problem here is a fpu stack overflow.
"ProcedureReturn n * Fact_M1(n-1)" leaves "n" on the stack and then calls "Fact_M1(n-1)". So within Fact_M1(n-1) you got 1 less space of the fpu stack. Since this stack has a size of 8 -> n=9 fails.

"ProcedureReturn Fact_M2(n-1) * n" calls Fact_M2(n-1) with an empty stack so everything is fine and then loads n onto the stack to perform the multiplication.

For reference:
n = qword [esp+PS0+0]
-1 = qword [D3]

Code:
; ProcedureReturn n * Fact_M1(n-1)
  FLD    qword [esp+PS0+0]
  FLD    qword [esp+PS0+0]
  FADD   qword [D3]
  SUB    esp,8
  FSTP   qword [esp]
  CALL  _Procedure0
  FMULP  st1,st0
  JMP   _EndProcedure1
[...]
; ProcedureReturn Fact_M2(n-1) * n
  FLD    qword [esp+PS2+0]
  FADD   qword [D3]
  SUB    esp,8
  FSTP   qword [esp]
  CALL  _Procedure2
  FMUL   qword [esp+PS2+0]
  JMP   _EndProcedure3

_________________
irc://irc.freenode.org/#purebasic


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye