/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mandelbrotset;

import java.math.BigDecimal;
import java.math.MathContext;
import org.apfloat.Apfloat;

/**
 *
 * @author gg
 */
public class FloatTester {
    static int MAX = 100;

    public static void main(String[] args) {

        long time = 0;
        int runtimes = 10000;
        
        time = System.currentTimeMillis();
        for(int i = 0; i < runtimes; i++){
            doitFloat();
        }
        System.out.println("Float: " + (System.currentTimeMillis() - time) + "ms");

        time = System.currentTimeMillis();
        for(int i = 0; i < runtimes; i++){
            doitDouble(); 
        }
        System.out.println("double: " + (System.currentTimeMillis() - time) + "ms");


        time = System.currentTimeMillis();
        for(int i = 0; i < runtimes; i++){
            doitBigDecimal();
        }
        System.out.println("BigDecimal: " + (System.currentTimeMillis() - time) + "ms");


        time = System.currentTimeMillis();
        for(int i = 0; i < runtimes; i++){
            doitApFloat();
        }
        System.out.println("ApFloat: " + (System.currentTimeMillis() - time) + "ms");


    }

    public static void doitDouble(){
        double d = 0.5d;

//        long before = System.currentTimeMillis();
        for(int i = 0; i < MAX; i++){
            d = d*d + 0.5d;
        }
//        return System.currentTimeMillis() - before;
    }

    public static void doitFloat(){
        float f = 0.5f;

//        long before = System.currentTimeMillis();
        for(int i = 0; i < MAX; i++){
            f = f*f + 0.5f;
        }
//        return System.currentTimeMillis() - before;
    }

    public static void doitBigDecimal(){
        MathContext mc = MathContext.DECIMAL32;
        BigDecimal initial = new BigDecimal("0.25", mc);
        BigDecimal bd = initial;

        for(int i = 0; i < MAX; i++){
            bd = bd.multiply(bd, mc).add(initial, mc);
        }
    }


    public static void doitApFloat(){
        Apfloat initial = new Apfloat("0.25", 7);
        Apfloat af = initial;

        for(int i = 0; i < MAX; i++){
            af = af.multiply(af).add(initial);
        }
    }

}
