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

Most elegant way to convert a length ratio to radians? #684

Open
etsach opened this issue Mar 5, 2025 · 2 comments
Open

Most elegant way to convert a length ratio to radians? #684

etsach opened this issue Mar 5, 2025 · 2 comments

Comments

@etsach
Copy link

etsach commented Mar 5, 2025

I'm working on a project, where we often have to compute angles or solid angles from lengths or area and distances.
So we would need to write code like:

radian_t DisplacementAngle = Displacement / Distance;

We are dealing with small angles, think about dα = dL / R

I know that angles were introduced as a strongly typed unit with good reasons, so I will not debate over this here.
But is there a possible nice syntax to write this? Currently I would do this kind of ugly code:

radian_t DisplacementAngle = radian_t((Displacement / Distance).value());

I nothing is available built-in, I would maybe suggest adding this as a trigonometry function, as we are more or less looking for an operation similar to arcsin, except it's linear. We could for instance name it "arcangle" (random name, maybe not the best):

radian_t DisplacementAngle = math::arcangle( Displacement / Distance );

So in short : is there a nice way to write this already? What about introducing this as a function along trig functions? It feels weird that computing the arcsine angle has a nice syntax, but not a dumb arc angle calculation.

@chiphogg
Copy link
Collaborator

chiphogg commented Mar 5, 2025

radian_t looks like syntax for nholthaus units --- is that right?

Anyway, I just recently made a lengthy comment on an issue for that library. It's directly relevant here, so check it out for some good background info.

The short of it in your case is you want to use the fully general equation, $\theta = (d/r) \Theta_C$, where $\Theta_C$ is a mathematical constant whose value equals one radian. Notice how the equation is now dimensionally balanced, and you can freely use any units for your distances and angles: you're no longer locked into radians, because $\Theta_C$ can be expressed in any angular units.

Of course, in practice when it comes to the code, you would simply multiply by rad, and let the units work themselves out: the library will handle the safety. I am more familiar with Au syntax than mp-units, but I think this is how you do that here:

using mp_units::si::unit_symbols::rad;

quantity DisplacementAngle = Displacement / Distance * rad;

@mpusz
Copy link
Owner

mpusz commented Mar 5, 2025

Hi @etsach 😃

I know that angles were introduced as a strongly typed unit with good reasons, so I will not debate over this here.

This is actually not the case both for quantities and units:

  • ISQ defines angular_measure as arc_length / radius,
  • SI defines radian as metre / metre.

We need to do the same if we want to be compliant. Based on the above you can calculate angle as either:

quantity displacement = 0.1 * mm;
quantity distance = 1 * cm;
quantity<rad> angle = displacement / distance;

or

quantity displacement = isq::arc_length(0.1 * mm);
quantity distance = isq::radius(1 * cm);
quantity<isq::angular_measure[rad]> angle = displacement / distance;

depending if you want to use typed or simple quantities.

Said that, we also have experimental strong-angular systems. You can try those if you prefer to treat angle as a strong quantity.

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

No branches or pull requests

3 participants