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

Add as_raw_number(...) utility #333

Merged
merged 2 commits into from
Dec 2, 2024
Merged

Add as_raw_number(...) utility #333

merged 2 commits into from
Dec 2, 2024

Conversation

chiphogg
Copy link
Contributor

This lets us pave the way to change the behavior of quantity arithmetic
when all units cancel out (#185). If we make this change all at once,
it'll generally be too big and hard to handle. But if we provide this
utility now, then people can migrate individual callsites gradually over
time. Then, at the end (probably 0.5.0?), making the switch to the new
behavior won't be such a big change.

Helps #185.

This lets us pave the way to change the behavior of quantity arithmetic
when all units cancel out (#185).  If we make this change all at once,
it'll generally be too big and hard to handle.  But if we provide this
utility now, then people can migrate individual callsites gradually over
time.  Then, at the end (probably 0.5.0?), making the switch to the new
behavior won't be such a big change.

Helps #185.
@chiphogg chiphogg added the release notes: ✨ lib (enhancement) PR enhancing the library code label Nov 29, 2024
@chiphogg chiphogg requested a review from geoffviola November 29, 2024 19:21
Copy link
Contributor

@geoffviola geoffviola left a comment

Choose a reason for hiding this comment

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

Sounds like this change will help migrate.

I feel like .in(unos) isn't great if the quantity wasn't a unos to begin with. But I do like keeping in the tradition of mentioning the type somewhere in the conversion statement. For example,

auto foo_p = percent(0.7);
...
double foo_d = foo_p.in(percent);

seems better than

auto foo_p = percent(0.7);
...
double foo_d = as_raw_number(foo_p);

@@ -534,6 +534,20 @@ constexpr auto operator%(Quantity<U1, R1> q1, Quantity<U2, R2> q2) {
return make_quantity<U>(q1.in(U{}) % q2.in(U{}));
}

// Callsite-readable way to convert a `Quantity` to a raw number.
//
// Only works for dimensionless `Quantities`; will return a compile-time error otherwise.
Copy link
Contributor

Choose a reason for hiding this comment

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

Commend: Thanks for this clarification. I was worried about cases like as_raw_number(seconds(1)).

@chiphogg
Copy link
Contributor Author

chiphogg commented Dec 2, 2024

Sounds like this change will help migrate.

I feel like .in(unos) isn't great if the quantity wasn't a unos to begin with. But I do like keeping in the tradition of mentioning the type somewhere in the conversion statement. For example,

auto foo_p = percent(0.7);
...
double foo_d = foo_p.in(percent);

seems better than

auto foo_p = percent(0.7);
...
double foo_d = as_raw_number(foo_p);

Great analysis, and I agree. The main use case for using as_raw_number with non-unitless dimensionless quantities is for results of calculations. This will let somebody write as_raw_number(frequency * duration), and have it continue to work even when we refactor to use more convenient units for frequency or duration --- as long as the combination can be safely converted to a raw number.

@chiphogg
Copy link
Contributor Author

chiphogg commented Dec 2, 2024

Sounds like this change will help migrate.

I feel like .in(unos) isn't great if the quantity wasn't a unos to begin with. But I do like keeping in the tradition of mentioning the type somewhere in the conversion statement. For example,

auto foo_p = percent(0.7);
...
double foo_d = foo_p.in(percent);

seems better than

auto foo_p = percent(0.7);
...
double foo_d = as_raw_number(foo_p);

Oh! And just to be crystal-clear, foo_p.in(percent) has different semantics than as_raw_number(foo_p) --- the former gives 0.7, while the latter gives 0.007 (or, in each case, as nearly as we can represent those values in double).

Non-unitless dimensionless units are really hard. 😅

@chiphogg chiphogg merged commit d450960 into main Dec 2, 2024
13 checks passed
@chiphogg chiphogg deleted the chiphogg/as-raw-number#185 branch December 2, 2024 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes: ✨ lib (enhancement) PR enhancing the library code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants