Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ApproximateDivision {
- private long tOf(int b) {
- long t = 1;
- while(t < b)
- t <<= 1;
- return t;
- }
- public double quotient(int a, int b, int terms) {
- long t = tOf(b);
- long c = t-b;
- System.out.println("t: " + t);
- System.out.println("c: " + c);
- double q = 0;
- for(int i=0; i<terms; i++)
- q += Math.pow(c, i) / (t<<i);
- System.out.println("q: " + q);
- return a*q;
- }
- public static void main(String[] args) {
- System.out.println(new ApproximateDivision().quotient(2, 5, 2));
- // System.out.println(new ApproximateDivision().quotient(7,8,5));
- // System.out.println(new ApproximateDivision().quotient(1,3,10));
- // System.out.println(new ApproximateDivision().quotient(1,10000,2));
- // System.out.println(new ApproximateDivision().quotient(1,7,20));
- // System.out.println(new ApproximateDivision().quotient(0,4,3));
- // System.out.println(new ApproximateDivision().quotient(50,50,1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement