# [RFC] [Discussion] Support object type in BCMath

- Thread: https://externals.io/message/122735
- Original archive: https://news-web.php.net/php.internals/122735
- Started by Saki Takamachi on 2024-03-24T13:13:37+00:00
- Messages: 100
- RFC: Support object type in BCMath (Implemented (PHP 8.4)) — https://externals.io/rfc/support_object_type_in_bcmath.md
- RFC: Correctly name the rounding mode and make it an Enum (Implemented (PHP 8.4)) — https://externals.io/rfc/correctly_name_the_rounding_mode_and_make_it_an_enum.md
- Page 1 of 4: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/122735.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #122735 — Saki Takamachi — 2024-03-24T13:13:37+00:00

URL: https://externals.io/message/122735#122735

Hi internals,

I want to start the discussion on the PHP RFC: Support object type in BCMath.

https://wiki.php.net/rfc/support_object_type_in_bcmath

Regards.

Saki

---

## #122741 — Barney Laurance — 2024-03-25T22:48:17+00:00

URL: https://externals.io/message/122735#122741 · In reply to: #122735

> I want to start the discussion on the PHP RFC: Support object type in BCMath.

I suggest renaming `setScale` to `withScale`. Although the docs will 
make clear that the object is immutable, `set` is associated with 
mutation and might be confusing. `with` is not as well known as a prefix 
but is associated with immutable objects. Also as with the value, any 
reason not to make the scale a pubic readonly property?

---

## #122742 — Barney Laurance — 2024-03-25T22:48:26+00:00

URL: https://externals.io/message/122735#122742 · In reply to: #122735

> I want to start the discussion on the PHP RFC: Support object type in BCMath.

I work on OLTP application using BCMath for money, and I would like to 
refactor to value objects (although we could also convert to using 
ints), so this is very relevant to me. Liking the RFC a lot. Is there 
any reason not to give the BcNum class a public readonly string `value` 
property? Would just save a few characters of typing to use value 
instead of getValue().

---

## #122745 — Barney Laurance — 2024-03-26T08:44:26+00:00

URL: https://externals.io/message/122735#122745 · In reply to: #122735

> I want to start the discussion on the PHP RFC: Support object type in BCMath.
>
> https://wiki.php.net/rfc/support_object_type_in_bcmath

One more suggestion - might it be worth adding a `format` function to 
the new BcNum class? This would be similar to the existing number_format 
function, but would avoid the need to lose precision by converting to 
float first.

---

## #122746 — Saki Takamachi — 2024-03-26T11:35:19+00:00

URL: https://externals.io/message/122735#122746 · In reply to: #122745

Hi Barney, thanks for the points and suggestions!

> Is there any reason not to give the BcNum class a public readonly string `value` property? Would just save a few characters of typing to use value instead of getValue().

> Also as with the value, any reason not to make the scale a pubic readonly property?

I had completely forgotten about the existence of read-only properties. That makes sense.


> I suggest renaming `setScale` to `withScale`. Although the docs will make clear that the object is immutable, `set` is associated with mutation and might be confusing. `with` is not as well known as a prefix but is associated with immutable objects.

Indeed, I felt uncomfortable using "set”. I didn't know that "with" was related to immutable.


**I immediately reflected the above two points in my RFC** :D


> One more suggestion - might it be worth adding a `format` function to the new BcNum class? This would be similar to the existing number_format function, but would avoid the need to lose precision by converting to float first.

I came up with the following code, is it close to what you intended?

```
$num = BcNum::fromNumberFormat(1.2345, 5);
$num->value; // 1.23450
```

Regards.

Saki

---

## #122747 — Barney Laurance — 2024-03-26T12:07:51+00:00

URL: https://externals.io/message/122735#122747 · In reply to: #122746

> **I immediately reflected the above two points in my RFC** :D

Thanks, looks good.

> > […]
>
> I came up with the following code, is it close to what you intended?
>
> ```
> $num = BcNum::fromNumberFormat(1.2345, 5);
> $num->value; // 1.23450
> ```

No, that's not quite what I meant - I meant more like the opposite:


```
$bcNum = new BcNum('1234567890123456789.23456789');
echo $bcNum->format(8, '.', ',') // 1,234,567,890,123,456,789.23456789
```


Maybe also worth providing a way to specify that all decimals should be 
printed, instead of just a fixed number of decimals.

---

## #122748 — Saki Takamachi — 2024-03-26T12:50:48+00:00

URL: https://externals.io/message/122735#122748 · In reply to: #122747

Hi Barney,

> No, that's not quite what I meant - I meant more like the opposite:
> 
> 
> ```
> $bcNum = new BcNum('1234567890123456789.23456789');
> echo $bcNum->format(8, '.', ',') // 1,234,567,890,123,456,789.23456789
> ```
> 
> 
> Maybe also worth providing a way to specify that all decimals should be printed, instead of just a fixed number of decimals.

Ah I see! It sounds exactly like `number_format`. Sure, this might make sense. To me, this seems worth supporting.


BTW,
```
$bcNum = new BcNum('1234567890123456789.23456789’);
```

When I saw this, I thought that the behavior when $scale is omitted is that in addition to the option of "using the global setting value", there is also the option of "counting the length of `$num` and storing all `$num`”. In other words, it does not use any global settings.

Which of these do you think is better?

Regards.

Saki

---

## #122750 — Aleksander Machniak — 2024-03-26T13:19:28+00:00

URL: https://externals.io/message/122735#122750 · In reply to: #122735

> Hi internals,
> 
> I want to start the discussion on the PHP RFC: Support object type in BCMath.
> 
> https://wiki.php.net/rfc/support_object_type_in_bcmath

Was BCMath\Number considered instead of BcNum?

ps. there's '2,111' in one place, but should be '2.111', I guess.

-- 
Aleksander Machniak
Kolab Groupware Developer        [https://kolab.org]
Roundcube Webmail Developer  [https://roundcube.net]
----------------------------------------------------
PGP: 19359DC1 # Blog: https://kolabian.wordpress.com

---

## #122751 — Saki Takamachi — 2024-03-26T13:35:13+00:00

URL: https://externals.io/message/122735#122751 · In reply to: #122750

Hi Aleksander,

> Was BCMath\Number considered instead of BcNum?

Yes, that was one of the candidates. However, as far as I know, there are no examples of PHP internal classes having namespaces.
Also, if use a namespace, the code will be written as `new Number()`, which is likely to conflict with existing code. In fact, if take a look at GitHub Code Search, you'll find 3.2k results.
https://github.com/search?type=code&auto_enroll=true&q=%22new+Number%28%22+language%3APHP+

This won't result in a BC Break, but it can be a bit difficult to use.


> ps. there's '2,111' in one place, but should be '2.111', I guess.

Oops, thank you. You have good eyes.

Regards.

Saki

---

## #122752 — Aleksander Machniak — 2024-03-26T13:47:37+00:00

URL: https://externals.io/message/122735#122752 · In reply to: #122751

> Hi Aleksander,
> 
> > […]
> 
> Yes, that was one of the candidates. However, as far as I know, there are no examples of PHP internal classes having namespaces.
> Also, if use a namespace, the code will be written as `new Number()`, which is likely to conflict with existing code. In fact, if take a look at GitHub Code Search, you'll find 3.2k results.
> https://github.com/search?type=code&auto_enroll=true&q=%22new+Number%28%22+language%3APHP+
> 
> This won't result in a BC Break, but it can be a bit difficult to use.

After reading https://wiki.php.net/rfc/namespaces_in_bundled_extensions 
again I see it is a perfect case to apply it. While it's not a must, I 
think we should go with BCMath/Number.

-- 
Aleksander Machniak
Kolab Groupware Developer        [https://kolab.org]
Roundcube Webmail Developer  [https://roundcube.net]
----------------------------------------------------
PGP: 19359DC1 # Blog: https://kolabian.wordpress.com

---

## #122753 — Saki Takamachi — 2024-03-26T14:03:58+00:00

URL: https://externals.io/message/122735#122753 · In reply to: #122752

Hi Aleksander,

> After reading https://wiki.php.net/rfc/namespaces_in_bundled_extensions again I see it is a perfect case to apply it. While it's not a must, I think we should go with BCMath/Number.

Thank you, I have read it now. Certainly in this case it makes sense to use "BcMath" as the namespace ("BcMath" is appropriate instead of "BCMath" as it must follow PHP naming conventions).

What concerns me is that the symbol "Number" is difficult to understand in the code. So, how about "BcMath\BcNum”?

Regards.

Saki

---

## #122754 — Larry Garfield — 2024-03-26T14:40:04+00:00

URL: https://externals.io/message/122735#122754 · In reply to: #122748

> Hi Barney,
>
> > […]
>
> Ah I see! It sounds exactly like `number_format`. Sure, this might make 
> sense. To me, this seems worth supporting.
>
>
> BTW,
> ```
> $bcNum = new BcNum('1234567890123456789.23456789’);
> ```
>
> When I saw this, I thought that the behavior when $scale is omitted is 
> that in addition to the option of "using the global setting value", 
> there is also the option of "counting the length of `$num` and storing 
> all `$num`”. In other words, it does not use any global settings.
>
> Which of these do you think is better?

Global mode settings are an anti-pattern in most cases.  Please avoid those whenever possible, as they lead to unpredictable behavior.

--Larry Garfield

---

## #122756 — Saki Takamachi — 2024-03-26T14:50:46+00:00

URL: https://externals.io/message/122735#122756 · In reply to: #122754

Hi Larry,

> Global mode settings are an anti-pattern in most cases.  Please avoid those whenever possible, as they lead to unpredictable behavior.

Yes, that's right.

BCMath has an existing global setting, so I was wondering if it was something I could ignore. But that means there's no reason to use global settings other than "they exist in existing implementations"…

Okay, regarding the existing global setting, I decided not to support it with BcNum.

Regards.

Saki

---

## #122757 — Saki Takamachi — 2024-03-26T15:15:46+00:00

URL: https://externals.io/message/122735#122757 · In reply to: #122756

Hi,

I wrote in my RFC that it does not support global settings.

Regards.

Saki

---

## #122758 — Derick Rethans — 2024-03-26T15:47:56+00:00

URL: https://externals.io/message/122735#122758 · In reply to: #122735

> Hi internals,
> 
> I want to start the discussion on the PHP RFC: Support object type in BCMath.
> 
> https://wiki.php.net/rfc/support_object_type_in_bcmath

I have some comments:

- You've picked as class name "BcNum". Following 
  our naming guidelines, that probably should be \BCMath\Num (or 
  \BC\Num, but that is less descriptive): 
  https://github.com/php/policies/blob/main/coding-standards-and-naming.rst#namespaces-in-extensions

  The reason it *should* have "BC" is that it comes from "Basic 
  Calculator" (https://www.php.net/manual/en/book.bc.php#118203)

- Should ->value rather be ->toString() ? ->value alone doesn't really 
  say much. I'm on the fence here though, as there is already 
  (internally) a ->__toString() method to make the (string) cast work.

- Would it make sense to have "floor" and "ceil" to also have a scale, 
  or precision? Or would developers instead have to use "round" in that 
  case?

- Which rounding modes are supported with "round", the same ones as the 
  normal `round()` function?

- In this example, what would $result->scale show? (Perhaps add that to 
  the example?):

	<?php
	$num = new BcNum('1.23', 2);
	$result = $num + '1.23456';
	$result->value; // '2.46456'
	$result->scale; // ??

- Exceptions

  The RFC does not mention which exceptions can be thrown. Is it just 
  the one? It might be beneficial to *do* have a new exception 
  hierarchy.



cheers,
Derick

-- 
https://derickrethans.nl | https://xdebug.org | https://dram.io

Author of Xdebug. Like it? Consider supporting me: https://xdebug.org/support

mastodon: @derickr@phpc.social @xdebug@phpc.social

---

## #122759 — Aleksander Machniak — 2024-03-26T16:31:08+00:00

URL: https://externals.io/message/122735#122759 · In reply to: #122735

> Hi internals,
> 
> I want to start the discussion on the PHP RFC: Support object type in BCMath.
> 
> https://wiki.php.net/rfc/support_object_type_in_bcmath

Here's another question.

1. Since we have withScale(), do we need to inherit the $scale argument 
from the functional API? Can't we derive it from the object the method 
is being invoked on?

So, instead, e.g.

public function add(BcNum|string|int $num, ?int $scale = null): BcNum {}
public function sqrt(?int $scale = null): BcNum {}

I'd suggest:

public function add(BcNum|string|int $num): BcNum {}
public function `sqrt()`: BcNum {}

but I have no clue about BCMath.

-- 
Aleksander Machniak
Kolab Groupware Developer        [https://kolab.org]
Roundcube Webmail Developer  [https://roundcube.net]
----------------------------------------------------
PGP: 19359DC1 # Blog: https://kolabian.wordpress.com

---

## #122762 — Barney Laurance — 2024-03-26T18:00:05+00:00

URL: https://externals.io/message/122735#122762 · In reply to: #122735

> I want to start the discussion on the PHP RFC: Support object type in 
> BCMath.


Do we also need `toFloat` and `toInt` functions? Seems like using 
explicit functions will be safer than casting.

For toInt I'd expect an exception if the value is outside the range of 
possible ints. For toFloat it might be nice to have a flag
argument to give the developer the choice of having it throw if the 
value is outside the range of floats or return `INF` or -INF,
or possibly the user should just check for infinite values themselves.

---

## #122766 — Saki Takamachi — 2024-03-27T00:03:24+00:00

URL: https://externals.io/message/122735#122766 · In reply to: #122758

Hi Derick,

> - You've picked as class name "BcNum". Following 
>  our naming guidelines, that probably should be \BCMath\Num (or 
>  \BC\Num, but that is less descriptive): 
>  https://github.com/php/policies/blob/main/coding-standards-and-naming.rst#namespaces-in-extensions
> 
>  The reason it *should* have "BC" is that it comes from "Basic 
>  Calculator" (https://www.php.net/manual/en/book.bc.php#118203)


I re-read the namespace RFC again. I also re-read the RFC regarding class naming conventions.
https://wiki.php.net/rfc/namespaces_in_bundled_extensions
https://wiki.php.net/rfc/class-naming

There's no need for the namespace to follow class naming conventions, but the acronym doesn't seem to need to be pascal-case anyway (I remembered it incorrectly). However, the RFC states that the extension's namespace must match the extension name, so it seems correct in this case for the namespace to be `BCMath`.

And indeed, looking at the example in the namespace RFC, `BCMath\Number` might be appropriate in this case (I think I was sleepy yesterday).

I changed `BcNum` to `BCMath\Number` in my RFC.


> - Should ->value rather be ->toString() ? ->value alone doesn't really 
>  say much. I'm on the fence here though, as there is already 
>  (internally) a ->__toString() method to make the (string) cast work.

What is the main difference between getting a read-only property with `->value` and getting the value using a method?


> - Would it make sense to have "floor" and "ceil" to also have a scale, 
>  or precision? Or would developers instead have to use "round" in that 
>  case?

> - Which rounding modes are supported with "round", the same ones as the 
>  normal `round()` function?

`bcfloor` and `bcceil` originally have no scale specification. This is because the result is always a string representing an integer value. And since the supported round-mode is the same as standard-round, `ROUND_FLOOR` and `ROUND_CEILING` are also supported.
Therefore, if want to obtain floor or ceil behavior with a specified scale, I recommend specifying the mode as round.


> - In this example, what would $result->scale show? (Perhaps add that to 
>  the example?):
> 
> <?php
> $num = new BcNum('1.23', 2);
> $result = $num + '1.23456';
> $result->value; // '2.46456'
> $result->scale; // ??

In this case, `$result->scale` will be `'5'`. I added this to the RFC.


> - Exceptions
> 
>  The RFC does not mention which exceptions can be thrown. Is it just 
>  the one? It might be beneficial to *do* have a new exception 
>  hierarchy.



As far as I know right now, following exceptions can be thrown:

- Value error when a string that is invalid as a number is used in a constructor, calculation method, or operation
- Divide by 0 error (include Modulo by zero)

I was thinking that it would be a bad idea to increase the number of classes without thinking, and was planning to use general exceptions, but would it be better to use dedicated exceptions?
By the way, generally when implementing such exceptions in userland, value errors and divide-by-zero errors are probably defined as separate classes, but should they be separated in this case?

Regards.

Saki

---

## #122767 — Saki Takamachi — 2024-03-27T00:36:52+00:00

URL: https://externals.io/message/122735#122767 · In reply to: #122759

Hi Aleksander,

> Here's another question.
> 
> 1. Since we have withScale(), do we need to inherit the $scale argument from the functional API? Can't we derive it from the object the method is being invoked on?
> 
> So, instead, e.g.
> 
> public function add(BcNum|string|int $num, ?int $scale = null): BcNum {}
> public function sqrt(?int $scale = null): BcNum {}
> 
> I'd suggest:
> 
> public function add(BcNum|string|int $num): BcNum {}
> public function `sqrt()`: BcNum {}
> 
> but I have no clue about BCMath.

Yeah, you're right. By using `withScale` before calculating, we can obtain results of arbitrary precision without using Scale during calculation.

The code in that case would look like this:
```
$num = new Number('1.23');
$num2 = new Number('4.56');

$numRescaled = $num->withScale(4);

$result = $numRescaled->add($num2);
$result->value; // '5.7900'
$result->scale; // 4
```

On the other hand, if  allow the calculation method to specify a scale, we can write it like this:
```
$num = new Number('1.23');
$num2 = new Number('4.56');

$result = $num->add($num2, 4);
$result->value; // '5.7900'
$result->scale; // 4
```

It's just one less line, but that's the only reason to support the `$scale` argument.

However, for calculations other than mul and div, the calculation results will always fit within the scale.
```
$num = new Number('1.23'); // scale 2
$num2 = new Number('1'); // scale 0

$num->add($num2); // '2.23', scale 2 is enough
$num->sub($num2); // '0.23', scale 2 is enough
$num->mod($num2); // '0.23', scale 2 is enough
```

On the other hand, for mul, div, and pow, the original `$num` scale may not be enough.
```
$num = new Number('1.23'); // scale 2
$num2 = new Number('1.1'); // scale 1

$num->mul($num2); // '1.353' be '1.35', scale 2 is not enough
```

```
$num = new Number('1.23'); // scale 2
$num2 = new Number('4'); // scale 0

$num->div($num2); // '0.3075' be '0.30', scale 2 is not enough
$num->pow($num2); // '2.28886641', be '2.28' scale 2 is not enough
```

For mul and pow, can calculate the required scale. However, for div, the calculation may never end, such as `0.33333....`, so it is not possible to calculate the scale to fit the result completely.

In cases like this, we thought that some users would want to easily specify the scale, so we created an easy-to-use signature.

However, it may not be necessary to specify scale for all calculation functions. Is it reasonable to specify only mul, div, pow, or only div?

Regards.

Saki

---

## #122768 — Saki Takamachi — 2024-03-27T00:38:00+00:00

URL: https://externals.io/message/122735#122768 · In reply to: #122766

Hi Derick,

I made one mistake.

> In this case, `$result->scale` will be `'5'`. I added this to the RFC.

It's `5`, not `'5’`.

Regards.

Saki

---

## #122769 — Saki Takamachi — 2024-03-27T00:40:45+00:00

URL: https://externals.io/message/122735#122769 · In reply to: #122762

Hi Barney,

> Do we also need `toFloat` and `toInt` functions? Seems like using explicit functions will be safer than casting.
> 
> For toInt I'd expect an exception if the value is outside the range of possible ints. For toFloat it might be nice to have a flag
> argument to give the developer the choice of having it throw if the value is outside the range of floats or return `INF` or -INF,
> or possibly the user should just check for infinite values themselves.

I was thinking about those features too. However, I'm concerned that proposing too many features will complicate the RFC and make it difficult to get it approved.

It might make sense to have a second vote on whether to implement those features.

Regards.

Saki

---

## #122771 — Aleksander Machniak — 2024-03-27T06:34:44+00:00

URL: https://externals.io/message/122735#122771 · In reply to: #122767

> On the other hand, if  allow the calculation method to specify a scale, we can write it like this:
> ```
> $num = new Number('1.23');
> $num2 = new Number('4.56');
> 
> $result = $num->add($num2, 4);
> $result->value; // '5.7900'
> $result->scale; // 4
> ```
> 
> It's just one less line, but that's the only reason to support the `$scale` argument.

If you write it as:

$result = $num->withScale(4)->add($num2);

it's not an extra line anymore. I also think that withScale() use will 
be rare, as we have the scale in constructor.

I think the intention is more clear here, and I think it applies to all 
cases you mentioned, including div or pow. If you know you need to 
change the scale just add ->withScale(X) before.

-- 
Aleksander Machniak
Kolab Groupware Developer        [https://kolab.org]
Roundcube Webmail Developer  [https://roundcube.net]
----------------------------------------------------
PGP: 19359DC1 # Blog: https://kolabian.wordpress.com

---

## #122772 — Aleksander Machniak — 2024-03-27T07:07:27+00:00

URL: https://externals.io/message/122735#122772 · In reply to: #122766

> > […]
> 
> In this case, `$result->scale` will be `'5'`. I added this to the RFC.

I'm not sure I like this. Maybe we should be more strict here and treat 
the $scale in constructor (and later withScale()) as the actual scale 
for all operations.

So, in the case above I'd expect ->scale to be 2, and ->value to be 
'2.46'. If I wanted $num to have a scale that may change, I'd not define 
it in the first place. Does that make sense?

ps. that also means withScale(null) should be possible.

-- 
Aleksander Machniak
Kolab Groupware Developer        [https://kolab.org]
Roundcube Webmail Developer  [https://roundcube.net]
----------------------------------------------------
PGP: 19359DC1 # Blog: https://kolabian.wordpress.com

---

## #122775 — Saki Takamachi — 2024-03-27T10:46:05+00:00

URL: https://externals.io/message/122735#122775 · In reply to: #122772

Hi Aleksander,

> If you write it as:
> 
> $result = $num->withScale(4)->add($num2);
> 
> it's not an extra line anymore. I also think that withScale() use will be rare, as we have the scale in constructor.
> 
> I think the intention is more clear here, and I think it applies to all cases you mentioned, including div or pow. If you know you need to change the scale just add ->withScale(X) before.

Ah, that's right. I wonder why I forgot about method chaining. Update RFC. If we miss something and $scale is needed, I can always revert the RFC.


> I'm not sure I like this. Maybe we should be more strict here and treat the $scale in constructor (and later withScale()) as the actual scale for all operations.
> 
> So, in the case above I'd expect ->scale to be 2, and ->value to be '2.46'. If I wanted $num to have a scale that may change, I'd not define it in the first place. Does that make sense?
> 
> ps. that also means withScale(null) should be possible.

I see, that may indeed make sense. Now here we have three choices about it. In fact, if want to behave like the current RFC, we can do it by calculating as `$num + new Number($str)`.
Yeah, I'll fix the RFC for this.

Regards.

Saki

---

## #122787 — Derick Rethans — 2024-03-28T12:00:46+00:00

URL: https://externals.io/message/122735#122787 · In reply to: #122766

> > […]
> 
> 
> I re-read the namespace RFC again. I also re-read the RFC regarding 
> class naming conventions. 
> https://wiki.php.net/rfc/namespaces_in_bundled_extensions 
> https://wiki.php.net/rfc/class-naming
> 
> There's no need for the namespace to follow class naming conventions, 
> but the acronym doesn't seem to need to be pascal-case anyway (I 
> remembered it incorrectly). However, the RFC states that the 
> extension's namespace must match the extension name, so it seems 
> correct in this case for the namespace to be `BCMath`.
> 
> And indeed, looking at the example in the namespace RFC, 
> `BCMath\Number` might be appropriate in this case (I think I was 
> sleepy yesterday).
> 
> I changed `BcNum` to `BCMath\Number` in my RFC.

That works fine as well. Especially as most people would add:

use BCMath\Number as Number;

and then just use "Number" everywhere.

> > […]
> 
> What is the main difference between getting a read-only property with 
> `->value` and getting the value using a method?

Feeling :-) Do we have precedence with other extension's objects 
perhaps already?

> > […]
> 
> > […]
> 
> `bcfloor` and `bcceil` originally have no scale specification. This is 
> because the result is always a string representing an integer value. 
> And since the supported round-mode is the same as standard-round, 
> `ROUND_FLOOR` and `ROUND_CEILING` are also supported. Therefore, if 
> want to obtain floor or ceil behavior with a specified scale, I 
> recommend specifying the mode as round.

OK. That makes sense.

> > […]
> 
> In this case, `$result->scale` will be `'5'`. I added this to the RFC.

Great. 

> > […]
> 
> 
> As far as I know right now, following exceptions can be thrown:
> 
> - Value error when a string that is invalid as a number is used in a 
>   constructor, calculation method, or operation
> - Divide by 0 error (include Modulo by zero)
> 
> I was thinking that it would be a bad idea to increase the number of 
> classes without thinking, and was planning to use general exceptions, 
> but would it be better to use dedicated exceptions?

It's what we did for the Date extension, and the Random extension, but 
in this case, it's probably not needed as you say.

> By the way, generally when implementing such exceptions in userland, 
> value errors and divide-by-zero errors are probably defined as 
> separate classes, but should they be separated in this case?

For that, yes. ValueErrors should be distinct from DivideByZeroError — I 
think we do have both of those already:

php -r 'echo 8/0;'

Fatal error: Uncaught DivisionByZeroError: Division by zero in Command 
line code on line 1

From the docs for ValueError:

"A ValueError is thrown when the type of an argument is correct but the 
value of it is incorrect."

From the docs for DivisionByZeroError:

" DivisionByZeroError is thrown when an attempt is made to divide a 
number by zero. "

Subclassing these for BCMath objects seems unnecessary therefore.

cheers,
Derick

-- 
https://derickrethans.nl | https://xdebug.org | https://dram.io

Author of Xdebug. Like it? Consider supporting me: https://xdebug.org/support

mastodon: @derickr@phpc.social @xdebug@phpc.social

---

Next page: https://externals.io/message/122735.md?page=2
