Skip to content

Commit

Permalink
Fix some ambiguity issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Muximize committed Feb 4, 2024
1 parent 79a11c3 commit df96c8e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/MassConcentrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static void ComponentMassFromMassConcentrationAndSolutionVolume(
var massConcentration = new MassConcentration(massConcValue, massConcUnit);
var volume = new Volume(volumeValue, volumeUnit);

Mass massComponent = massConcentration * volume;
Mass massComponent = (Density)massConcentration * volume;

AssertEx.EqualTolerance(expectedMassValue, massComponent.As(expectedMassUnit), tolerance);
}
Expand Down
20 changes: 18 additions & 2 deletions UnitsNet/CustomCode/Quantities/MassConcentration.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,29 @@ namespace UnitsNet
{
public partial struct MassConcentration
{
/// <summary>
/// Because Density and MassConcentration are almost the same, we can convert between them.
/// </summary>
public static implicit operator MassConcentration(Density density)
{
return From(density.ToUnit(Density.BaseUnit).Value, BaseUnit);
}

/// <summary>
/// Because Density and MassConcentration are almost the same, we can convert between them.
/// </summary>
public static explicit operator Density(MassConcentration massConcentration)
{
return Density.From(massConcentration.ToUnit(BaseUnit).Value, Density.BaseUnit);
}

/// <summary>
/// Get <see cref="Molarity" /> from this <see cref="MassConcentration" /> using the known component <see cref="MolarMass" />.
/// </summary>
/// <param name="molecularWeight"></param>
public Molarity ToMolarity(MolarMass molecularWeight)
{
return this / molecularWeight;
return (Density)this / molecularWeight;
}

/// <summary>
Expand All @@ -21,7 +37,7 @@ public Molarity ToMolarity(MolarMass molecularWeight)
/// <returns></returns>
public VolumeConcentration ToVolumeConcentration(Density componentDensity)
{
return this / componentDensity;
return VolumeConcentration.FromDecimalFractions((Density)this / componentDensity);
}

#region Static Methods
Expand Down
4 changes: 2 additions & 2 deletions UnitsNet/CustomCode/Quantities/MassFraction.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public partial struct MassFraction
/// <returns>The actual mass of the component involved in this mixture</returns>
public Mass GetComponentMass(Mass totalMass)
{
return totalMass * this;
return totalMass * DecimalFractions;
}

/// <summary>
Expand All @@ -21,7 +21,7 @@ public Mass GetComponentMass(Mass totalMass)
/// <returns>The total mass of the mixture</returns>
public Mass GetTotalMass(Mass componentMass)
{
return componentMass / this;
return componentMass / DecimalFractions;
}

#region Static Methods
Expand Down
2 changes: 1 addition & 1 deletion UnitsNet/CustomCode/Quantities/Molarity.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public MassConcentration ToMassConcentration(MolarMass molecularWeight)
/// <param name="componentMass"></param>
public VolumeConcentration ToVolumeConcentration(Density componentDensity, MolarMass componentMass)
{
return this * componentMass / componentDensity;
return VolumeConcentration.FromDecimalFractions(this * componentMass / componentDensity);
}

#region Static Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static VolumeConcentration FromVolumes(Volume componentVolume, Volume mix
/// <param name="componentMolarMass"></param>
public static VolumeConcentration FromMolarity(Molarity molarity, Density componentDensity, MolarMass componentMolarMass)
{
return molarity * componentMolarMass / componentDensity;
return FromDecimalFractions(molarity * componentMolarMass / componentDensity);
}

#endregion
Expand Down

0 comments on commit df96c8e

Please sign in to comment.