shamuraq Posted August 18, 2009 Share Posted August 18, 2009 Hi All, I need to create this question in my worksheet. How many 1/3 are there in 3 wholes? So i came out with these: $ans = (1/3) / 3; and i got 0.11111111111111 How do i solve this as a fraction? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 18, 2009 Share Posted August 18, 2009 First off your math is wrong. There are 9 thirds in 3 wholes. Computer math is always stored as integer or floating point. You need to find a math library that supports fractions or write your own. Quote Link to comment Share on other sites More sharing options...
shamuraq Posted August 19, 2009 Author Share Posted August 19, 2009 Yeah ur right. the equation part was wrong. My bad. But the math library. Any suggestion? To write my own would take years from now. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted August 19, 2009 Share Posted August 19, 2009 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. Quote Link to comment Share on other sites More sharing options...
shamuraq Posted August 19, 2009 Author Share Posted August 19, 2009 roopurt18, Thank you sooooo much mate... I'll give it a shot. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.