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