Beispiel #1
0
 /**
  * @test
  */
 public function check_idempotent_float_number_conversion()
 {
     $f0 = new FN(12.89);
     $this->assertTrue($f0->equals(12.89));
     $f = new FN($f0->__toString());
     $this->assertTrue($f->equals(12.89));
     foreach (range(1, 99) as $i) {
         $f = new FN($f->__toString());
         $this->assertTrue($f->equals(12.89));
     }
     $this->assertTrue($f->equals($f0));
 }
Beispiel #2
0
 /**
  * @param string|int|float|\Enimiste\Math\VO\FloatNumber $value
  *
  * @param int                                            $scale
  *
  * @return \Enimiste\Math\VO\FloatNumber
  */
 function as_float_number($value, $scale = 2)
 {
     if ($value instanceof \Enimiste\Math\VO\FloatNumber) {
         return $value->copy($scale);
     } else {
         if ($scale < 0) {
             throw new \RuntimeException('Scale should be >= 0');
         }
         if ($value instanceof \Enimiste\Math\VO\IntegerNumber) {
             $value = $value->__toString();
         }
         return new \Enimiste\Math\VO\FloatNumber($value, $scale);
     }
 }
Beispiel #3
0
 /**
  * Check if this number is less than the given value
  *
  * @param N $other
  *
  * @return mixed
  */
 protected function _lt(N $other)
 {
     if (!$other instanceof FloatNumber) {
         $other = new FloatNumber($other->__toString());
     }
     $scales = self::getScales($this, $other);
     $l = $this->copy($scales[1]);
     $r = $other->copy($scales[1]);
     $ls = preg_replace("#[^0-9\\-]#", '', $l->__toString());
     $rs = preg_replace("#[^0-9\\-]#", '', $r->__toString());
     return (new IntegerNumber($ls))->lt(new IntegerNumber($rs));
 }
Beispiel #4
0
 /**
  * Sub two Numbers
  * $l - $r
  *
  * @param VONumber $l
  * @param VONumber $r
  *
  * @return VONumber
  */
 public function sub(VONumber $l, VONumber $r)
 {
     if ($l instanceof IntegerNumber && $r instanceof IntegerNumber) {
         return new IntegerNumber(bcsub($l->__toString(), $r->__toString()));
     } else {
         $scale = FloatNumber::getScales($l, $r);
         $res = new FloatNumber(bcsub($l->__toString(), $r->__toString(), $scale[1]), $scale[0]);
         return $res;
     }
 }