Skip to content

Commit cca315c

Browse files
authored
Add operators between Energy, ElectricCharge, EletricPotential (#1249)
1 parent 52f05c0 commit cca315c

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

UnitsNet.Tests/CustomCode/ElectricChargeTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,17 @@ public void ElectricChargeDividedByDurationEqualsElectricCurrent()
3939
ElectricCurrent i = ElectricCharge.FromAmpereHours(20) / Duration.FromHours(4);
4040
Assert.Equal(5, i.Amperes);
4141
}
42+
43+
[Theory]
44+
[InlineData(1, 1, 1)]
45+
[InlineData(0, int.MaxValue, 0)]
46+
[InlineData(10, 2, 20)]
47+
[InlineData(-10, 2, -20)]
48+
[InlineData(-10, -2, 20)]
49+
public void ElectricChargeMultipliedByElectricPotentialEqualsEnergy(float current, float potential, float expected)
50+
{
51+
Energy j = ElectricCharge.FromCoulombs(current) * ElectricPotential.FromVolts(potential);
52+
Assert.Equal(expected, j.Joules);
53+
}
4254
}
4355
}

UnitsNet.Tests/CustomCode/ElectricPotentialTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,17 @@ public void ElectricPotentialMultipliedByElectricCurrentEqualsPower()
5050
Power p = ElectricPotential.FromVolts(10) * ElectricCurrent.FromAmperes(2);
5151
Assert.Equal(20, p.Watts);
5252
}
53+
54+
[Theory]
55+
[InlineData(1, 1, 1)]
56+
[InlineData(0, int.MaxValue, 0)]
57+
[InlineData(10, 2, 20)]
58+
[InlineData(-10, 2, -20)]
59+
[InlineData(-10, -2, 20)]
60+
public void ElectricPotentialMultipliedByElectricChargeEqualsEnergy(float potential, float current, float expected)
61+
{
62+
Energy j = ElectricPotential.FromVolts(potential) * ElectricCharge.FromCoulombs(current);
63+
Assert.Equal(expected, j.Joules);
64+
}
5365
}
5466
}

UnitsNet.Tests/CustomCode/EnergyTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ public void EnergyDividedByPowerEqualsDuration()
132132
Assert.Equal(5, d.Hours);
133133
}
134134

135+
[Fact]
136+
public void EnergyDividedByElectricPotentialEqualsElectricCharge()
137+
{
138+
ElectricCharge c = Energy.FromJoules(20) / ElectricPotential.FromVolts(5);
139+
Assert.Equal(4, c.Coulombs);
140+
}
141+
142+
[Fact]
143+
public void EnergyDividedByElectricChargeEqualsElectricPotential()
144+
{
145+
ElectricPotential v = Energy.FromJoules(20) / ElectricCharge.FromCoulombs(5);
146+
Assert.Equal(4, v.Volts);
147+
}
148+
135149
[Fact]
136150
public void EnergyTimesFrequencyEqualsPower()
137151
{

UnitsNet/CustomCode/Quantities/ElectricCharge.extra.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@ public partial struct ElectricCharge
1616
{
1717
return Duration.FromHours(charge.AmpereHours / current.Amperes);
1818
}
19+
20+
/// <summary>Get <see cref="Energy"/> from <see cref="ElectricCharge"/> times <see cref="ElectricPotential"/> .</summary>
21+
public static Energy operator *(ElectricCharge charge, ElectricPotential potential)
22+
{
23+
return Energy.FromJoules(potential.Volts * charge.Coulombs);
24+
}
1925
}
2026
}

UnitsNet/CustomCode/Quantities/ElectricPotential.extra.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,11 @@ public AmplitudeRatio ToAmplitudeRatio()
3939
{
4040
return Power.FromWatts(potential.Volts * current.Amperes);
4141
}
42+
43+
/// <summary>Get <see cref="Energy"/> from <see cref="ElectricPotential"/> times <see cref="ElectricCharge"/>.</summary>
44+
public static Energy operator *(ElectricPotential potential, ElectricCharge charge)
45+
{
46+
return Energy.FromJoules(potential.Volts * charge.Coulombs);
47+
}
4248
}
4349
}

UnitsNet/CustomCode/Quantities/Energy.extra.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ public partial struct Energy
2525
return Duration.FromSeconds(energy.Joules / (double)power.Watts);
2626
}
2727

28+
/// <summary>Get <see cref="ElectricCharge"/> from <see cref="Energy"/> divided by <see cref="ElectricPotential"/>.</summary>
29+
public static ElectricCharge operator /(Energy energy, ElectricPotential potential)
30+
{
31+
return ElectricCharge.FromCoulombs(energy.Joules / potential.Volts);
32+
}
33+
34+
/// <summary>Get <see cref="ElectricPotential"/> from <see cref="Energy"/> divided by <see cref="ElectricCharge"/>.</summary>
35+
public static ElectricPotential operator /(Energy energy, ElectricCharge current)
36+
{
37+
return ElectricPotential.FromVolts(energy.Joules / current.Coulombs);
38+
}
39+
2840
/// <summary>Get <see cref="Power"/> from <see cref="Energy"/> times <see cref="Frequency"/>.</summary>
2941
public static Power operator *(Energy energy, Frequency frequency)
3042
{

0 commit comments

Comments
 (0)