Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class fraction {
- private final double limit = 1.0E-6;
- //Input a decimal number and get the fraction
- public String getFraction(double x) {
- double h1 = 1, h2 = 0, k1 = 0, k2 = 1;
- double y = x;
- do {
- double a = Math.floor(y);
- double aux = h1;
- h1 = a*h1+h2;
- h2 = aux;
- aux = k1;
- k1 = a*k1+k2;
- k2 = aux;
- y = 1/(y-a);
- } while (Math.abs(x-h1/k1) > x*limit );
- return ((int) h1) + "/" + ((int) k1);
- }
- //some other methods...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement