When I ask GHCi for the golden section, I get the correct answer:
Prelude> (-1 + sqrt 5) / 2
0.6180339887498949
Doing the same inside "xmonad.hs" (for tiling), I receive:
xmonad.hs:32:22: error:
• No instance for (Floating Rational) arising from a use of ‘sqrt’
• In the second argument of ‘(+)’, namely ‘sqrt 5’
In the first argument of ‘(/)’, namely ‘(- 1 + sqrt 5)’
In the expression: (- 1 + sqrt 5) / 2
What happens here, what helps?
If you ask GHCi for the value of this expression and request a
Rationalresult, you get the same error:The issue is that the
Rationaltype doesn't have aFloatinginstance, and you appear to be using the expression(-1 + sqrt 5) / 2in a context where aRationalis expected.This makes sense. The
Rationaltype is intended to represent exact calculations on rational numbers, and there is no exact rational representation of this expression.The
Data.Ratiopackage includes theapproxRationalfunction to get a nearby rational approximation to a non-rational expression, so you should be able to write:to get a rational that's within 0.001 of your desired result: