Real::subDecimal()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Domain\System;
13
14
/**
15
 * Real Class.
16
 *
17
 * @author Karel Osorio Ramírez <[email protected]>
18
 */
19
class Real extends Number
20
{
21
    /**
22
     * @param float $value
23
     *
24
     * @return \Cubiche\Domain\System\Real
25
     */
26
    public static function fromNative($value)
27
    {
28
        return new static($value);
29
    }
30
31
    /**
32
     * @param float $value
33
     *
34
     * @throws \InvalidArgumentException
35
     */
36
    protected function __construct($value)
37
    {
38
        $validatedValue = \filter_var($value, FILTER_VALIDATE_FLOAT);
39
        if ($validatedValue !== false) {
40
            $this->value = $value;
41
        } elseif (\is_infinite($value)) {
42
            $this->value = $value;
43
        } else {
44
            throw new \InvalidArgumentException(sprintf(
45
                'Argument "%s" is invalid. Allowed types for argument are "float".',
46
                $value
47
            ));
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function toInteger(RoundingMode $roundingMode = null)
55
    {
56
        if ($roundingMode === null) {
57
            $roundingMode = RoundingMode::HALF_UP();
58
        }
59
60
        return Integer::fromNative(\round($this->toNative(), 0, $roundingMode->toNative()));
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function toReal()
67
    {
68
        return self::fromNative($this->value);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function toDecimal()
75
    {
76
        return Decimal::fromNative($this->value);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function isInfinite()
83
    {
84
        return \is_infinite((float) $this->value);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function isPositive()
91
    {
92
        return ((float) $this->value) > 0;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function isNegative()
99
    {
100
        return ((float) $this->value) < 0;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function isZero()
107
    {
108
        return ((float) $this->value) == 0;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    protected function invertedAdd(Number $x)
115
    {
116
        return $x->addReal($this);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function addInteger(Integer $x)
123
    {
124
        return $this->addReal($x->toReal());
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function addReal(Real $x)
131
    {
132
        return new self($this->toNative() + $x->toNative());
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function addDecimal(Decimal $x, $scale = null)
139
    {
140
        return $this->toDecimal()->addDecimal($x, $scale);
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    protected function invertedSub(Number $x)
147
    {
148
        return $x->subReal($this);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function subInteger(Integer $x)
155
    {
156
        return $this->subReal($x->toReal());
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function subReal(Real $x)
163
    {
164
        return new self($this->toNative() - $x->toNative());
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function subDecimal(Decimal $x, $scale = null)
171
    {
172
        return $this->toDecimal()->subDecimal($x, $scale);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    protected function invertedMult(Number $x)
179
    {
180
        return $x->multReal($this);
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function multInteger(Integer $x)
187
    {
188
        return $this->multReal($x->toReal());
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function multReal(Real $x)
195
    {
196
        return new self($this->toNative() * $x->toNative());
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202
    public function multDecimal(Decimal $x, $scale = null)
203
    {
204
        return $this->toDecimal()->multDecimal($x, $scale);
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210
    protected function invertedDiv(Number $x)
211
    {
212
        return $x->divReal($this);
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function divInteger(Integer $x)
219
    {
220
        return $this->divReal($x->toReal());
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226 View Code Duplication
    public function divReal(Real $x)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
    {
228
        $value = $this->divSpecialCases($x);
229
        if ($value !== null) {
230
            return $value;
231
        }
232
233
        return new self($this->toNative() / $x->toNative());
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239
    public function divDecimal(Decimal $x, $scale = null)
240
    {
241
        return $this->toDecimal()->divDecimal($x, $scale);
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247
    protected function invertedPow(Number $x)
248
    {
249
        return $x->powReal($this);
250
    }
251
252
    /**
253
     * @param int $x
254
     *
255
     * @return Number
256
     */
257
    public function powInteger(Integer $x)
258
    {
259
        return $this->powReal($x->toReal());
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265
    public function powReal(Real $x)
266
    {
267
        return self::fromNative(\pow($this->toNative(), $x->toNative()));
268
    }
269
270
    /**
271
     * {@inheritdoc}
272
     */
273
    public function powDecimal(Decimal $x, $scale = null)
274
    {
275
        return $this->powReal($x->toReal())->toDecimal();
276
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281
    public function sqrt($scale = null)
282
    {
283
        if ($scale === null) {
284
            return self::fromNative(\sqrt($this->toNative()));
285
        }
286
287
        return $this->toDecimal()->sqrt($scale);
288
    }
289
}
290