#1494: factorial example in languages/squaak gives incorrect result
--------------------+-------------------------------------------------------
Reporter: parthm | Type: bug
Status: new | Priority: normal
Milestone: | Component: none
Version: 2.1.0 | Severity: medium
Keywords: | Lang:
Patch: | Platform:
--------------------+-------------------------------------------------------
[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]%
--
Ticket URL: <https://trac.parrot.org/parrot/ticket/1494>
Parrot <https://trac.parrot.org/parrot/>
Parrot Development
_______________________________________________
parrot-tickets mailing list
[email protected]
http://lists.parrot.org/mailman/listinfo/parrot-tickets