factorial example in languages/squaak gives incorrect result #636

Open
parrot opened this Issue Mar 4, 2010 · 2 comments

Comments

Projects
None yet
2 participants
@ghost

ghost commented Mar 4, 2010

[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]%

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

@ghost

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment