Use a math library other than moontoast/math #109
Comments
Here's an initial, stubbed-out approach to a ramsey/math library: https://github.com/ramsey/math Again, I'm not necessarily of the mindset that the math library must be my own. These are just some thoughts I had on how a math library should work. |
@ramsey maybe it's worth using Also I think it would be better to have an ability to set custom math library via FeatureSet and provide "default" |
I'm still giving this a lot of thought. Ideally, ramsey/uuid wouldn't need to know about any external number library at all. I would love to return an unsigned 128-bit integer (or at least 2 unsigned 64-bit integers) and let the caller handle the number by passing it to their preferred library. To do this, I would need to return the number in string format, since PHP can't handle unsigned 64-bit integers, much less unsigned 128-bit integers. I can do this using bcmath or gmp, but that requires a system dependency, and not everyone has those installed. Likewise, I could use moontoast/math to create this string and return it (as just a string), but again, moontoast/math depends on bcmath. If there were a pure PHP polyfill for bcmath or gmp, then I could use that to perform the calculations and return a string number. Then, the caller can use whatever math library they wish. This is my preferred approach, but I have yet to find a pure PHP polyfill for bcmath or gmp. phpseclib does provide a pure PHP implementation, but it is far more than I need, and I prefer that the polyfill provide the bcmath or gmp functions rather than an abstraction around them. |
After looking closer at brick/math, I think it's the solution I want. They use gmp or bcmath, if available, falling back to their own pure PHP big number calculator, if not. However, I don't want to return brick/math objects, since I don't want external code to depend on it. So, I'll use brick/math internally and return string representations of integers. Another option, for better type-safety, is to provide a |
I stumbled into this issue by co-incidence since I found that moontoast has not received any update recently. Anyway that is not a big issue. @ramsey How about creating interface for Integer and wrap brick/math to return that interface. In that way we do not have to re-write brick/math and we will get type-safety as well. If you guys are ok with it. I can create PR for it. |
@nicholasnet That sounds great to me. Go for it! |
I did some investigation it seems like all we need is these methods in interface for our use case.
However, there some significant difference between
So, my question is which implementation we need to choose I am guessing BigDecimal but we need to confirm. Also considering this piece of code that we have currently in
What will be the
We will need to nail down this wrinkle first because no matter which Math library we choose we need to make sure that existing implementation does not break and it is correct. |
I don't think the As for how to handle the rounding with Brick, I can't answer that right now. I'll try to do some investigation to see how it works. |
|
I am the author of moontoast/math. I'm currently working on a replacement solution for the |
D'oh! Apologies |
No worries! |
@nicholasnet It turns out the proper rounding should have been "down" instead of "half down." The rounding method I'm using in I'm using brick/math in the uuid/src/Converter/Time/GenericTimeConverter.php Lines 81 to 99 in 85dae9e This will change a little before I release it, since I want to support microseconds in the |
Closing, since this has been addressed in the |
A math library is not required for this library unless users want to operate on the UUID as a 128-bit integer. For this purpose, the library has suggested moontoast/math as an optional dependency, and internally, this is what ramsey/uuid uses, if it is present. However, moontoast/math is no longer maintained.
I will either create a ramsey/math library that allows the use of different adapters for working with large numbers, or I want to find an existing math library that provides similar functionality.
The text was updated successfully, but these errors were encountered: