/**
  * Finds the Fibonacci number closest to an given integer
  *
  * @param integer $num
  * @return mixed a Fibonacci number (integer) on success, PEAR_Error otherwise
  * @access public
  */
 function closestTo($num)
 {
     /*{{{*/
     if (!Math_IntegerOp::isMath_Integer($num)) {
         return PEAR::raiseError("Invalid parameter: not a Math_Integer object");
     }
     $n = Math_Fibonacci::_estimateN($num);
     $fib1 =& Math_Fibonacci::term($n);
     $cmp = Math_IntegerOp::compare($fib1, $num);
     if ($cmp == 0) {
         return $fib1;
     } elseif ($cmp == 1) {
         // overshoot, see n - 1
         $new_n = Math_IntegerOp::sub($n, new Math_Integer(1));
     } else {
         // undeshoot, try n + 1
         $new_n = Math_IntegerOp::add($n, new Math_Integer(1));
     }
     $fib2 = Math_Fibonacci::term($new_n);
     $d1 = Math_IntegerOp::abs(Math_IntegerOp::sub($fib1, $num));
     $d2 = Math_IntegerOp::abs(Math_IntegerOp::sub($fib2, $num));
     $cmp = Math_IntegerOp::compare($d1, $d2);
     if ($cmp == -1 || $cmp == 0) {
         return $fib1;
     } else {
         return $fib2;
     }
 }
    $i2 = new Math_Integer('11111');
}
$i3 = new Math_Integer(6);
echo '* Using lib: ' . MATH_INTLIB . "\n";
echo 'i1 = ' . $i1->toString() . "\n";
echo 'i2 = ' . $i2->toString() . "\n";
echo 'i3 = ' . $i3->toString() . "\n";
$res = Math_IntegerOp::add($i1, $i2);
echo 'i1 + i2 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::sub($i1, $i2);
echo 'i1 - i2 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::sub($i2, $i1);
echo 'i2 - i1 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::mul($i1, $i2);
echo 'i1 * i2 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::div($i1, $i3);
echo 'i1 / i3 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::mod($i2, $i3);
echo 'i1 % i3 = ' . $res->toString() . "\n";
$res = Math_IntegerOp::neg($i1);
echo 'neg(i1) = ' . $res->toString() . "\n";
echo 'sign(neg(i1)) = ' . Math_IntegerOp::sign($res) . "\n";
echo 'sign(neg(0)) = ' . Math_IntegerOp::sign(new Math_Integer(0)) . "\n";
echo 'sign(i2) = ' . Math_IntegerOp::sign($i2) . "\n";
echo 'compare(i1, i2) = ' . Math_IntegerOp::compare($i1, $i2) . "\n";
echo 'compare(i3, i3) = ' . Math_IntegerOp::compare($i3, $i3) . "\n";
echo 'compare(i2, i1) = ' . Math_IntegerOp::compare($i2, $i1) . "\n";
$res = Math_IntegerOp::abs(Math_IntegerOp::neg($i2));
echo 'abs(neg(i2)) = ' . $res->toString() . "\n";
// vim: ts=4:sw=4:et:
// vim6: fdl=1: