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"); } }
function Fraction_Getvalue4Show($idx, $marker, $val, $oc, $style_arr, $first_denom, $frac_prec) { $ret = Fraction::toFract($val); // convert with highest possible precision NOW $split_frac = FALSE; $redux = FALSE; $pos_l = 0; $pos_h = $oc; // span left hand AND right hand item! foreach ($style_arr as $st) { switch ($st) { default: 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 "redux": // the produced fraction MUST be reducible if ($idx >= $pos_l && $idx <= $pos_h) { $redux = TRUE; } break; case "split_frac": if ($idx >= $pos_l && $idx <= $pos_h) { $split_frac = TRUE; } break; } } if ($redux) { if ($ret->denom != $first_denom) { $m = $first_denom / $ret->denom; if (FltEquals((int) $m, $m, $frac_prec)) { $ret->denom *= $m; $ret->num *= $m; } } } if ($ret->denom == 1) { return sprintf("%s", $ret->num); } else { $iv = 0; if ($split_frac) { $iv = (int) $val; $ret->num -= $iv * $ret->denom; if ($iv < 0) { $ret->num = -$ret->num; } } if ($iv != 0) { if ($marker >= $oc && $oc > 1) { return sprintf("%s %s/%s (= %s/%s)", $iv, $ret->num, $ret->denom, $iv * $ret->denom + $ret->num, $ret->denom); } else { return sprintf("%s %s/%s", $iv, $ret->num, $ret->denom); } } else { return sprintf("%s/%s", $ret->num, $ret->denom); } } }