view src/decimal/math.d @ 4:b37c218c1442

Initial development of BCD integers
author Paul (paul.d.anderson@comcast.net)
date Thu, 18 Mar 2010 18:10:25 -0700
parents a984d3056cc4
children
line wrap: on
line source

/** 
 * A D programming language implementation of the 
 * General Decimal Arithmetic Specification, 
 * Version 1.70, (25 March 2009).
 *
 * by Paul D. Anderson
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
**/

module decimal.math;

import decimal.decimal;
import decimal.context;
//import std.math;

public:
    
    //--------------------------------
    //
    // CONSTANTS
    //
    //--------------------------------
	
/+	public Decimal HALF;
	public Decimal ONE = Decimal(1);
	public Decimal ZERO = Decimal(0);
	
	static {
		HALF = Decimal("0.5");
	}+/

    /**
     * Returns the value of e to the default precision.
     */    
    Decimal e() {
        Decimal result;
        return result;
    }
     
    /**
     * Returns the value of e to the specified precision.
     */    
    Decimal e(uint precision) {
        Decimal result;
        return result;
    }
     
/+
    /**
     * Returns the value of pi to the default precision.
     */    
    Decimal pi() {
		Decimal ONE = Decimal(1);
		writeln("1 = ", ONE);
		Decimal TWO = Decimal(2);
		writeln("2 = ", TWO);
		Decimal HALF = Decimal(0.5);
		writeln("0.5 = ", HALF);
        Decimal x = sqrt(TWO);
		writeln("x = ", x);
		Decimal y = sqrt(x);
		writeln("y = ", y);
		Decimal p = TWO + x;
		writeln("p = ", p);
		x = y;
		int i = 0;
		while (true) {
			writeln("i = ", i);
			x = HALF * (x + ONE/x);
			writeln("step 1");
			Decimal np = p * ((x + ONE)/(y + ONE));
			writeln("step 2");
			writeln("np = ", np);
			if (p == np) return p;
			writeln("step 3");
			p = np;
			writeln("step 4");
			x = sqrt(x);
			writeln("step 5");
//			writeln("x = ", x);
			Decimal oox = ONE/x;
//			writeln("ONE/x = ", oox);
//			writeln ("x + ONE/x = ", x + oox);
//			Decimal t1 = oox + x;
//			writeln("t1 = ", t1);
//			Decimal t1 = x + oox; // ONE + oox; //x + x; // + ONE/x;
			writeln("step 6");
//			Decimal t2 = (y  * x) + ONE; ///x; //ONE / (y + ONE);
//			y = ONE/x + y * x;
			writeln("step 7");
			y = (ONE/x + y * x) / (y + ONE); //t1 / t2;
			writeln("step 8");
			i++;
//			break;
		}
        return p;
    }
	
	unittest {
		write("pi....");
		writeln("pi = ", pi());
	}
+/     
    /**
     * Returns the value of pi to the specified precision.
     */    
    Decimal pi(uint precision) {;
        Decimal result;
        return result;
    }
     
    /**
     * Returns the square root of the argument to the current precision.
     */    
    Decimal sqrt(Decimal arg) {
		return sqrt(arg, context.precision);
    }
	
	
	unittest {
		write("sqrt.....");
		Decimal dcm = Decimal(4);
		assert(sqrt(dcm) == Decimal(2));
		writeln("sqrt(2) = ", sqrt(Decimal(2)));
	}
     
    /**
     * Returns the square root of the argument to the specified precision.
     */    
    Decimal sqrt(Decimal arg, uint precision) {
		pushContext();
		context.precision = precision + 2;
		Decimal HALF = Decimal(0.5);
		Decimal ONE = Decimal(1);
		Decimal x = HALF * (arg + ONE);
		Decimal xp;
		while(true){
			xp = x;
			x = HALF * (x + arg/x);
			if (x == xp) break;
		}
		popContext();
		round(xp);
        return xp;
    }
    
    //--------------------------------
    //
    // EXPONENTIAL AND LOGARITHMIC FUNCTIONS
    //
    //--------------------------------
    
    /**
     * Decimal version of std.math function.
     * Required by General Decimal Arithmetic Specification
     *
     */
    Decimal exp(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     * 2^x
     */
    Decimal exp2(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     * exp(x) - 1
     */
    Decimal expm1(Decimal arg) {
        Decimal result;
        return result;
    }
   
    /**
     * Decimal version of std.math function.
     * Required by General Decimal Arithmetic Specification
     *
     */
    Decimal log(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     * log(1 + x)
     */
    Decimal log1p(Decimal arg) {
        Decimal result;
        return result;
    }
   
    /**
     * Decimal version of std.math function.
     * Required by General Decimal Arithmetic Specification
     *
     */
    Decimal log10(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     * Required by General Decimal Arithmetic Specification
     *
     */
    Decimal log2(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     * Required by General Decimal Arithmetic Specification
     *
     */
    Decimal pow(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    //--------------------------------
    //
    // TRIGONOMETRIC FUNCTIONS
    //
    //--------------------------------
    
    /**
     * Decimal version of std.math function.
     *
     */
    Decimal sin(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal cos(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Replaces std.math function expi
     *
     */
    Decimal[] sincos(Decimal arg) {
        Decimal[] result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal tan(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal asin(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal acos(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal atan(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal atan2(Decimal y, Decimal x) {
        Decimal result;
        return result;
    }

    //--------------------------------
    //
    // HYPERBOLIC TRIGONOMETRIC FUNCTIONS
    //
    //--------------------------------
    
    /**
     * Decimal version of std.math function.
     *
     */
    Decimal sinh(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal cosh(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal tanh(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal asinh(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal acosh(Decimal arg) {
        Decimal result;
        return result;
    }

    /**
     * Decimal version of std.math function.
     *
     */
    Decimal atanh(Decimal arg) {
        Decimal result;
        return result;
    }

/+    //--------------------------------
    //
    // General Decimal Arithmetic Specification Functions
    //
    //--------------------------------
    
    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal compare(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal compareSignal(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal compareTotal(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }
    
    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal divideInteger(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal fma(Decimal op1, Decimal op2, Decimal op3) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal ln(Decimal op1) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal log10(Decimal op1) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal max(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal maxMagnitude(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal min(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal minMagnitude(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal nextMinus(Decimal op1) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal nextPlus(Decimal op1) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal nextToward(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal power(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal remainder(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal remainderNear(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     * 
     * NOTE: performs both round-to-integral-exact and
     * round-to-integral-value
     *
     * TODO: implement
     */
    Decimal rint(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

// logical operations

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal and(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal or(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal xor(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }

    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal invert(Decimal op1) {
        Decimal result;
        return result;
    }


    /**
     * part of spec
     *
     * TODO: implement
     */
    Decimal compareTotal(Decimal op1, Decimal op2) {
        Decimal result;
        return result;
    }
+/