Computer Science Canada Conversion of integer to float ?? in Haskell |
Author: | neokewl [ Tue Jan 02, 2007 7:54 pm ] |
Post subject: | Conversion of integer to float ?? in Haskell |
Hey I was trying out a program to find the area of cirlce in Haskell. I have a problem in converting the data types from integer to float. The pgm is as follows: type Point = (Int,Int) data Fig = Pt Point | Circle Point Int area :: Fig -> Float area (Circle a b) = 3.141 * b*b the above definition of area of circle is giving me compile errors as 3.141 is a fractional value. How do i convert the integer value of b to floating point so tht the multiplication is compatible. thnx neokewl |
Author: | Clayton [ Tue Jan 02, 2007 7:56 pm ] |
Post subject: | |
Is it necessary that 'b' be the type of Integer? Also, next time please use [code][/code] tags |
Author: | neokewl [ Tue Jan 02, 2007 8:00 pm ] |
Post subject: | |
yes b is of type integer as the constructor Circle takes a Point and Int as arguments... yea next time i will put code tags ... sorry 4 tht... |
Author: | Clayton [ Tue Jan 02, 2007 9:47 pm ] |
Post subject: | |
That's the point I'm trying to make though, why not just have your Point have two Floats, and then have the Circle constructor take the point, and a Float? |
Author: | neokewl [ Tue Jan 02, 2007 10:59 pm ] |
Post subject: | |
yes.. it works fine if we define point as type Point = (Float,Float) ... thts the easy way out .... i want to figure out how to do type conversion in haskell .... sometimes the data one gets may not be as we require it .... |
Author: | Clayton [ Tue Jan 02, 2007 11:06 pm ] |
Post subject: | |
well in this sort of application, Floats actually make more sense, because the following is a perfectly legal ordered pair (or point) (1.2, 5.3) and the radius of your circle isn't always going to be an integer. |
Author: | neokewl [ Tue Jan 02, 2007 11:10 pm ] |
Post subject: | |
yes ..i agree with u tht floats make more sense .... but given a situation where u are given int... isnt there a way for type conversion in haskell.... my aim is not to find the area of the circle or rectangle , but it is to understand type conversion in haskell .... its just some problem i formulated myself ..... to do this... |
Author: | wtd [ Wed Jan 03, 2007 1:04 am ] |
Post subject: | |
Perhaps the fromInteger function? |
Author: | neokewl [ Thu Jan 04, 2007 4:16 pm ] |
Post subject: | |
Hi, The fromInteger function works fine for Integer type to float type. Thnx buddy. Cheers neokewl |