Example #1
0
 /**
  * This method returns a value as a boxed object.  A value is typically a PHP typed
  * primitive or object.  It is considered type-safe.
  *
  * @access public
  * @static
  * @param mixed $value                                      the value(s) to be boxed
  * @return IRatio\Type                                      the boxed object
  */
 public static function make($value) : IRatio\Type
 {
     $values = is_array($value) ? $value : func_get_args();
     $values = array_map(function ($value) {
         return $value instanceof IInt32\Type ? $value : IInt32\Type::make($value);
     }, $values);
     $denominator = $values[1];
     $signum = IInt32\Module::signum($denominator)->unbox();
     if ($signum == 0) {
         throw new Throwable\InvalidArgument\Exception('Unable to create ratio. Denominator must not be zero.');
     }
     $numerator = $values[0];
     if (IInt32\Module::signum($numerator)->unbox() == 0) {
         return IRatio\Type::zero();
     }
     if ($signum < 0) {
         $numerator = IInt32\Module::negate($numerator);
         $denominator = IInt32\Module::negate($denominator);
     }
     $gcd = IInt32\Module::gcd($numerator, $denominator);
     if (!IInt32\Module::eq($gcd, IInt32\Type::one())->unbox()) {
         $numerator = IInt32\Module::divide($numerator, $gcd);
         $denominator = IInt32\Module::divide($denominator, $gcd);
     }
     return new IRatio\Type($numerator, $denominator);
 }
Example #2
0
 /**
  * This method provides the data for testing the making of a boxed value.
  *
  * @return array
  */
 public function data_singleton()
 {
     $data = array(array(array(IRatio\Type::one()), array(IInt32\Type::one(), IInt32\Type::one())), array(array(IRatio\Type::zero()), array(IInt32\Type::zero(), IInt32\Type::one())), array(array(IRatio\Type::negative()), array(IInt32\Type::negative(), IInt32\Type::one())));
     return $data;
 }
Example #3
0
 /**
  * This method returns whether the operand is a negative number.
  *
  * @access public
  * @static
  * @param IRatio\Type $x                                    the object to be evaluated
  * @return IBool\Type                                       whether the operand is a negative
  *                                                          number
  */
 public static function isNegative(IRatio\Type $x) : IBool\Type
 {
     $a = IInt32\Module::isNegative($x->numerator())->unbox();
     $b = IInt32\Module::isNegative($x->denominator())->unbox();
     return IBool\Type::box(($a || $b) && $a != $b);
 }