-
Notifications
You must be signed in to change notification settings - Fork 393
Fix arithmetic for Temperature #219
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
Conversation
@SirRufo Can you look over and comment? I made a stab at this, but having problem with arithmetic doing things like See |
On a side note, the whole inherit units stuff was a side track, as I wound up not using it when I realized the conversions were different. I am keeping it, as it's been a topic of discussion before. Might come in handy later. |
We cannot add two temperatures or multiply a temperature by a n (which is just adding for n times), because it does not make sense at all. If we measure two cubes with each have a side length of 1 m, putting them together will indeed double the volume (2 * 1 cbm = 2 cbm), but the temperature of both (20°C measured for each) will not double - it will remain the same at 20°C. If the temperature is different (10°C and 20°C) then putting both together we will have a total temperature of 15°C (assuming both are of the same homogene material and ideal conditions). The whole temperature arithmetic has to be done with the delta values. BTW: I would also remove the - operator from temperature type. |
{ | ||
public partial struct Temperature | ||
{ | ||
public static Temperature operator -(Temperature right) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove this
Thanks, yes I think you are right. As much as 2 * 20 degrees Celsius should be 40 degrees Celsius, this must be multiplied by a temperature delta of 2 degrees Celsius. After a good night's sleep I finally realized that the different temperature scales have different zero points in addition to different scaling factors, so you can't simply multiply by two, but you can multiply or divide by some amount within each scale - the I'll remove the number arithmetic and only keep the Delta arithmetic with regards to Temperature, including the negate So, the next thing that comes to mind. Are there other unit classes beyond |
ef94181
to
8e1d45f
Compare
@SirRufo Please review again. I found that Temperature.FromDegreesCelsius(20) * TemperatureDelta.FromDegreesCelsiusDelta(3) // NOT 60 degrees C..
Temperature.FromDegreesCelsius(20).Multiply(3, TemperatureUnit.DegreeCelsius) // 60 degrees C Am I mistaken, or is this the path we must take for multiplication and division? I've added some more tests now and all arithmetic seem to work as I expect now. Fahenheit and Celsius have different scales and different zero points, so they should cover most cases. By the way, I did not mean to hijack your PR, but I went down a rabbit hole and have yet to climb out :-) |
I only have a look at the units ...
Sure we need this? That are the ones I see at first:
Useful units (which are new to your lib) dealing with
but that is another story |
Where did you see this? I thought I removed all The Good point about the new units |
So from what I can tell, this PR is now ready to merge.
|
Defaults to true if not set (backwards compatible). Set to false to not generate default arithmetic for a unit class, such as Temperature that must have custom arithmetic.
See discussion in related issue. The semantics of the current Temperature arithmetic is plain wrong, such as adding 1 + 1 degrees Celsius is not 2 degrees Celsius, but rather +275.15 degrees Celsius. The problem is how we in speech mixi temperature delta/interval and temperature measurements, in contrast to time (DateTime vs TimeSpan, with different units). Refs #218 #218
Inherits units from Temperature.
Use TemperatureDelta for + and - operators.
Cannot use TemperatureDelta with * and / operators, since different temperature units have different zero points, so one must first convert to a unit of choice, then perform the arithmetic, then convert back to Temperature.
8e1d45f
to
04ced50
Compare
Fixes #218
Temperature
removed, they were plain wrongTemperature
andTemperatureDelta
is addedTemperature.Multiply()
and.Divide()
, easy to get wrongTemperatureDelta
added with default arithmetic, which should cover the examples you gave above