1  
     2  /**
     3   * com.sekati.math.RuleOfThree
     4   * @version 1.0.1
     5   * @author jason m horwitz | sekati.com
     6   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     7   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     8   * 
     9   * Adapted from nectere for dependencies
    10   */
    11   
    12  /**
    13   * The rule of three is the method of finding the fourth term of a mathematical proportion when the first three terms are known.
    14   * {@code Usage:
    15   * 	var calc:RuleOf3 = new RuleOf3 (null, 500, 25, 100, 0); // partial value is 125;
    16   * }
    17   * @see http://en.wikipedia.org/wiki/Rule_of_three_%28mathematics%29
    18   */
    19  class com.sekati.math.RuleOf3 extends Number {
    20  
    21  	private var _n:Number;
    22  
    23  	/**
    24  	 * @param partialValue (Number)
    25  	 * @param totalValue (Number)
    26  	 * @param partialPercent (Number)
    27  	 * @param totalPercent (Number)
    28  	 * @param zeroPercentValue (Number)
    29  	 * @throws Error on fault. 
    30  	 */
    31  	public function RuleOf3(partialValue:Number, totalValue:Number, partialPercent:Number, totalPercent:Number, zeroPercentValue:Number) {
    32  		//defaults to 0% == 0
    33  		if (zeroPercentValue == null) {
    34  			zeroPercentValue = 0;
    35  		}
    36  		//calculate the null value    
    37  		if (partialValue == null) {
    38  			//partialValue
    39  			_n = ((totalValue - zeroPercentValue) * partialPercent / totalPercent) + zeroPercentValue;
    40  			return;
    41  		} else if (totalValue == null) {
    42  			//totalValue
    43  			_n = ((partialValue - zeroPercentValue) * totalPercent / partialPercent) + zeroPercentValue;
    44  			return;
    45  		} else if (partialPercent == null) {
    46  			//partialPercent
    47  			_n = ((partialValue - zeroPercentValue) * totalPercent) / (totalValue - zeroPercentValue);
    48  			return;
    49  		} else if (totalPercent == null) {
    50  			//totalPercent
    51  			_n = ((totalValue - zeroPercentValue) * partialPercent) / (partialValue - zeroPercentValue);
    52  			return;
    53  		}
    54  		//error
    55  		throw new Error( "@@@ com.sekati.math.RuleOf3 Error: could not calculate faulty arguments." );
    56  	}
    57  
    58  	/**
    59  	 * return number of representation of _n
    60  	 * @return Number
    61  	 */
    62  	public function valueOf():Number {
    63  		return _n;	
    64  	}
    65  
    66  	/**
    67  	 * return string representation of _n
    68  	 * @return String
    69  	 */
    70  	public function toString():String {
    71  		return _n.toString( );
    72  	}
    73  }