Jump to content

Solving equation as fraction rather than decimal...Anyone?


shamuraq

Recommended Posts

You might try this:

http://www.phpclasses.org/browse/package/4628.html

 

Following is a small bit of code I wrote to calculate probabilities.  A probability is just a fraction, i.e. 1 out of 6 or 1 / 6.  It may give you a couple hints of how you can construct your own if you can't find a pre-built solution:

 

<?php
class Probability {
    public $For = 0;
    public $Total = 0;
    
    /**
    * Returns NOT P
    * 
    * @param Probability $a
    * @return Probability
    */
    public static function NotP( Probability $a ) {
        return new Probability( $a->Total - $a->For, $a->Total );    
    }
    
    /**
    * Multiply two probabilities together
    * 
    * @return Probability
    */
    public static function Multiply() {
        $c = new Probability( 1, 1 );
        $args = func_get_args();
        foreach( $args as $arg ) {
            if( !($arg instanceof Probability) ) {
                throw new Exception( 'Argument is not a Probability instance ' . __METHOD__ );
            }
            $c->For *= $arg->For;
            $c->Total *= $arg->Total;
            $c->Reduce();
        }
        return $c->Reduce();
    }
    
    public function __construct( $for, $total ) {
        $this->For = (int)$for;
        $this->Total = (int)$total;  
        if( !$this->IsValid() ) { throw new Exception( 'Invalid probabiity: ' . $this ); }  
    }
    
    public function IsValid() {
        return $this->Total !== 0 && $this->For >= 0 && $this->For <= $this->Total;
    }
    
    public function Reduce() {
        $gcd = calc_gcd( $this->For, $this->Total );
        $this->For /= $gcd;
        $this->Total /= $gcd;
        return $this;    
    }
    
    public function __toString() {
        $against = $this->Total - $this->For;
        return "P: {$this->For} / {$this->Total}, !P: {$against} / {$this->Total}";
    }
    
    public function Value( $prec ) {
        $prec = (int)$prec;
        return round( 100 * $this->For / $this->Total, $prec );
    }
}

/**
* Reduce a fraction.
* 
* @param integer $num
* @param integer $den
*/
function frac_reduce( &$a, &$b ) {
    $gcd = calc_gcd( $a, $b );
    $a /= $gcd;
    $b /= $gcd;
}

/**
* Calculates the greatest common divisor of a bunch of numbers
* 
* @return integer
*/
function calc_gcd() {
    $args = func_get_args();
    foreach( $args as $k => $v ) {
        $v = (int)abs($v);
        if( $v === 0 ) {
            return 1;
        }
        $args[$k] = $v;
    }
    $min = call_user_func_array( 'min', $args );
    while( $min > 1 ) {
        $t = true;
        foreach( $args as $v ) {
            $t = $t && ($v % $min === 0);
            if( $t === false ) {
                break;
            }
        }
        if( $t === true ) {
            break;
        }
        $min--;
    }
    return $min;
}

?>

 

It's not the best code as I wrote it as a quick side project, but it may help.  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.