
Hello! I am reading Discrete Mathematics Using a computer by Hall and O'Donnell and in the introduction to hugs section, they give this example: [[x,y] | x<-[1..50],y<-[1..50],z<-[1..50],x**2+y**2==z**2] now I type it in and expect it to return a number of results, but it doesn't, it shows: [[8.0,15.0,17.0],[14.0,48.0,50.0],[15.0,8.0,17.0],[15.0,20.0,25.0],[20.0,15.0,25.0],[21.0,28.0,35.0],[27.0,36.0,45.0],[28.0,21.0,35.0],[30.0,40.0,50.0],[36.0,27.0,45.0],[40.0,30.0,50.0],[48.0,14.0,50.0]] My question is where is [3.0,4.0,5.0],[5.0,12.0,13.0]? I have looked everywhere I could think of to see what I am doing wrong, but I have no idea. Maybe a rounding error somewhere? Thank you, Scott Anderson

Scott
[[x,y] | x<-[1..50],y<-[1..50],z<-[1..50],x**2+y**2==z**2] expect it to return a number of results, but it shows: [[8.0,15.0,17.0],[14.0,48.0,50.0],[15.0,8.0,17.0],[15.0,20.0,25.0],[20.0,15 .0,25.0],[21.0,28.0,35.0],[27.0,36.0,45.0],[28.0,21.0,35.0],[30.0,40.0,50.0] ,[36.0,27.0,45.0],[40.0,30.0,50.0],[48.0,14.0,50.0]] My question is where is [3.0,4.0,5.0],[5.0,12.0,13.0]? Maybe a rounding error somewhere?
That's right. Haskell has 3 exponentiation operators. If you replace ** with ^ or ^^ then you'll get the results you expect. The ** operator is the one which is defined for floating point numbers. The easy implementation of a**b begins by finding the log of a, so it's going to give approximate results unless you're in some very sophisticated and rigorous numeric environment. In my Hugs on a PC, I get Prelude> 5.0 ** 2.0 == 25.0 False -- Scott Turner p.turner@computer.org http://www.ma.ultranet.com/~pkturner

Thu, 28 Dec 2000 18:04:38 -0500, Scott
Maybe a rounding error somewhere?
Indeed. (**) is floating-point power. Use (^) instead. (^) requires an integer nonnegative exponent and does _not_ force a floating point base like (**) does - the base can be any numeric type. It will default to Integer in your case. -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
participants (3)
-
qrczak@knm.org.pl
-
Scott
-
Scott Turner