summaryrefslogtreecommitdiffstats
path: root/Number.php
diff options
context:
space:
mode:
authorRemi Collet <remi@remirepo.net>2020-02-04 09:37:24 +0100
committerRemi Collet <remi@remirepo.net>2020-02-04 09:37:24 +0100
commit09e48a05b98b94fd52dc2562089f67e8d1279298 (patch)
treea1ef9b67ca87f0f3d563a41961d82cb248092309 /Number.php
parent483932783561752f0add0693b8b221a2fd09246e (diff)
update to 2.0.0 (alpha)HEADmaster
raise dependency on PHP 7.2 fix undefined symbol using patch from https://github.com/php-decimal/ext-decimal/pull/34 disable ZTS build which segfaults, reported as https://github.com/php-decimal/ext-decimal/issues/35
Diffstat (limited to 'Number.php')
-rw-r--r--Number.php132
1 files changed, 132 insertions, 0 deletions
diff --git a/Number.php b/Number.php
new file mode 100644
index 0000000..07ca222
--- /dev/null
+++ b/Number.php
@@ -0,0 +1,132 @@
+<?php
+
+/**
+ *
+ */
+class Number extends \Decimal\Number
+{
+ protected $value;
+
+ private static function parse($number)
+ {
+ $value = $number instanceof self ? $number->value : $number;
+
+ if ($value === "INF") $value = INF;
+ else if ($value === "-INF") $value = -INF;
+ else if ($value === "NAN") $value = NAN;
+
+ return $value;
+ }
+
+ protected function __construct($value)
+ {
+ $this->value = self::parse($value);
+ }
+
+ public static function valueOf($value): \Decimal\Number
+ {
+ return new static($value);
+ }
+
+ public function add($other): \Decimal\Number
+ {
+ printf("%s\n", __METHOD__);
+
+ return new static($this->value + $this->parse($other));
+ }
+
+ public function sub($other): \Decimal\Number
+ {
+ printf("%s\n", __METHOD__);
+
+ return new static($this->value - $this->parse($other));
+ }
+
+ public function mul($other): \Decimal\Number
+ {
+ printf("%s\n", __METHOD__);
+
+ return new static($this->value * $this->parse($other));
+ }
+
+ public function div($other): \Decimal\Number
+ {
+ printf("%s\n", __METHOD__);
+
+ return new static($this->value / $this->parse($other));
+ }
+
+ public function pow($other): \Decimal\Number
+ {
+ printf("%s\n", __METHOD__);
+
+ return new static($this->value ** $this->parse($other));
+ }
+
+ public function mod($other): \Decimal\Number
+ {
+ printf("%s\n", __METHOD__);
+
+ return new static($this->value % $this->parse($other));
+ }
+
+ public function shiftl($places): \Decimal\Number
+ {
+ printf("%s\n", __METHOD__);
+
+ return new static($this->value * (10 ** $places));
+ }
+
+ public function shiftr($places): \Decimal\Number
+ {
+ printf("%s\n", __METHOD__);
+
+ return new static($this->value / (10 ** $places));
+ }
+
+ public function round(int $places = NULL, int $mode = NULL): \Decimal\Number
+ {
+ return new static($this->toDecimal(\Decimal\Decimal::MAX_PRECISION)->round($places, $mode)->toString());
+ }
+
+ public function toFixed(int $places = NULL, bool $commas = NULL, int $mode = NULL): string
+ {
+ return new static($this->toDecimal(\Decimal\Decimal::MAX_PRECISION)->toFixed($places, $commas, $mode));
+ }
+
+ public function toDecimal(int $precision): \Decimal\Decimal
+ {
+ printf("%s\n", __METHOD__);
+
+ return parent::toDecimal($precision);
+ }
+
+ public function toRational(): \Decimal\Rational
+ {
+ printf("%s\n", __METHOD__);
+
+ return parent::toRational();
+ }
+
+ public function toString(): string
+ {
+ return (string) $this->value;
+ }
+
+ public function toInt(): int
+ {
+ return (int) $this->value;
+ }
+
+ public function toFloat(): float
+ {
+ return (float) $this->value;
+ }
+
+ public function compareTo($other): int
+ {
+ printf("%s\n", __METHOD__);
+
+ return $this->value <=> self::parse($other);
+ }
+}