3
votes

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
2
You don't need that import GHC.Float, as realToFrac is a standard Haskell function reexported from the Prelude.Ørjan Johansen

2 Answers

8
votes

The general function to use to convert between different floating point types (and some others, such as Rational) is realToFrac.

0
votes

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