Haskell: incorrect math results

Viewed 135

I'm trying to use Haskell for evaluating a few functions. One of them is:

k x = 27 * x ^ 4 + 1792 * x ^ 3 + 2592 * x ^ 2 - 324 * x - 243

Using wolfram alpha I've found it's root:

27x^4 + 1792x^3 + 2592x^2 - 324x - 243 = 0
=> x ā‰ƒ 0.331478863194230

But haskell gives the wrong answer when evaluated with the root:

k 0.331478863194230 == -6.252776074688882e-13

I'm sure it's not Wolfram's issue, as Desmos gives the same result. I'm not that familiar with Haskell and I wonder what might be the cause of this quirky behavior.

1 Answers

By the Rational Root Theorem, it's easy to see this polynomial has no rational roots. So Wolfram would never be able to show you an exact answer in decimal form. It of course has to round to a near approximation, and even indicates it's doing so with the ā‰ƒ symbol. It's not surprising that plugging that approximate root into your function doesn't output 0.

In fact, if you avoid floating point numbers you can see with arbitrary precision what the output would be when passing that input:

 > k x = 27 * x ^ 4 + 1792 * x ^ 3 + 2592 * x ^ 2 - 324 * x - 243
 > k 0.331478863194230 :: Rational
=> (-54003681976002235123002052941041334918065893) % 100000000000000000000000000000000000000000000000000000000

which is

-5.4003681976002235123002052941041334918065893e-13
Related