
-----------------------------------
neokewl
Tue Jan 02, 2007 7:54 pm

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

-----------------------------------
Clayton
Tue Jan 02, 2007 7:56 pm


-----------------------------------
Is it necessary that 'b' be the type of Integer?

Also, next time please use [code][/code] tags :D

-----------------------------------
neokewl
Tue Jan 02, 2007 8:00 pm


-----------------------------------
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...

-----------------------------------
Clayton
Tue Jan 02, 2007 9:47 pm


-----------------------------------
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?

-----------------------------------
neokewl
Tue Jan 02, 2007 10:59 pm


-----------------------------------
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 ....

-----------------------------------
Clayton
Tue Jan 02, 2007 11:06 pm


-----------------------------------
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.

-----------------------------------
neokewl
Tue Jan 02, 2007 11:10 pm


-----------------------------------
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...

-----------------------------------
wtd
Wed Jan 03, 2007 1:04 am


-----------------------------------
Perhaps the fromInteger function?

-----------------------------------
neokewl
Thu Jan 04, 2007 4:16 pm


-----------------------------------
Hi,

 The fromInteger function works fine for Integer type to float type. Thnx buddy.


Cheers
neokewl
