Reputation: 41
I want to convert a Double to a GLfloat. I want to use it for comparations.
xM <- newIORef 0.0
zM <- newIORef 0.0
mobs <- newIORef []
mapM_ (\x -> colision x xM) mobs
mobs is fulled with a method.
colision mob xC = do
xcama <- get xC
--zcama <- get zC
let menor = mob!!0
let mayor = mob!!7
--if xm>= xmin && xm <= xmax && zm >= zmin && zm <= zmax then renderText (1, (-1.4)) $ "Dead"
--else renderText (1, (-1.4)) $ "Warning..."
renderText (1, (-1.4)) $ "Warning..."
When I try to compile it show me this error:
"Couldn't match type 'Foreign.C.Types.CDouble' with 'Foreign.C.Types.CFloat' Expected type :GLfloat
Actual type: GLdouble
-----Solution:------
I use this code:
import GHC.Float
d2f = realToFrac :: GLdouble -> GLfloat
Upvotes: 4
Views: 2132
Reputation: 505
Faster:
import Unsafe.Coerce(unsafeCoerce)
import GHC.Float(double2Float)
doubleToGF = unsafeCoerce . double2Float :: Double -> GLfloat
I've been wishing for a way to make this less "unsafe" with Data.Coerce. Hasn't panned out as of yet
Upvotes: 0
Reputation: 18189
The general function to use to convert between different floating point types (and some others, such as Rational
) is realToFrac
.
Upvotes: 9