Convert double To Rational - Android java.lang

Android examples for java.lang:Double

Description

Convert double To Rational

Demo Code

public class Main {

  public static void doubleToRational(double value, long[] output) {
    doubleToRational(value, output, 0.00001);
  }/*from w  w w  . j  a  v  a  2s .c o m*/

  private static void doubleToRational(double value, long[] output, double error) {
    long number = (long) value;
    value -= number;
    if (value < 0.000001 || error > 1) {
      output[0] = (int) (number + value + 0.5);
      output[1] = 1;
    } else {
      doubleToRational(1.0 / value, output, error / value);
      number = number * output[0] + output[1];
      output[1] = output[0];
      output[0] = number;
    }
  }


}

Related Tutorials