Ticket #1494 (new bug)
factorial example in languages/squaak gives incorrect result
Reported by: | parthm | Owned by: | |
---|---|---|---|
Priority: | normal | Milestone: | |
Component: | none | Version: | trunk |
Severity: | medium | Keywords: | |
Cc: | Language: | ||
Patch status: | Platform: |
Description
[squaak]% cat examples/factorial.sq # Copyright (C) 2008, Parrot Foundation. # $Id: factorial.sq 36833 2009-02-17 20:09:26Z allison $
print("Please enter number: ") x = read()
sub factorial(n)
## test for < 2 because all numbers are represented by ## floating-point numbers, and equality checks on those ## do not work (yet?) if n < 2 then
return 1
else
return n * factorial(n - 1)
end
end
print("factorial of ", x, " is: ", factorial(x))
[squaak]% ./installable_squaak examples/factorial.sq Please enter number: 10 factorial of 10
is: 1
[squaak]% ./installable_squaak examples/factorial.sq Please enter number: 10 factorial of 10
is: 1
[squaak]% pwd /home/parthm/src/parrot-2.1.1/examples/languages/squaak [squaak]% which parrot /home/parthm/src/parrot-2.1.1/parrot [squaak]%
It seems (n < 2) is always returning true. Below are the results with prints added.
[squaak]% cat fac2.sq # Copyright (C) 2008, Parrot Foundation. # $Id: factorial.sq 36833 2009-02-17 20:09:26Z allison $
print("Please enter number: ") x = read()
print("you entered:", x)
sub factorial(n)
## test for < 2 because all numbers are represented by ## floating-point numbers, and equality checks on those ## do not work (yet?)
print("got: ", n) print("is n < 2:", n < 2)
if n < 2 then
print("return 1") return 1
else
print("return n * fac(n-1)") return n * factorial(n - 1)
end
end
print("factorial of ", x, " is: ", factorial(x))
[squaak]% ./installable_squaak fac2.sq Please enter number: 10 you entered:10
got: 10
is n < 2:1 return 1 factorial of 10
is: 1
[squaak]%