summaryrefslogtreecommitdiffstats
path: root/Number.php
blob: 07ca222d6e05e970eb0b2cb531c6e34329f8a4ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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);
    }
}