Ejemplo n.º 1
0
 /**
  * This method returns the result of adding the specified value to this object's value.
  *
  * @access public
  * @static
  * @param IRatio\Type $x                                    the left operand
  * @param IRatio\Type $y                                    the right operand
  * @return IRatio\Type                                      the result
  */
 public static function add(IRatio\Type $x, IRatio\Type $y) : IRatio\Type
 {
     if (IInt32\Module::signum($y->numerator())->unbox() == 0) {
         return $x;
     }
     if (IInt32\Module::signum($x->numerator())->unbox() == 0) {
         return $y;
     }
     if (IInt32\Module::eq($x->denominator(), $y->denominator())->unbox()) {
         return IRatio\Type::box(IInt32\Module::add($x->numerator(), $y->numerator()), $x->denominator());
     }
     return IRatio\Type::make(IInt32\Module::add(IInt32\Module::multiply($x->numerator(), $y->denominator()), IInt32\Module::multiply($y->numerator(), $x->denominator())), IInt32\Module::multiply($x->denominator(), $y->denominator()));
 }
Ejemplo n.º 2
0
 /**
  * This method tests the "signum" method.
  *
  * @dataProvider data_signum
  */
 public function test_signum(array $provided, array $expected)
 {
     $p0 = IInt32\Module::signum(IInt32\Type::box($provided[0]), IInt32\Type::box($provided[1]));
     $e0 = $expected[0];
     $this->assertInstanceOf('\\Saber\\Data\\ITrit\\Type', $p0);
     $this->assertSame($e0, $p0->unbox());
 }
Ejemplo n.º 3
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);
 }