Advertisement
zaq178miami

PHP Decimal class

Apr 16th, 2013
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | None | 0 0
  1. <?php
  2.  
  3. class DecimalException extends \Exception {}
  4.  
  5. // NOTE: math operators (neg, mul, div, sub, add) creates new object for result
  6. class Decimal implements \JsonSerializable
  7. {
  8.     const COMPARE_EQUAL   = 0;
  9.     const COMPARE_GREATER = 0.1;
  10.     const COMPARE_LESS    = -0.1;
  11.  
  12.     private static $zero;
  13.  
  14.     /**
  15.      * @var resource GMP Integer
  16.      */
  17.     private $value;
  18.  
  19.     /**
  20.      * @param int|string $value Value to be converted with GMP.
  21.      * @param int        $radix The radix
  22.      *
  23.      * @throws DecimalException If value could not benn converted to GMP resource
  24.      */
  25.     public function __construct($value, $radix = 10)
  26.     {
  27.         $this->value = @gmp_init($value, $radix);
  28.  
  29.         if (false === $this->value) {
  30.             throw new DecimalException("Wrong value format or type", $value);
  31.         }
  32.  
  33.     }
  34.  
  35.     public function isLessThan(Decimal $value)
  36.     {
  37.         return (gmp_cmp($this->value, $value->value) < 0);
  38.     }
  39.  
  40.     public function isEqualTo(Decimal $value)
  41.     {
  42.         return !gmp_cmp($this->value, $value->value); // == 0
  43.     }
  44.  
  45.     public function isGreaterThan(Decimal $value)
  46.     {
  47.         return (gmp_cmp($this->value, $value->value) > 0);
  48.     }
  49.  
  50.     public function isZero()
  51.     {
  52.         return !gmp_intval($this->value); // gmp_intval should be faster than comparison
  53.     }
  54.  
  55.     public function neg()
  56.     {
  57.         $_ret = new Decimal(0);
  58.  
  59.         $_ret->value = gmp_neg($this->value);
  60.  
  61.         return $_ret;
  62.     }
  63.  
  64.     public function abs()
  65.     {
  66.         $_ret = new Decimal(0);
  67.  
  68.         $_ret->value = gmp_abs($this->value);
  69.  
  70.         return $_ret;
  71.     }
  72.  
  73.     public function add(Decimal $value)
  74.     {
  75.         $_ret = new Decimal(0);
  76.  
  77.         $_ret->value = gmp_add($this->value, $value->value);
  78.  
  79.         return $_ret;
  80.     }
  81.  
  82.     public function sub(Decimal $value)
  83.     {
  84.         $_ret = new Decimal(0);
  85.  
  86.         $_ret->value = gmp_sub($this->value, $value->value);
  87.  
  88.         return $_ret;
  89.     }
  90.  
  91.  
  92.     public function mul(Decimal $value)
  93.     {
  94.         $_ret = new Decimal(0);
  95.  
  96.         $_ret->value = gmp_mul($this->value, $value->value);
  97.  
  98.         return $_ret;
  99.     }
  100.  
  101.     public function div(Decimal $value, $round = GMP_ROUND_ZERO)
  102.     {
  103.         $_ret = new Decimal(0);
  104.  
  105.         $_ret->value = gmp_div_q($this->value, $value->value, $round);
  106.  
  107.         return $_ret;
  108.     }
  109.  
  110. //    public function div_r(Decimal $value)
  111. //    {
  112. //        $_ret = new Decimal(0);
  113. //
  114. //        $_ret->value = gmp_div_r($this->value, $value->value);
  115. //
  116. //        return $_ret;
  117. //    }
  118.  
  119.     public function __toString()
  120.     {
  121.         return gmp_strval($this->value);
  122.     }
  123.  
  124.     public static function zero()
  125.     {
  126.         if (!self::$zero) {
  127.             self::$zero = new Decimal(0);
  128.         }
  129.  
  130.         return self::$zero;
  131.     }
  132.  
  133.     public function jsonSerialize()
  134.     {
  135.         return (string)$this;
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement