Skip to content

Implement direct conversion functions - #1227

Closed
tmilnthorp wants to merge 1 commit into
angularsen:masterfrom
tmilnthorp:tmilntho/directconv
Closed

Implement direct conversion functions#1227
tmilnthorp wants to merge 1 commit into
angularsen:masterfrom
tmilnthorp:tmilntho/directconv

Conversation

@tmilnthorp

Copy link
Copy Markdown
Collaborator

Implementing direct conversion functions would help reduce error. Currently, to go from ounces to pounds, we go:
ounces -> kilograms -> pounds

This shows an example of going directly from ounces -> pounds.

Long term, we could move all FromUnitToBaseFunc/FromBaseToUnitFunc into this conversions array.

Just a quick example to show what's on my mind.

@angularsenangularsen left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it 💖
It seems like a natural next step for the conversion functions you added in #588.

"BaseUnits": {
"M": "Ounce"
},
"Conversions": [

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DirectConversions maybe, to be more explicit that this is an optional thing?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@angularsen I think I would actually make it mandatory. I may take in stages, but maybe the first stage is the convert the current FromUnitToBaseFunc/FromBaseToUnitFunc to the Conversions?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh gotcha, yes I suppose we could refactor the existing definitions to this.

The upside is more explicit naming, the old naming is kinda confusing. Also gathering all conversion functions, both direct and via base, in one place might be better.

The downside is that it is maybe not as obvious that the conversions need to go via a "base" unit, but we could maybe fail codegen if any unit has missing conversions from/to base unit?

@angularsenangularsen added the pinned Issues that should not be auto-closed due to inactivity. label Jul 8, 2024
@angularsen

Copy link
Copy Markdown
Owner

I think we should close this PR now as superseded by #1544.

The motivating issue was precision loss from converting through an intermediate base unit, for example ounces -> kilograms -> pounds. Current master now uses fraction-backed QuantityValue and exact generated ConversionExpression metadata, so this specific example is already exact while the value stays as QuantityValue: 1 oz -> 1/16 lb and 1 lb -> 16 oz.

Generated quantities no longer own a TryToUnit switch; .As() and .ToUnit() now go through UnitConverter and UnitInfo conversion expressions. If we later find a remaining use case for direct same-quantity overrides, I think that should be designed in the UnitInfo/UnitConverter metadata layer with validation and clear precedence rules.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

discussenhancementhelp wantedpinnedIssues that should not be auto-closed due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@tmilnthorp@angularsen