Re: [RFC] [Discussion] Support object type in BCMath
| From: | Jordan LeDoux | Date: | Thu, 04 Apr 2024 21:10:08 +0000 |
| Subject: | Re: [RFC] [Discussion] Support object type in BCMath | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-122943@lists.php.net to get a copy of this message | ||
On Thu, Apr 4, 2024 at 1:59 PM Barney Laurance <barney@redmagic.org..uk>
wrote:
> Hi again,
>
> On 27/03/2024 00:40, Saki Takamachi wrote:
>
> 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.
>
> Coming back to this point, I think these are basic features that people
> would expect to be there - I think I would find just slightly frustrating
> to start learning how to use a class like this and then
> find that it doesn't have these functions. Casting and calling
> intval or
> floatval all feel like slightly awkward workarounds that
> shouldn't be
> needed in a greenfield project. We know that the string
> inside the object is always a numeric string, so it should be easier to
> parse it as an int than to parse it as a date or a JSON document. Code
> doing the latter should stand out as odd looking.
>
>
>
The class cannot guarantee that it can return a value in the type you
request however, so the way that is handled would need to be decided. The
value can easily be outside of the range of an int. Should it return a
float silently in that case for toInt()? What if the value is beyond the
range of a float? That would be a very rare situation, as floats can
represent extremely large numbers (with very reduced accuracy), but I would
expect it to throw an exception if that happened. Ideally an exception that
I could catch and ignore, since I can almost surely deal with that error in
most situations.
What about a number that is so small that it can't fit in a float? Similar
situation, though I expect it would occur slightly more often than a number
being too large to fit in a float, even though it would also be rare.
I think these helper functions belong in the RFC, but they aren't quite
straightforward, which is what I think Saki was alluding to.
Jordan