Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. package testbench;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.BigInteger;
  5. import java.math.MathContext;
  6. import java.util.Random;
  7.  
  8. import Bench.IBenchmark;
  9.  
  10. class Dummy {
  11.     public void m() {
  12.     }
  13. }
  14.  
  15. public class TestCPUDigitsOfPi implements IBenchmark {
  16.     protected int array[];
  17.     protected int n;
  18.     private boolean running;
  19.    
  20.     BigDecimal a = new BigDecimal(1.0);
  21.     BigDecimal b = new BigDecimal(1.0 / (2^(1/2)));
  22.     BigDecimal t = new BigDecimal(1/4.0);
  23.     BigDecimal p = new BigDecimal(1.0);
  24.    
  25.    
  26.    
  27.    
  28.     public void initialize(Object ...params) {
  29.        
  30.         this.running = true; //set running
  31.         Random random = new Random();
  32.         this.n = (Integer) params[0];
  33.         this.array = new int[n];
  34.        
  35.         for(int i = 0; i < n; i++)
  36.             array[i] = random.nextInt(1000);
  37.        
  38.     }
  39.  
  40.  
  41.     private void computePi3() {
  42.     }
  43.  
  44.     private void computePi2() {
  45.        
  46.     }
  47.    
  48.    
  49.     public static int log2(int x)
  50.     {
  51.         return (int)(Math.log(x) / Math.log(2));
  52.     }
  53.    
  54.    
  55.     BigDecimal pi = new BigDecimal("0");
  56.     BigDecimal a1;
  57.     MathContext mc = new MathContext(100);
  58.     MathContext power = new MathContext(2);
  59.    
  60.     private BigDecimal computePi1(int n) // numar de cifre corecte in pi
  61.     {
  62.         double i = 0;
  63.         System.out.println(log2(n));
  64.         for(i = 0; i <= n; i++)//while(i <= n )//log2(n)
  65.         {
  66.            
  67.             a1 = (a.add(b, mc));
  68.             a1 = a1.divide(new BigDecimal(2.0), mc); //inainte avea mc aici
  69.            
  70.             b = (a.multiply(b, mc));
  71.             b = b.sqrt(mc);        
  72.            
  73.             t = t.subtract(p.multiply((a.subtract(a1).pow(2, mc)), mc));
  74.            
  75.             p = p.multiply(new BigDecimal(2.0), mc);
  76.            
  77.             //a = a1;
  78.            
  79.         }
  80.         pi = ((a1.add(b, mc)).pow(2, mc)).divide(t.multiply(new BigDecimal(4.0), mc), mc);  
  81.         return pi;
  82.     }
  83.  
  84.     public void run(Object[] ... param) {
  85.     }
  86.  
  87.     @Override
  88.     public void clean() {
  89.        
  90.     }
  91.  
  92.     @Override
  93.     public void cancel() {
  94.         this.running = false; //clear running
  95.     }
  96.  
  97.     @Override
  98.     public void run(Object... param) {
  99.         int option = (Integer)(param[0]);
  100.         switch(option)
  101.         {
  102.         case 1:
  103.             BigDecimal pi = new BigDecimal(1.0);
  104.             pi = computePi1((Integer)(param[1])); //al doilea argument
  105.             System.out.println(pi);
  106.             break;
  107.         case 2:
  108.             computePi2();
  109.             break;
  110.         case 3:
  111.             computePi3();
  112.             break;
  113.         default:
  114.             throw new IllegalArgumentException("Invalid option, press 1 - 3");
  115.         }
  116.        
  117.     }
  118.  
  119.     @Override
  120.     public void warmup() {
  121.         for (int i = 0; i < 100000; i++) {
  122.             Dummy dummy = new Dummy();
  123.             dummy.m();
  124.         }
  125.        
  126.     }
  127.  
  128.  
  129.     @Override
  130.     public void run() {
  131.         // TODO Auto-generated method stub
  132.        
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement