Skip to content

Commit ad40c95

Browse files
authored
Release/3.0.0 (#13)
1 parent 1f056e4 commit ad40c95

18 files changed

Lines changed: 319 additions & 225 deletions

README.md

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,38 @@ composer require tiny-blocks/math
2929
The library exposes some concrete implementations for arbitrary precision numbers. Concrete implementations implement
3030
the `BigNumber` interface, which provides the behaviors for the respective **BigNumbers**.
3131

32-
### Using the from method
32+
### Using the fromString method
3333

34-
With the `from` method, a new instance of type `BigNumber` is created from a valid numeric value. You can provide
35-
a `string` or `float` value.
34+
With the `fromString` method, a new instance of type `BigNumber` is created from a valid string numeric value.
3635

3736
```php
38-
BigDecimal::from(value: '10');
39-
BigDecimal::from(value: 10);
37+
BigDecimal::fromString(value: '10');
38+
BigDecimal::fromString(value: '-123.456');
4039
```
4140

42-
It is possible to set a `scale` for the object through the `from` method.
41+
It is possible to set a `scale` for the object through this method.
4342

4443
```php
45-
BigDecimal::from(value: '10', scale: 2);
44+
BigDecimal::fromString(value: '10', scale: 2);
4645
```
4746

48-
Floating point values instantiated from a `float` may not be safe, as they are imprecise by design and may result in a
49-
loss of precision. Always prefer to instantiate from a `string`, which supports an unlimited amount digits.
47+
Always prefer to instantiate from a string, which supports an unlimited number of digits and ensures no loss of
48+
precision.
49+
50+
### Using the fromFloat method
51+
52+
With the `fromFloat` method, a new instance of type `BigNumber` is created from a valid float value.
53+
54+
```php
55+
BigDecimal::fromFloat(value: 10.0);
56+
BigDecimal::fromFloat(value: -123.456);
57+
```
58+
59+
It is also possible to set a `scale` for the object through this method.
60+
61+
```php
62+
BigDecimal::fromFloat(value: 10.0, scale: 2);
63+
```
5064

5165
### Using the methods of mathematical operations
5266

@@ -55,8 +69,8 @@ loss of precision. Always prefer to instantiate from a `string`, which supports
5569
Performs an addition operation between this value and another value.
5670

5771
```php
58-
$augend = BigDecimal::from(value: 1);
59-
$addend = BigDecimal::from(value: '1');
72+
$augend = BigDecimal::fromString(value: '1');
73+
$addend = BigDecimal::fromFloat(value: 1.0);
6074

6175
$result = $augend->add(addend: $addend);
6276

@@ -68,8 +82,8 @@ $result->toString(); # 2
6882
Performs a subtraction operation between this value and another value.
6983

7084
```php
71-
$minuend = BigDecimal::from(value: 1);
72-
$subtrahend = BigDecimal::from(value: '1');
85+
$minuend = BigDecimal::fromString(value: '1');
86+
$subtrahend = BigDecimal::fromFloat(value: 1.0);
7387

7488
$result = $minuend->subtract(subtrahend: $subtrahend);
7589

@@ -81,8 +95,8 @@ $result->toString(); # 0
8195
Performs a multiplication operation between this value and another value.
8296

8397
```php
84-
$multiplicand = BigDecimal::from(value: 1);
85-
$multiplier = BigDecimal::from(value: '1');
98+
$multiplicand = BigDecimal::fromString(value: '1');
99+
$multiplier = BigDecimal::fromFloat(value: 1.0);
86100

87101
$result = $multiplicand->multiply(multiplier: $multiplier);
88102

@@ -94,8 +108,8 @@ $result->toString(); # 1
94108
Performs a division operation between this value and another value.
95109

96110
```php
97-
$dividend = BigDecimal::from(value: 1);
98-
$divisor = BigDecimal::from(value: '1');
111+
$dividend = BigDecimal::fromString(value: '1');
112+
$divisor = BigDecimal::fromFloat(value: 1.0);
99113

100114
$result = $dividend->divide(divisor: $divisor);
101115

@@ -112,18 +126,18 @@ rounding occurs:
112126
- `HALF_UP`: Round number away from zero when halfway.
113127

114128
```php
115-
$value = BigDecimal::from(value: 0.9950, scale: 2);
116-
129+
$value = BigDecimal::fromFloat(value: 0.9950, scale: 2);
130+
117131
$result = $value->withRounding(mode: RoundingMode::HALF_UP);
118-
132+
119133
$result->toString(); # 1
120134
```
121135

122136
- `HALF_DOWN`: Round number to zero when halfway.
123137

124138
```php
125-
$value = BigDecimal::from(value: 0.9950, scale: 2);
126-
139+
$value = BigDecimal::fromFloat(value: 0.9950, scale: 2);
140+
127141
$result = $value->withRounding(mode: RoundingMode::HALF_DOWN);
128142

129143
$result->toString(); # 0.99
@@ -132,20 +146,20 @@ rounding occurs:
132146
- `HALF_EVEN`: Round number to the nearest even value when halfway.
133147

134148
```php
135-
$value = BigDecimal::from(value: 0.9950, scale: 2);
136-
149+
$value = BigDecimal::fromFloat(value: 0.9950, scale: 2);
150+
137151
$result = $value->withRounding(mode: RoundingMode::HALF_EVEN);
138-
152+
139153
$result->toString(); # 1
140154
```
141155

142156
- `HALF_ODD`: Round number to the nearest odd value when halfway.
143157

144158
```php
145-
$value = BigDecimal::from(value: 0.9950, scale: 2);
146-
159+
$value = BigDecimal::fromFloat(value: 0.9950, scale: 2);
160+
147161
$result = $value->withRounding(mode: RoundingMode::HALF_ODD);
148-
162+
149163
$result->toString(); # 0.99
150164
```
151165

@@ -154,7 +168,7 @@ rounding occurs:
154168
Sometimes it is necessary to convert a value to negative, in these cases you can use the `negate` method.
155169

156170
```php
157-
$value = BigDecimal::from(value: 1);
171+
$value = BigDecimal::fromFloat(value: 1);
158172

159173
$result = $value->negate();
160174

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
},
4545
"require": {
4646
"php": "^8.2",
47-
"tiny-blocks/value-object": "^2",
4847
"ext-bcmath": "*"
4948
},
5049
"require-dev": {

src/BigDecimal.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44

55
namespace TinyBlocks\Math;
66

7-
use TinyBlocks\Math\Internal\BigNumberAdapter;
7+
use TinyBlocks\Math\Internal\BigNumberBehavior;
88
use TinyBlocks\Math\Internal\Number;
99
use TinyBlocks\Math\Internal\Scale;
10-
use TinyBlocks\Vo\ValueObject;
11-
use TinyBlocks\Vo\ValueObjectAdapter;
1210

13-
final class BigDecimal extends BigNumberAdapter implements BigNumber, ValueObject
11+
final class BigDecimal extends BigNumberBehavior implements BigNumber
1412
{
15-
use ValueObjectAdapter;
13+
public static function fromFloat(float $value, ?int $scale = BigNumber::AUTOMATIC_SCALE): BigNumber
14+
{
15+
$scale = Scale::from(value: $scale);
16+
$number = Number::from(value: $value);
17+
18+
return new BigDecimal(number: $number, scale: $scale);
19+
}
1620

17-
public static function from(float|string $value, ?int $scale = BigNumber::AUTOMATIC_SCALE): BigDecimal
21+
public static function fromString(string $value, ?int $scale = BigNumber::AUTOMATIC_SCALE): BigNumber
1822
{
19-
$scale = new Scale(value: $scale);
23+
$scale = Scale::from(value: $scale);
2024
$number = Number::from(value: $value);
2125

2226
return new BigDecimal(number: $number, scale: $scale);
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use TinyBlocks\Math\Internal\Operations\Rounding\Rounder;
1313
use TinyBlocks\Math\RoundingMode;
1414

15-
abstract class BigNumberAdapter
15+
abstract class BigNumberBehavior implements BigNumber
1616
{
1717
private MathOperations $mathOperations;
1818

@@ -23,27 +23,29 @@ protected function __construct(protected readonly Number $number, protected read
2323
$this->mathOperations = $operationsFactory->build();
2424
}
2525

26-
abstract public static function from(float|string $value, ?int $scale = BigNumber::AUTOMATIC_SCALE): BigNumber;
26+
abstract public static function fromFloat(float $value, ?int $scale = BigNumber::AUTOMATIC_SCALE): BigNumber;
27+
28+
abstract public static function fromString(string $value, ?int $scale = BigNumber::AUTOMATIC_SCALE): BigNumber;
2729

2830
public function add(BigNumber $addend): BigNumber
2931
{
3032
$result = $this->mathOperations->add(augend: $this, addend: $addend);
3133

32-
return $this::from(value: $result->number->value, scale: $result->scale->value);
34+
return static::fromString(value: $result->number->value, scale: $result->scale->value);
3335
}
3436

3537
public function subtract(BigNumber $subtrahend): BigNumber
3638
{
3739
$result = $this->mathOperations->subtract(minuend: $this, subtrahend: $subtrahend);
3840

39-
return $this::from(value: $result->number->value, scale: $result->scale->value);
41+
return static::fromString(value: $result->number->value, scale: $result->scale->value);
4042
}
4143

4244
public function multiply(BigNumber $multiplier): BigNumber
4345
{
4446
$result = $this->mathOperations->multiply(multiplicand: $this, multiplier: $multiplier);
4547

46-
return $this::from(value: $result->number->value, scale: $result->scale->value);
48+
return static::fromString(value: $result->number->value, scale: $result->scale->value);
4749
}
4850

4951
public function divide(BigNumber $divisor): BigNumber
@@ -54,27 +56,27 @@ public function divide(BigNumber $divisor): BigNumber
5456

5557
$result = $this->mathOperations->divide(dividend: $this, divisor: $divisor);
5658

57-
return $this::from(value: $result->number->value, scale: $result->scale->value);
59+
return static::fromString(value: $result->number->value, scale: $result->scale->value);
5860
}
5961

6062
public function withRounding(RoundingMode $mode): BigNumber
6163
{
6264
$rounder = new Rounder(mode: $mode, bigNumber: $this);
6365
$rounded = $rounder->round();
6466

65-
return $this::from(value: $rounded->value, scale: $this->scale->value);
67+
return static::fromString(value: $rounded->value, scale: $this->scale->value);
6668
}
6769

6870
public function withScale(int $scale): BigNumber
6971
{
7072
$number = $this->scale->numberWithScale(number: $this->number, scale: $scale);
7173

72-
return $this::from(value: $number->value, scale: $scale);
74+
return static::fromString(value: $number->value, scale: $scale);
7375
}
7476

7577
public function negate(): BigNumber
7678
{
77-
$multiplicand = $this->from(value: -1);
79+
$multiplicand = static::fromFloat(value: -1);
7880

7981
return $this->multiply(multiplier: $multiplicand);
8082
}

src/Internal/Operations/Adapters/Scales/Addition.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
public function __construct(private BigNumber $augend, private BigNumber $addend)
1616
{
17-
$this->augendScale = new Scale(value: $this->augend->getScale());
18-
$this->addendScale = new Scale(value: $this->addend->getScale());
17+
$this->augendScale = Scale::from(value: $this->augend->getScale());
18+
$this->addendScale = Scale::from(value: $this->addend->getScale());
1919
}
2020

2121
public function applyScale(): Scale

src/Internal/Operations/Adapters/Scales/Division.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
public function __construct(private BigNumber $dividend, private BigNumber $divisor)
1616
{
17-
$this->dividendScale = new Scale(value: $this->dividend->getScale());
18-
$this->divisorScale = new Scale(value: $this->divisor->getScale());
17+
$this->dividendScale = Scale::from(value: $this->dividend->getScale());
18+
$this->divisorScale = Scale::from(value: $this->divisor->getScale());
1919
}
2020

2121
public function applyScale(): Scale

src/Internal/Operations/Adapters/Scales/Multiplication.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
public function __construct(private BigNumber $multiplicand, private BigNumber $multiplier)
1616
{
17-
$this->multiplicandScale = new Scale(value: $this->multiplicand->getScale());
18-
$this->multiplierScale = new Scale(value: $this->multiplier->getScale());
17+
$this->multiplicandScale = Scale::from(value: $this->multiplicand->getScale());
18+
$this->multiplierScale = Scale::from(value: $this->multiplier->getScale());
1919
}
2020

2121
public function applyScale(): Scale

src/Internal/Operations/Adapters/Scales/Subtraction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
public function __construct(private BigNumber $minuend, private BigNumber $subtrahend)
1616
{
17-
$this->minuendScale = new Scale(value: $this->minuend->getScale());
18-
$this->subtrahendScale = new Scale(value: $this->subtrahend->getScale());
17+
$this->minuendScale = Scale::from(value: $this->minuend->getScale());
18+
$this->subtrahendScale = Scale::from(value: $this->subtrahend->getScale());
1919
}
2020

2121
public function applyScale(): Scale

src/Internal/Operations/MathOperations.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,38 @@
1010
interface MathOperations
1111
{
1212
/**
13-
* @param BigNumber $augend
14-
* @param BigNumber $addend
15-
* @return Result
13+
* Performs the addition of two BigNumber instances.
14+
*
15+
* @param BigNumber $augend The number to which another number (addend) will be added.
16+
* @param BigNumber $addend The number to be added to the augend.
17+
* @return Result The result of adding augend and addend, encapsulated in a Result object.
1618
*/
1719
public function add(BigNumber $augend, BigNumber $addend): Result;
1820

1921
/**
20-
* @param BigNumber $minuend
21-
* @param BigNumber $subtrahend
22-
* @return Result
22+
* Performs the subtraction of one BigNumber instance from another.
23+
*
24+
* @param BigNumber $minuend The number from which another number (subtrahend) will be subtracted.
25+
* @param BigNumber $subtrahend The number to be subtracted from the minuend.
26+
* @return Result The result of subtracting subtrahend from minuend, encapsulated in a Result object.
2327
*/
2428
public function subtract(BigNumber $minuend, BigNumber $subtrahend): Result;
2529

2630
/**
27-
* @param BigNumber $multiplicand
28-
* @param BigNumber $multiplier
29-
* @return Result
31+
* Performs the multiplication of two BigNumber instances.
32+
*
33+
* @param BigNumber $multiplicand The number to be multiplied.
34+
* @param BigNumber $multiplier The number by which the multiplicand will be multiplied.
35+
* @return Result The result of multiplying multiplicand and multiplier, encapsulated in a Result object.
3036
*/
3137
public function multiply(BigNumber $multiplicand, BigNumber $multiplier): Result;
3238

3339
/**
34-
* @param BigNumber $dividend
35-
* @param BigNumber $divisor
36-
* @return Result
40+
* Performs the division of one BigNumber instance by another.
41+
*
42+
* @param BigNumber $dividend The number to be divided.
43+
* @param BigNumber $divisor The number by which the dividend will be divided.
44+
* @return Result The result of dividing dividend by divisor, encapsulated in a Result object.
3745
*/
3846
public function divide(BigNumber $dividend, BigNumber $divisor): Result;
3947
}

src/Internal/Scale.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ final class Scale
1111
{
1212
private const MINIMUM = 0;
1313
private const MAXIMUM = 2147483647;
14+
private const ZERO_DECIMAL_PLACE = 0;
1415

15-
public function __construct(public readonly ?int $value)
16+
private function __construct(public readonly ?int $value)
1617
{
1718
if (!$this->hasAutomaticScale() && ($this->value < self::MINIMUM || $this->value > self::MAXIMUM)) {
1819
throw new InvalidScale(value: $this->value, minimum: self::MINIMUM, maximum: self::MAXIMUM);
1920
}
2021
}
2122

23+
public static function from(?int $value): Scale
24+
{
25+
return new Scale(value: $value);
26+
}
27+
2228
public function scaleOf(string $value): Scale
2329
{
2430
$number = Number::from(value: $value);
@@ -41,7 +47,7 @@ public function numberWithScale(Number $number, int $scale): Number
4147
}
4248

4349
$decimal = $result[0];
44-
$decimalPlaces = substr($result[1], 0, $scale);
50+
$decimalPlaces = substr($result[1], self::ZERO_DECIMAL_PLACE, $scale);
4551

4652
$template = '%s.%s';
4753
$value = sprintf($template, $decimal, $decimalPlaces);

0 commit comments

Comments
 (0)