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

/*
 * NewJApplet.java
 *
 * Created on Oct 14, 2009, 12:43:02 PM
 */
import java.awt.*;
import java.text.*;

/**
 *
 * @author root
 */
public class NewJApplet extends javax.swing.JApplet implements Runnable {

    private volatile Thread calculatorThread;
    DecimalFormat df;

    /** Initializes the applet NewJApplet */
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void run() {
        Thread thisThread = Thread.currentThread();
        while (calculatorThread == thisThread) {
            try {
                thisThread.sleep(1);
            } catch (InterruptedException e) {
            }
            precision();
            jTextArea1.append(" ready ......\n");
            stop();
        }
    }

    public void start() {
        if (calculatorThread == null) {
            calculatorThread = new Thread(this);
            calculatorThread.start();
        }
    }

    public void stop() {
        Thread tmpcalculatorThread = calculatorThread;
        calculatorThread = null;
        if (tmpcalculatorThread != null) {
            tmpcalculatorThread.interrupt();
        }
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 668, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(647, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 750, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables

    public void precision() {
        double one, x, y, p;
        double powers[];
        int n, i;
        df = new DecimalFormat("##0.00000000000000000");
        powers = new double[100];
        one = 1.0;
        x = 1.0;
        n = 0;
        powers[0] = one;

        // add 1+ 2^(-n)  until the result equals 1
        // we have to keep the compiler from optimizing too much !

        jTextArea1.setTabSize(10);
        jTextArea1.append("     n \t x    \t   1+x  \t  (1+x)-1\n");

        do {


            y = x + one;
            jTextArea1.append("2^-" + n + "   " + df.format(x) + "   " + df.format(y) + "   " + df.format(y - one) + " \n");

            x = x / 2.0;
            n = n + 1;
            powers[n] = x;
        } while (y > one);

        jTextArea1.append(" machine precision reached\n"); 




    }
}
