Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to be able to find low-complexity algebraic approximants to decimal numbers. For example, 1.41 can be approximated as a solution to the polynomial x^2==2. There is a Mathematica function RootApproximant that seems like it should be able to do this approximation. However, I can't get it to work. RootApproximant[N[Sqrt[2]] returns $\sqrt{2}$ correctly, but RootApproximant[1.41] doesn't - it prefers to approximate it as 141/100.

Presumably I need to tell it to lower the accuracy it's aiming for. There is an option called "DegreeCost" that can be passed, but I can't work out how to use that to achieve my goal.

share|improve this question
Note that RootApproximant[1.4142, 7] is Sqrt[2] – Dr. belisarius Mar 25 '13 at 12:49
How about changing the precision of the input: RootApproximant[1.41`3] – Simon Woods Mar 25 '13 at 12:54

Though RootApproximant is doing the right thing for $1.41$, if you persist to get some polynomial what about adding some random precision. Try this!

TrickApprox[loPric_?NumericQ, degree_?IntegerQ, degreeCost_: 2] := Module[{val}, 
  val = ToExpression@(ToString@loPric <>ToString@FromDigits@RandomInteger[{0, 4}, 8]);
  RootApproximant[val, degree, Method -> {"DegreeCost" -> degreeCost}]

Now test it for a cubic polynomial.

TrickApprox[1.41, 3]

Root[-5765 + 7694 #1 - 2705 #1^2 + 104 #1^3 &, 1]

N[%, 3]


If you increase the DegreeCost using the third argument RootApproximant will try not to include higher degree terms in the approximation.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.