Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call simplify before toFloat conversion #73

Merged
merged 2 commits into from
Aug 10, 2022

Conversation

olsavmic
Copy link
Contributor

@olsavmic olsavmic commented Aug 4, 2022

Calling $rationalNumber->toFloat() may result in a huge loss of precision due to the limits of floating point representation which can be prevented (not every time, but in a lot of real-world cases) by calling ->simplified() first.

Such loss of precision caused by a very large nominator or denominator may be actually (and unexpectedly) way larger than if regular 64bit floating numbers were used for all math operations instead (assuming some real-world numeric range). Even with regular numeric range, float overflow may easily occur.

We ran into this issue in production quite a few times in the legacy parts where we did not migrate yet to using BigRational and we perform the float conversion.

Therefore I'd like to suggest calling ->simplify every time before ->toFloat() conversion. I understand that the operation does not come for free, I'd like to hear your opinion :)

Thank you for this otherwise excellent library!

@BenMorel
Copy link
Member

BenMorel commented Aug 4, 2022

Hi, I think this makes a lot of sense! Could you please add a test that would fail without simplify() and succeed with it?

@olsavmic
Copy link
Contributor Author

olsavmic commented Aug 5, 2022

Hi, I added two test cases showing the issue. Seems like I was mistaken with the "Such loss of precision caused by a very large nominator or denominator may be actually (and unexpectedly) way larger..." part (and the error is always within the scale of PHP_FLOAT_EPSILON) but the test case with float overflow shows a real-world scenario that can easily occur and can be prevented. :)

@BenMorel
Copy link
Member

BenMorel commented Aug 5, 2022

Thanks, could you please avoid rewriting the coding style as it makes reading the diff tedious. FYI we’re working on imposing a coding style on the project and while some or your changes are relevant, they’ll be applied in a separate step.

@olsavmic olsavmic force-pushed the simplify-before-to-float-conversion branch from 57eb2d9 to 17cd1a5 Compare August 7, 2022 09:59
@olsavmic olsavmic force-pushed the simplify-before-to-float-conversion branch from 17cd1a5 to f89baa7 Compare August 7, 2022 10:01
@olsavmic
Copy link
Contributor Author

olsavmic commented Aug 7, 2022

Of course, sorry for that!

tests/BigRationalTest.php Outdated Show resolved Hide resolved
@BenMorel BenMorel merged commit d54a395 into brick:master Aug 10, 2022
@BenMorel
Copy link
Member

Thank you! Released as 0.10.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants