package JSci.maths;

import java.lang.Comparable;

import JSci.GlobalSettings;
import JSci.maths.groups.AbelianGroup;
import JSci.maths.fields.*;

/**
* The FixedPoint class encapsulates arbitrary fixed-point numbers.
* @version 1.0
* @author Mark Hale
*/
public final class FixedPoint extends Number implements Comparable, Ring.Member {
        private final int man;
        private final int exp;

        /**
        * Constructs a number of the form <code>mantissa</code>*2<sup><code>exponent</code></sup>.
        */
        public FixedPoint(int mantissa, int exponent) {
                man = mantissa;
                exp = exponent;
        }
        /**
        * Compares two fixed point numbers for equality.
        * @param obj a fixed point number.
        */
        public boolean equals(Object obj) {
                if(obj instanceof FixedPoint) {
                        FixedPoint x = (FixedPoint) obj;
                        return (man == x.man) && (exp == x.exp);
                } else
                        return false;
        }
        /**
        * Compares two fixed point numbers.
        * @param obj a fixed point number.
        * @return a negative value if <code>this&lt;obj</code>,
        * zero if <code>this==obj</code>,
        * and a positive value if <code>this&gt;obj</code>.
        */
        public int compareTo(Object obj) throws IllegalArgumentException {
                if(obj!=null && (obj instanceof FixedPoint)) {
                        FixedPoint diff = this.subtract((FixedPoint)obj);
                        if(Math.abs(diff.value())<=GlobalSettings.ZERO_TOL)
                                return 0;
                        else
                                return (int)(diff.value());
                } else
                        throw new IllegalArgumentException("Invalid object.");
        }
        /**
        * Returns a string representing the value of this fixed point number.
        */
        public String toString() {
                return man+" * 2^"+exp;
        }
        /**
        * Returns the double value.
        */
        public double value() {
                return ((double)man)*Math.pow(2.0, exp);
        }
        public int intValue() {
                return (int) value();
        }
        public long longValue() {
                return (long) value();
        }
        public float floatValue() {
                return (float) value();
        }
        public double doubleValue() {
                return value();
        }
        /**
        * Returns the negative of this number.
        */
        public AbelianGroup.Member negate() {
                return new FixedPoint(-man, exp);
        }
        /**
        * Returns the addition of this number and another.
        */
        public AbelianGroup.Member add(final AbelianGroup.Member n) {
                if(n instanceof FixedPoint)
                        return add((FixedPoint)n);
                else
                        throw new IllegalArgumentException("Member class not recognised by this method.");
        }
        /**
        * Returns the addition of this fixed point number and another.
        */
        public FixedPoint add(final FixedPoint n) {
                if(exp == n.exp)
                        return new FixedPoint(man+n.man, exp);
                else
                        throw new IllegalArgumentException("Incompatible exponents.");
        }
        /**
        * Returns the subtraction of this number and another.
        */
        public AbelianGroup.Member subtract(final AbelianGroup.Member n) {
                if(n instanceof FixedPoint)
                        return subtract((FixedPoint)n);
                else
                        throw new IllegalArgumentException("Member class not recognised by this method.");
        }
        /**
        * Returns the subtraction of this fixed point number and another.
        */
        public FixedPoint subtract(final FixedPoint n) {
                if(exp == n.exp)
                        return new FixedPoint(man-n.man, exp);
                else
                        throw new IllegalArgumentException("Incompatible exponents.");
        }
        /**
        * Returns the multiplication of this number and another.
        */
        public Ring.Member multiply(final Ring.Member n) {
                if(n instanceof FixedPoint)
                        return multiply((FixedPoint)n);
                else
                        throw new IllegalArgumentException("Member class not recognised by this method.");
        }
        /**
        * Returns the multiplication of this fixed point number and another.
        */
        public FixedPoint multiply(final FixedPoint n) {
                return new FixedPoint(man*n.man, exp+n.exp);
        }
}

