Ticket #1494 (new bug)

Opened 4 years ago

Last modified 4 years ago

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

Change History

Changed 4 years ago by parthm

Just to add. This was with parrot-2.1.1 tarball on ubuntu 9.10

Changed 4 years ago by coke

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

Changed 4 years ago by coke

  • version changed from 2.1.0 to trunk
Note: See TracTickets for help on using tickets.