public static function divide(Fraction $a, Fraction $b) { $num = $a->numerator * $b->denominator; $den = $a->denominator * $b->numerator; $sum = new Fraction($num, $den); $sum->reduce(); $sum->show(); }
/** @param array $set [ varname => fraction ] */ function __construct(array $set) { foreach ($set as $var => $coeff) { $set[$var] = Fraction::create($coeff); } ksort($set); $this->set = $set; }
/** @return Restriction */ function fixRightSide() { if ($this->limit->isLowerThan(0)) { $set = array(); foreach ($this->set as $var => $coeff) { $set[$var] = $coeff->multiply(-1); } $type = $this->type === self::TYPE_EQ ? $this->type : ($this->type === self::TYPE_GOE ? self::TYPE_LOE : self::TYPE_GOE); $this->limit = $this->limit->multiply(-1); } else { $set = $this->set; $type = $this->type; } $this->set = $set; $this->type = $type; return $this; }
public static function multfract($c, $d, $i, $f) { Fraction::check($d); //checking if denominator is not zero Fraction::check($f); //checking if denominator is not zero $mf = $c / $d * ($i / $f); echo $c . "/" . $d . " * " . $i . "/" . $f . " = " . number_format($mf, 2, ',', '') . '<br/>'; }
/** * @param string $var * @param array $set * @param Fraction|numeric $b */ function __construct($var, array $set, $b) { parent::__construct($set); $this->var = (string) $var; $this->b = Fraction::create($b); }
if (isset($_REQUEST['uploaded_filenamepath'])) { printf("<p>non-GPC: filename='%s'</p>\n", $_REQUEST['uploaded_filenamepath']); } } $fp = fopen($tmpName, 'r'); if ($fp != 0) { $content = fread($fp, $fileSize); fclose($fp); } else { $content = "bogus\n"; } printf("<p>file content:</p><pre>%s</pre>\n", $content); // print_r(hash_algos()); printf("<p>hash = '%s'</p>\n", hash("crc32", $content)); printf("<p>Test fractions ...</p>\n"); Fraction::Test(); } // collect configuration: load presets, append custom file content. Feed that combined mess to the 'cleaner' and then the generator. $config = ""; if (isset($_REQUEST['language'])) { $language = strip_tags($_REQUEST['language']); if (strlen($language) > 0) { $config .= sprintf("language: %s\n", $language); } } if (isset($_REQUEST['chapter'])) { $chapter = (int) $_REQUEST['chapter']; $config .= sprintf("chapter: %d\n", $chapter); } if (isset($_REQUEST['ex_cnt'])) { $ex_cnt = (int) $_REQUEST['ex_cnt'];
private function setFractionData() { if (isset($this->phkRequestData['fraction_id']) && !isset($this->phkRequestData['fraction_text'])) { App::import("Model", "Fraction"); $fraction = new Fraction(); $result = $fraction->find("first", array('conditions' => array('Fraction.id' => $this->phkRequestData['fraction_id']))); if (count($result)) { $this->phkRequestData['fraction_id'] = $result['Fraction']['id']; $this->phkRequestData['fraction_text'] = $result['Fraction']['description']; $this->phkRequestData['condo_id'] = $result['Fraction']['condo_id']; } } }
public static function Test() { $vut = 0.1; $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 1 failed!</p>\n"); } $vut = 0.99999997; $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 2 failed!</p>\n"); } $vut = (0x40000000 - 1.0) / (0x40000000 + 1.0); $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 3 failed!</p>\n"); } $vut = 1.0 / 3.0; $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 4 failed!</p>\n"); } $vut = 1.0 / (0x40000000 - 1.0); $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 5 failed!</p>\n"); } $vut = 320.0 / 240.0; $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 6 failed!</p>\n"); } $vut = 6.0 / 7.0; $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 7 failed!</p>\n"); } $vut = 320.0 / 241.0; $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 8 failed!</p>\n"); } $vut = 720.0 / 577.0; $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 9 failed!</p>\n"); } $vut = 2971.0 / 3511.0; $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 10 failed!</p>\n"); } $vut = 3041.0 / 7639.0; $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 11 failed!</p>\n"); } $vut = 1.0 / sqrt(2.0); $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 12 failed!</p>\n"); } $vut = 3.1415279; $ret = Fraction::toFract($vut); if (!(abs($vut - $ret->Val()) < 1.0E-9)) { printf("<p>test 13 failed!</p>\n"); } }
/** * @param array $set * @param Fraction|numeric $value */ function __construct(array $set, $value) { parent::__construct($set); $this->value = Fraction::create($value); }
function Fraction_QuantizeValue($val, $idx, $frac_prec, &$first_denom, $style_arr, $rnd, $oc) { $ret = Fraction::toFractWithPrecision($val, $frac_prec); $nofrac1 = FALSE; $redux = FALSE; $prime_max = 0; if (DebugReporting::$dbg >= 4) { printf("fraction = %s/%s (@ prec=%s)\n", $ret->num, $ret->denom, $frac_prec); } $pos_l = 0; $pos_h = $oc; // span left hand AND right hand item! foreach ($style_arr as $st) { switch ($st) { default: if (!strncmp($st, "prime_max=", 10)) { $prime_max = (int) substr($st, 10); } else { if (!strncmp($st, "step=", 5)) { // calculus values may only be values, rounded to a given factor. $factor = (double) substr($st, 5); if (DebugReporting::$dbg >= 2) { printf("<p>fraction step: factor = %s, ret= %s, idx = %d, zero_ok = %d, pos_l = %d, pos_h = %d\n", $factor, $val, $idx, $zero_ok, $pos_l, $pos_h); } if ($idx >= $pos_l && $idx <= $pos_h) { $val = (int) ($val / $factor) * $factor; if (DebugReporting::$dbg >= 2) { printf("<p>fraction step: corrected ret: %s, factor = %s, idx = %d, zero_ok = %d\n", $val, $factor, $idx, $zero_ok); } $ret = Fraction::toFractWithPrecision($val, $frac_prec); } } else { if ($st[0] == 'T') { // value may only be one of a specified set // // originally only intended for [tables of] multiplication, but now usable anywhere. if ($idx >= $pos_l && $idx <= $pos_h) { // allow only particular tables of multiplication: set following the T! // since the multiplication tables may go BEYOND 10 while we only allow multipliers up to 10, // we cannot do this by 'filtering' the currently generated material. // Instead, we just pick the table of choice from the list and apply that to lvalue[0]. // // Of course, the answer must be recalculated too! $multipliers = explode(',', substr($st, 1)); $diff = 4000000000.0; $val = $ret->Val(); foreach ($multipliers as $m) { $delta = abs($val - (int) $m); if ($delta < $diff) { $val = (int) $m; $ret->num = $val * $ret->denom; $diff = $delta; } } if (DebugReporting::$dbg >= 2) { printf("<p>T table quatization: delta: %s, idx = %d, val: %s\n", $diff, $idx, $val); } } } else { if ($st[0] == 'p' && FALSE !== strpos("3456789", substr($st, 1)) && strlen($st) == 2) { // next filters apply to Nth (and subsequent) lvalues only! $pos_l = (int) substr($st, 1) - 1; $pos_h = $oc - 1; } } } } break; case "p1": // next filters apply to first lvalue only! $pos_l = 0; $pos_h = 0; break; case "p2": // next filters apply to second (and subsequent) lvalues only! $pos_l = 1; $pos_h = $oc - 1; break; case "pO": // next filters apply to answer (right hand) lvalue only! $pos_l = $oc; $pos_h = $oc; break; case "px": // next filters apply to ANY lvalues! $pos_l = 0; $pos_h = $oc - 1; break; case "nofrac1": // we do not accept a divisor of '1'! if ($idx >= $pos_l && $idx <= $pos_h) { $nofrac1 = TRUE; } break; case "redux": // the produced fraction MUST be reducible if ($idx >= $pos_l && $idx <= $pos_h) { $redux = TRUE; } break; case "1denom": // only permit one (common) denominator: take the denom of lvalue[0] and apply to all if (!$first_denom || $idx == 0) { $first_denom = $ret->denom; } else { if ($ret->denom != $first_denom) { // ensure the /value/ of the fraction, which gets its denominator patched, stays close to what it was: $rescale = $first_denom / $ret->denom; $ret->num = (int) ($rescale * $ret->num + 0.5); $ret->denom = $first_denom; } } break; case "1denom_M": // only permit one (common) denominator: take the denom of lvalue[0] and apply to all // // same as '1denom' apart from the fact that it also accepts 'non-reduced' fractions with the // same denominator, i.e. if denominator has been set to be '8', the fraction '1/2' is okay // too as it can be expressed in 8ths: '4/8'. // // It also picks the denominator at random, while '1denom' just picks the denominator of the // first term. if (!$first_denom || $idx == 0) { // pick a 'common denominator' at random and apply that one to each term as it comes along. $m_range = (int) (1.0 / ($frac_prec * $ret->denom)); $m = (int) ($d_range * $rnd->frand()) + 1; $d = $ret->denom * $m; if ($prime_max > 0 && !CheckPrimeMax($d, $prime_max, $frac_prec)) { $d = $ret->denom; } $first_denom = $d; } $factor = $first_denom / $ret->denom; // is our fraction expressible as a $first_denom fraction? if not: tweak! if (!FltEquals((int) $factor, $factor, $frac_prec)) { // ensure the /value/ of the fraction, which gets its denominator patched, stays close to what it was: $ret->num = (int) ($factor * $ret->num + 0.5); $ret->denom = $first_denom; } break; } } if ($prime_max > 0 && !CheckPrimeMax($ret->denom, $prime_max, $frac_prec)) { if (DebugReporting::$dbg >= 1) { printf("<p>boom at checkprimemax (%s/%s @ %s)\n", $ret->num, $ret->denom, $prime_max); } throw new Exception(sprintf("value not acceptable: fraction denominator %s is contains primes beyond the maximum allowed %s", $ret->denom, $prime_max)); } if ($redux) { // ensure the fraction is reducible! $gcd = CalcGCD($ret->num, $ret->denom); if ($gcd <= 1) { $m = 1; if (!$first_denom || $idx == 0) { $m_range = (int) (1.0 / ($frac_prec * $ret->denom)) - 1; $m = 2 + (int) ($m_range * $rnd->frand()); $first_denom = $m * $ret->denom; } if ($m <= 1) { if (DebugReporting::$dbg >= 1) { printf("<p>boom at GCD (%s/%s @ %s / first=%s / idx = %s)\n", $ret->num, $ret->denom, $m, $first_denom, $idx); } throw new Exception(sprintf("value not acceptable: fraction %s/%s is not expandible nor reducible", $ret->num, $ret->denom)); } } } return $ret->Val(); }
/** @return Table */ function nextStep() { if ($this->z2 !== NULL && !$this->hasHelperInBasis()) { $this->removeHelpers(); } $newrows = array(); $keycol = $this->getKeyColumn(); $keyrow = $this->getKeyRow(); if ($keycol === NULL || $keyrow === NULL) { return $this; } foreach ($this->rows as $row) { $rowset = array(); if ($row->getVar() === $keyrow->getVar()) { $var = $keycol; $b = $row->getB()->divide($keyrow->get($keycol)); foreach ($row->getSet() as $v => $c) { $rowset[$v] = $c->divide($keyrow->get($keycol)); } } else { $var = $row->getVar(); $set = $row->getSet(); $dvd = $set[$keycol]->multiply(-1)->divide($keyrow->get($keycol)); $b = $dvd->multiply($keyrow->getB())->add($row->getB()); foreach ($row->getSet() as $v => $c) { $rowset[$v] = $dvd->multiply($keyrow->get($v))->add($c); } } $newrows[$var] = array($rowset, $b); } $this->rows = array(); foreach ($newrows as $var => $meta) { $this->addRow(new TableRow($var, $meta[0], $meta[1])); } foreach (array('z', 'z2') as $zvar) { if ($this->{$zvar} === NULL) { continue; } $zcoeffs = array(); $zdvd = $this->{$zvar}->get($keycol)->multiply(-1)->divide($keyrow->get($keycol)); foreach ($this->{$zvar}->getSet() as $v => $c) { $zcoeffs[$v] = $zdvd->multiply($keyrow->get($v))->add($c); } $this->{$zvar} = new TableRow($zvar, $zcoeffs, $zdvd->multiply($keyrow->getB())->add($this->{$zvar}->getB())); } // find solution (if any) if ($this->isSolved()) { if ($this->solution !== FALSE) { $this->solution = array(); foreach ($this->z->getVariableList() as $var) { if (strncmp($var, 'y', 1) === 0) { continue; } foreach ($this->rows as $row) { if ($row->getVar() === $var) { $this->solution[$var] = $row->getB(); continue 2; } } $this->solution[$var] = Fraction::create(0); } ksort($this->solution); } } return $this; }
if ($recipe->fields['recipe_picture_type'] != NULL) { ?> <br/> <a href="./index.php?m=recipes&a=view_picture_mob&recipe_id=<?php echo $recipe_id; ?> ">View Picture</a> <?php } echo "</div>"; echo "<b>" . $LangUI->_('Ingredients') . ":</b>"; // Save the optional ingredients for later display $optIng = array(); // Iterate through the ingredients while (!$ingredients->EOF) { $fraction = new Fraction($ingredients->fields['map_quantity'] * $scale_by); $quant = $fraction->toString(); if ($ingredients->fields['map_optional'] != $DB_LINK->true) { // print out the ingredient echo "<br />"; echo $quant; if ($ingredients->fields['unit_desc'] != $LangUI->_('Unit')) { echo ' ' . $ingredients->fields['unit_desc'] . $LangUI->_('(s)'); } echo ' ' . $ingredients->fields['map_qualifier']; echo ' ' . $ingredients->fields['ingredient_name']; } else { // save this one for later $optIng[] = array('id' => $ingredients->fields['map_ingredient'], 'name' => $ingredients->fields['ingredient_name'], 'unit' => $ingredients->fields['unit_desc'], 'unit_id' => $ingredients->fields['map_unit'], 'qualifier' => $ingredients->fields['map_qualifier'], 'quantity_dec' => $ingredients->fields['map_quantity'], 'quantity' => $quant, 'int_quant' => $ingredients->fields['map_quantity'] * $scale_by); } $ingredients->MoveNext();
function readIngredientLine($line) { $contLastLine = false; /* * Extract the three parts of a ingredient (name, amount, measurement) * into separate strings. They are separated according to their length: * amount: columns 0-6 * measurement: columns 8-9 * name: columns 11+ */ $amount = substr($line, 0, 7); // col 0-6 $measurement = substr($line, 8, 2); // col 8-9 $name = substr($line, 11); // col 11+ $amount = trim($amount); $measurement = trim($measurement); $name = trim($name); if (intval($amount) == 0) { $amount = $amount; } else { $amount = Fraction::strToFloat($amount); } if ($name != "" && $name[0] == '-' && $name[1] != '-') { // continue previous ingredient line. $contLastLine = true; trim($name); $name = trim(preg_replace("/^-+/", " ", $name)); // remove all leading "-" chars $this->currentIngredient->name = htmlspecialchars($name, ENT_QUOTES); // print "readIngredientLine: Extra matches: $name <br />\n"; } else { // just an ordinary ingredient line $this->currentIngredient->quantity = $amount; $this->setUnit($measurement); $this->currentIngredient->name = htmlspecialchars($name, ENT_QUOTES); // print "readIngredientLine: Ingredient: $name <br />\n"; } return $contLastLine; }
/** @return Table */ function toTable() { $zcoeffs = array(); foreach ($this->function->getSet() as $var => $coeff) { $zcoeffs[$var] = $coeff->multiply(-1); } $z = new ValueFunc($zcoeffs, 0); $z2b = Fraction::create(0); $z2coeffs = array(); foreach ($this->restrictions as $idx => $r) { foreach ($r->getSet() as $var => $coeff) { if (strncmp($var, 'y', 1) === 0 && $coeff->isEqualTo(1)) { foreach ($r->getSet() as $v => $c) { !isset($z2coeffs[$v]) && ($z2coeffs[$v] = Fraction::create(0)); strncmp($v, 'y', 1) !== 0 && ($z2coeffs[$v] = $z2coeffs[$v]->subtract($c)); } $z2b = $z2b->subtract($r->getLimit()); } } } $z2 = count($z2coeffs) ? new ValueFunc($z2coeffs, $z2b) : NULL; $table = new Table($z, $z2); foreach ($this->basismap as $var => $idx) { $table->addRow(new TableRow($var, $this->restrictions[$idx]->getSet(), $this->restrictions[$idx]->getLimit())); } return $table; }
?> </b></a> ( <?php if ($v['related_required'] == $DB_LINK->false) { echo '<a href="./index.php?m=lists&a=current&mode=add&recipe_selected_0=yes&recipe_id_0=' . $k . '&recipe_scale_0=' . $recipe_scale . '">' . $LangUI->_('Add to shopping list') . '</a>'; } else { echo $LangUI->_('Required'); } ?> ) : <?php if ($related_ings[$k] != NULL) { foreach ($related_ings[$k] as $i) { // prints out a decimal if that is what we want echo "<br />"; $fraction = new Fraction($i['map_quantity'] * $scale_by); echo $fraction->toString(); if ($i['unit_desc'] != $LangUI->_('Unit')) { echo ' ' . $i['unit_desc'] . $LangUI->_('(s)'); } echo ' ' . $i['map_qualifier']; echo ' ' . $i['ingredient_name']; } } ?> </td> <td width="50%" valign="top"> <b><?php echo $LangUI->_('Directions'); ?> :</b><br />
//the sum of two fractions public static function sumfract($c, $d, $i, $f) { $sumfr = $c / $d + $i / $f; echo $c . "/" . $d . " + " . $i . "/" . $f . " = " . number_format($sumfr, 2, ',', '') . '<br/>'; } //Subtract two fractions public static function subfract($c, $d, $i, $f) { $subfr = $c / $d - $i / $f; echo $c . "/" . $d . " - " . $i . "/" . $f . " = " . number_format($subfr, 2, ',', '') . '<br/>'; } //divide two fractions public static function devfract($c, $d, $i, $f) { $df = $c / $d / ($i / $f); echo $c . "/" . $d . " / " . $i . "/" . $f . " = " . number_format($df, 2, ',', '') . '<br/>'; } //multiplication of two fractions public static function multfract($c, $d, $i, $f) { $mf = $c / $d * ($i / $f); echo $c . "/" . $d . " * " . $i . "/" . $f . " = " . number_format($mf, 2, ',', '') . '<br/>'; } } $ttt = new Fraction(55, 0); Fraction::sumfract(23456, 8, 56788, 167); Fraction::subfract(1, 2, 4, 10); Fraction::devfract(1, 2, 5, 500); Fraction::multfract(30, 5, 3, 2);