3

So I have started a project where I make a quadratic equation solver and I have managed to do so. My next step is to convert the value of X1 and X2 eg.(X+X1)(X+X2) to an exact fraction, if they become a decimal.

So an example is:

12x2 + 44x + 21

gives me,

X1 = -3.10262885097732

X2 = -0.564037815689349

But how would i be able to convert this to an exact fraction? Thanks for the help!

Community
  • 1
  • 1
John Smith
  • 31
  • 2
  • Possible duplicate of [Algorithm for simplifying decimal to fractions](http://stackoverflow.com/questions/5124743/algorithm-for-simplifying-decimal-to-fractions) – Adrian Sanguineti Jan 31 '17 at 04:35
  • 4
    Those are both irrational numbers. There is no exact fraction to represent them. – JLRishe Jan 31 '17 at 06:07
  • Maybe you can find something useful here: http://stackoverflow.com/questions/5124743/algorithm-for-simplifying-decimal-to-fractions – Slaven Tojić Jan 31 '17 at 07:15
  • 1
    The solutions to your example quadratic are `x = -11/6 - sqrt(29/2)/3` & `x = sqrt(29/2)/3 - 11/6`. The `sqrt(29/2)` part is irrational so there are no exact fractions as JLRishe says. – Enigmativity Jan 31 '17 at 08:02
  • @Enigmativity And `sqrt(29/2)/3` is the same as `sqrt(58)/6`. Usually people write it as `(-11 ± sqrt(58))/6`. – Jeppe Stig Nielsen Jan 31 '17 at 08:32
  • @JeppeStigNielsen - I just posted what WolframAlpha gave me. :-) – Enigmativity Jan 31 '17 at 09:10

1 Answers1

0

You can solve this problem using Continued Fractions.

As stated in comments, you can't obtain a fraction (a rational number) that exactly represents an irrational number, but you can get pretty close.

I implemented once, in a pet project, a rational number type. You can find it here. Look into TryFromDouble for an example of how to get the closest rational number (with specified precision) to any given number using Continued Fractions.

An extract of relevant code is the following (I will Not post the whole type implementation because it is too long, but the code should still be pretty understandable):

public static bool TryFromDouble(double target, double precision, out Rational result)
{
    //Continued fraction algorithm: http://en.wikipedia.org/wiki/Continued_fraction
    //Implemented recursively. Problem is figuring out when precision is met without unwinding each solution. Haven't figured out how to do that.
    //Current implementation computes rational number approximations for increasing algorithm depths until precision criteria is met, maximum depth is reached (fromDoubleMaxIterations)
    //or an OverflowException is thrown. Efficiency is probably improvable but this method will not be used in any performance critical code. No use in optimizing it unless there is
    //a good reason. Current implementation works reasonably well.

    result = zero;
    int steps = 0;

    while (Math.Abs(target - Rational.ToDouble(result)) > precision)
    {
        if (steps > fromDoubleMaxIterations)
        {
            result = zero;
            return false;
        }

        result = getNearestRationalNumber(target, 0, steps++);
    }

    return true;
}

private static Rational getNearestRationalNumber(double number, int currentStep, int maximumSteps)
{
    var integerPart = (BigInteger)number;
    double fractionalPart = number - Math.Truncate(number);

    while (currentStep < maximumSteps && fractionalPart != 0)
    {
        return integerPart + new Rational(1, getNearestRationalNumber(1 / fractionalPart, ++currentStep, maximumSteps));
    }

    return new Rational(integerPart);
}
Community
  • 1
  • 1
InBetween
  • 32,319
  • 3
  • 50
  • 90