-
Notifications
You must be signed in to change notification settings - Fork 391
Units multiplied or divided by ratio #388
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
Comments
I think the units already have generated operators (multiply or divide to double) for that. Or maybe I misunderstood your suggestion? :) e.g. public static Length operator *(Length left, double right)
|
What is not working that way at the moment? Perhaps an operator overload is missing unintentionally. |
In fact, what happens is the operation, for example with Length and Ratio
|
I couldn't understand what you mean, can you please write down what you expect and what you actually get? |
I think he's suggesting to implement operator overload (* and /) for all the quantities (Length, Mass, etc) to the Ratio struct. currently this is not possible: |
Exactly! Thanks @gojanpaolo |
@CPMOliveira Could you provide a real world example on how this would be beneficial? I would actually prefer defining the specific Ratio property to relate to other units. e.g. ...or maybe I'm just not using the Ratio struct that often. :) |
I'm a bit unsure about this request. I understand the usecase,
TLDR; I am not sure how much |
@angularsen Another approach is to implement an implicit conversion from Ratio to double (and/or QuantityValue).
This will make |
Interesting, I didn't originally think implicit cast would work here, but it sure does. If we can agree that decimal fractions is THE intuitive choice for an implicit cast, then I think I could get behind this proposal. I am a little bit concerned that someone will be bitten by accidentally assigning the ratio to a double and getting something else than they expected. Say, if they are working with parts per million numbers and constructing ratios from these, then accidentally assigning a ratio to a double would give them decimal fractions instead. I think it feels kind of weird for this to happen, and the only motivation for adding it is to avoid binary size increase to support arithmetic using Ratio instead of double. And we're not sure how many are using ratios like this to begin with. Are we adding value here or are we just complicating things? I'm a little on the fence on this one... would like to have some more opinions on this first. |
Guys Sorry for the delay in answering, but I was absent these last few days. An addendum: although I can use the dimensionless variables as double, many of them are represented as percentage, ppm or ppt. |
@CPMOliveira, keep in mind that my proposed solution (implicit conversion) removes static typing when using the Ratio struct which is not always desirable. :) |
So after thinking a bit more on this, if we add implicit cast to double then decimal fraction is probably the most sane choice rather than percentage, parts per million etc. I still don't have a good overview of how easy it is to "accidentally" cast from Ratio to double and expect it to be something else than decimal fraction, but I can imagine it happen. So the thing to consider is how likely such accidents are to happen versus the increase in binary size of adding Ratio to all quantities' operator overload for multiplication and division to make such accidents impossible. Let's also not forget @gojanpaolo 's example, I tried to choose some quantities that make sense to me:
Although it is nice to not have to call Trying to come up with an example of when maybe implicit cast to double can trip you up:
I think refactoring like this is probably the most likely reason for getting the implicit cast wrong. One can argue that it's the coder's own fault, but I'm not a fan of running into quirks like this myself. Implicit casts should be used with care. |
It's not a issue, but a suggestion, all units remain its units when multiplied or divided by ratio.
The text was updated successfully, but these errors were encountered: