Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1.  
  2. public class ApproximateDivision {
  3.  
  4.     private long tOf(int b) {
  5.         long t = 1;
  6.         while(t < b)
  7.             t <<= 1;
  8.        
  9.         return t;
  10.     }
  11.    
  12.     public double quotient(int a, int b, int terms) {
  13.        
  14.         long t = tOf(b);
  15.         long c = t-b;
  16.        
  17.         System.out.println("t: " + t);
  18.         System.out.println("c: " + c);
  19.        
  20.         double q = 0;
  21.        
  22.         for(int i=0; i<terms; i++)
  23.             q += Math.pow(c, i) / (t<<i);
  24.        
  25.         System.out.println("q: " + q);
  26.        
  27.         return a*q;
  28.     }
  29.    
  30.     public static void main(String[] args) {
  31.         System.out.println(new ApproximateDivision().quotient(2, 5, 2));
  32. //      System.out.println(new ApproximateDivision().quotient(7,8,5));
  33. //      System.out.println(new ApproximateDivision().quotient(1,3,10));
  34. //      System.out.println(new ApproximateDivision().quotient(1,10000,2));
  35. //      System.out.println(new ApproximateDivision().quotient(1,7,20));
  36. //      System.out.println(new ApproximateDivision().quotient(0,4,3));
  37. //      System.out.println(new ApproximateDivision().quotient(50,50,1));
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement