Skip to content

Unit-consistent conversion functions, starting with 'to_deg' and 'from_deg' - #4829

Open
henrikt-ma wants to merge 1 commit into
modelica:masterfrom
henrikt-ma:unit-consistent-conversions
Open

henrikt-ma wants to merge 1 commit into
modelica:masterfrom
henrikt-ma:unit-consistent-conversions

Conversation

@henrikt-ma

Copy link
Copy Markdown
Contributor

The unit conversion functions in Modelica.Units.Conversions are a poor man's solution to something that should be a language responsibility. For now, we have to live with them, but all the conversion functions suffer from not being expressed with consistent use of units.

While Modelica 3.7 didn't introduce operators for safe unit conversion, it did introduce implicit unit conversion for unitful literals, which allows us to solve two problems at once:

  • Make the conversion functions unit consistent.
  • Express the conversions in a way that allows the unit machinery to detect incorrect conversions.

I know that some people find the simple solution hard to understand, but it would also be possible to combine old-style error-prone conversion factors with the safety of implicit conversion by the use of an assert which can be checked once at translation time:

protected
  constant Real unitConversionFactor(unit = "deg/rad") = 1'1';
  constant Real unitConversionFactorCheck(unit = "deg/rad") = 57.30; // Avoiding the question whether pi should carry unit.
algorithm
  assert(abs(unitConversionFactor - unitConversionFactorCheck) < 10 * Modelica.Constants.eps * abs(unitConversionFactor), "Wrong conversion factor.");

Note that the question of how to express as 180/π is orthogonal to the idea of using constant asserts to make more people feel safe.

Personally, I'd prefer to not express this sort of lack of faith in the unit machineries, but it's a perfect topic for a MAP-LIB discussion.

I suggest that we concentrate on to_deg and from_deg in this PR, and then do the other unit conversion functions similarly in a follow-up PR once the design pattern is clear.

@henrikt-ma henrikt-ma added L: Units Issue addresses Modelica.Units requires Modelica 3.7 Issue that requires Modelica Language Specification 3.7 labels Oct 16, 2026
@beutlich
beutlich force-pushed the unit-consistent-conversions branch from 3e4ace3 to 26baa91 Compare October 20, 2026 20:42

This branch has not been deployed

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

Labels

L: Units Issue addresses Modelica.Units requires Modelica 3.7 Issue that requires Modelica Language Specification 3.7

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant