@@ -29,24 +29,38 @@ composer require tiny-blocks/math
2929The library exposes some concrete implementations for arbitrary precision numbers. Concrete implementations implement
3030the ` 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
5569Performs 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
6882Performs 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
8195Performs 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
94108Performs 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:
154168Sometimes 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
0 commit comments