[HOpenGL] GLfloat vs. GLdouble

Sven Panne Sven_Panne@BetaResearch.de
Tue, 11 Dec 2001 09:31:45 +0100


Andre W B Furtado wrote:
> [sorry for the silly question]

It's not *that* silly, most people have to browse through Haskell's strange
numeric class hierarchy to answer it...   :-[

> Which is the correct way to convert a GLfloat to a GLdouble? (There is any
> Prelude function that converts a Float to a Double?)

Given the fact that one doesn't know what floating types GLfloat/GLdouble
correspond to exactly, one has to go a rather generic way via Rational:
In class Real there is

   toRational :: Real a => a -> Rational

and class Fractional contains

   fromRational :: Fractional a => Rational -> a

As a convenience function, the Prelude contains

   realToFrac   :: (Real a, Fractional b) => a -> b
   realToFrac   = fromRational . toRational

which does exactly what you want, since Float and Double have both instances
for Real and Fractional.

Not very intuitive, but that's the way Haskell is (currently) defined...

A performance note: GHC's Prelude has rules in its Prelude to avoid going
via Rational for all 4 possible combinations of Float and Double for
realToFrac, so it's relatively cheap.

Cheers,
   S.