-
Notifications
You must be signed in to change notification settings - Fork 477
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
Construct fraction and eliminate common terms when calculating conversion factor to increase accuracy #2108
base: master
Are you sure you want to change the base?
Construct fraction and eliminate common terms when calculating conversion factor to increase accuracy #2108
Conversation
Calculate factor by first constructing the scaling fraction, eliminating terms that appear in numerator and denominator and then finally evaluating the fraction.
CodSpeed Performance ReportMerging #2108 will not alter performanceComparing Summary
|
This looks good. I wonder if test_to_compact_fraction is related to to using fraction in a a non_int_type = float ureg |
Thank you for the encouragement.
If I instead use the Pint 0.24.4 method of calculating only using multiplication and setting negative sign for the exponent for denominator terms then the factor is evaluated as:
I have updated the PR to use multiplication only. |
I'll merge this before the next release. Leaving it open until then for others to comment on. |
pre-commit run --all-files
with no errorsThis change increases accuracy for unit conversions involving fractions which do not have real number representations.
Some conversions take long winding paths where premature evaluation of fractions lead to numerical errors.
Conversions using pint 0.24.4:
Using this PR:
Example
Take for instance conversion from
inch
tothou
. The factor is1000
so conversion should be lossless. However, the conversion process is actuallyinch
->yard
->meter
->yard
->inch
->thou
. The fraction is built one multiplication at a time and this incurs a loss of precision.The calculation is:
factor = 1 * (1/36) * (1/0.9144) * 0.9144 * 36 * 1000
This change first builds the fraction, then eliminates common terms in the fraction and finally it is evaluated numerically. The fraction is built and elimination performed resulting in
factor = 1000
.