Skip to content

Commit 418b714

Browse files
committed
Temperature: Add Multiply() and Divide() methods
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.
1 parent 823ed2a commit 418b714

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

UnitsNet/CustomCode/UnitClasses/Temperature.extra.cs

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,86 @@
2121

2222
#if !WINDOWS_UWP
2323
// Operator overloads not supported in Universal Windows Platform (WinRT Components)
24-
using System;
24+
using UnitsNet.Units;
2525

2626
namespace UnitsNet
2727
{
2828
public partial struct Temperature
2929
{
30+
/// <summary>
31+
/// Add a <see cref="Temperature" /> and a <see cref="TemperatureDelta" />.
32+
/// </summary>
33+
/// <remarks>Due to temperature units having different scales, the arithmetic must be performed on the same scale.</remarks>
34+
/// <returns>The new temperature.</returns>
3035
public static Temperature operator +(Temperature left, TemperatureDelta right)
3136
{
3237
return new Temperature(left.Kelvins + right.KelvinsDelta);
3338
}
3439

40+
/// <summary>
41+
/// Add a <see cref="TemperatureDelta" /> and a <see cref="Temperature" />.
42+
/// </summary>
43+
/// <remarks>Due to temperature units having different scales, the arithmetic must be performed on the same scale.</remarks>
44+
/// <returns>The new temperature.</returns>
3545
public static Temperature operator +(TemperatureDelta left, Temperature right)
3646
{
3747
return new Temperature(left.KelvinsDelta + right.Kelvins);
3848
}
3949

50+
/// <summary>
51+
/// Subtract a <see cref="Temperature" /> by a <see cref="TemperatureDelta" />.
52+
/// </summary>
53+
/// <remarks>Due to temperature units having different scales, the arithmetic must be performed on the same scale.</remarks>
54+
/// <returns>The new temperature.</returns>
4055
public static Temperature operator -(Temperature left, TemperatureDelta right)
4156
{
4257
return new Temperature(left.Kelvins - right.KelvinsDelta);
4358
}
4459

60+
/// <summary>
61+
/// Subtract a <see cref="Temperature" /> by a <see cref="TemperatureDelta" />.
62+
/// </summary>
63+
/// <remarks>Due to temperature units having different scales, the arithmetic must be performed on the same scale.</remarks>
64+
/// <returns>The delta temperature (difference).</returns>
4565
public static TemperatureDelta operator -(Temperature left, Temperature right)
4666
{
4767
return new TemperatureDelta(left.Kelvins - right.Kelvins);
4868
}
4969

50-
// public static Temperature operator *(double left, Temperature right)
51-
// {
52-
// return new Temperature(left * right.Kelvins);
53-
// }
54-
//
55-
// public static Temperature operator *(Temperature left, double right)
56-
// {
57-
// return new Temperature(left.Kelvins * right);
58-
// }
59-
//
60-
// public static Temperature operator /(Temperature left, double right)
61-
// {
62-
// return new Temperature(left.Kelvins / right);
63-
// }
64-
//
65-
// public static double operator /(Temperature left, Temperature right)
66-
// {
67-
// return Convert.ToDouble(left.Kelvins / right.Kelvins);
68-
// }
70+
/// <summary>
71+
/// Multiply temperature with a <paramref name="factor" /> in a given <paramref name="unit" />.
72+
/// </summary>
73+
/// <remarks>
74+
/// Due to different temperature units having different zero points, we cannot simply
75+
/// multiply or divide a temperature by a factor. We must first convert to the desired unit, then perform the
76+
/// calculation.
77+
/// </remarks>
78+
/// <param name="factor">Factor to multiply by.</param>
79+
/// <param name="unit">Unit to perform multiplication in.</param>
80+
/// <returns>The resulting <see cref="Temperature" />.</returns>
81+
public Temperature Multiply(double factor, TemperatureUnit unit)
82+
{
83+
double resultInUnit = As(unit) * factor;
84+
return From(resultInUnit, unit);
85+
}
86+
87+
88+
/// <summary>
89+
/// Divide temperature by a <paramref name="divisor" /> in a given <paramref name="unit" />.
90+
/// </summary>
91+
/// <remarks>
92+
/// Due to different temperature units having different zero points, we cannot simply
93+
/// multiply or divide a temperature by a factor. We must first convert to the desired unit, then perform the
94+
/// calculation.
95+
/// </remarks>
96+
/// <param name="divisor">Factor to multiply by.</param>
97+
/// <param name="unit">Unit to perform multiplication in.</param>
98+
/// <returns>The resulting <see cref="Temperature" />.</returns>
99+
public Temperature Divide(double divisor, TemperatureUnit unit)
100+
{
101+
double resultInUnit = As(unit) / divisor;
102+
return From(resultInUnit, unit);
103+
}
69104
}
70105
}
71106

0 commit comments

Comments
 (0)