# factorial example in languages/squaak gives incorrect result #636

Open
opened this Issue Mar 4, 2010 · 2 comments

Projects
None yet
2 participants

### ghost commented Mar 4, 2010

[squaak]% cat examples/factorial.sq

# \$Id: factorial.sq 36833 2009-02-17 20:09:26Z allison \$

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
10
factorial of 10
is: 1
[squaak]% ./installable_squaak examples/factorial.sq
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

# \$Id: factorial.sq 36833 2009-02-17 20:09:26Z allison \$

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
10
you entered:10

got: 10

is n < 2:1
return 1
factorial of 10
is: 1
[squaak]%

Originally http://trac.parrot.org/parrot/ticket/1494 by parthm

### ghost commented Mar 4, 2010

 Trac commenter: parthm Just to add. This was with parrot-2.1.1 tarball on ubuntu 9.10
Contributor

### coke commented Aug 18, 2010

 This fails differently in trunk today as read() no longer seems to be a valid function for this language.