/**
  * 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;
     }
 }
//define ('MATH_INTLIB', 'bcmath');
//define ('MATH_INTLIB', 'std');
include_once 'Math/IntegerOp.php';
if (MATH_INTLIB == 'gmp' || MATH_INTLIB == 'bcmath') {
    $i1 = new Math_Integer('333333333333333333333333');
    $i2 = new Math_Integer('111111111111111111111111');
} else {
    $i1 = new Math_Integer('33333');
    $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";