Skip to content

Commit

Permalink
Add operators for ReciprocalLength/-Area (#1382)
Browse files Browse the repository at this point in the history
Added additional operators that result in `ReciprocalLength`
(Length/Area, Area/Volume) and `ReciprocalArea` (Length/Volume)

Co-authored-by: Travis Bement <[email protected]>
  • Loading branch information
trb5016 and Travis Bement authored Apr 4, 2024
1 parent 6c4faa9 commit 1b87682
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions UnitsNet.Tests/CustomCode/AreaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public void AreaDividedByLengthEqualsLength()
Assert.Equal(length, Length.FromMeters(10));
}

[Fact]
public void AreaDividedByVolumeEqualsReciprocalLength()
{
ReciprocalLength reciprocalLength = Area.FromSquareMeters(50) / Volume.FromCubicMeters(5);
Assert.Equal(reciprocalLength, ReciprocalLength.FromInverseMeters(10));
}

[Fact]
public void AreaTimesMassFluxEqualsMassFlow()
{
Expand Down
14 changes: 14 additions & 0 deletions UnitsNet.Tests/CustomCode/LengthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ public void LengthDividedBySpeedEqualsDuration()
Assert.Equal(Duration.FromSeconds(10), duration);
}

[Fact]
public void LengthDividedByAreaEqualsReciprocalLength()
{
ReciprocalLength reciprocalLength = Length.FromMeters(20) / Area.FromSquareMeters(2);
Assert.Equal(ReciprocalLength.FromInverseMeters(10), reciprocalLength);
}

[Fact]
public void LengthDividedByVolumeEqualsReciprocalArea()
{
ReciprocalArea reciprocalArea = Length.FromMeters(20) / Volume.FromCubicMeters(2);
Assert.Equal(ReciprocalArea.FromInverseSquareMeters(10), reciprocalArea);
}

[Fact]
public void LengthTimesSpeedEqualsKinematicViscosity()
{
Expand Down
6 changes: 6 additions & 0 deletions UnitsNet/CustomCode/Quantities/Area.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public ReciprocalArea Inverse()
return Length.FromMeters(area.SquareMeters / length.Meters);
}

/// <summary>Get <see cref="ReciprocalLength"/> from <see cref="Area"/> divided by <see cref="Volume"/>.</summary>
public static ReciprocalLength operator /(Area area, Volume volume)
{
return ReciprocalLength.FromInverseMeters(area.SquareMeters / volume.CubicMeters);
}

/// <summary>Get <see cref="MassFlow"/> from <see cref="Area"/> times <see cref="MassFlux"/>.</summary>
public static MassFlow operator *(Area area, MassFlux massFlux)
{
Expand Down
12 changes: 12 additions & 0 deletions UnitsNet/CustomCode/Quantities/Length.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ public static bool TryParseFeetInches(string? str, out Length result, IFormatPro
return Duration.FromSeconds(length.Meters/speed.MetersPerSecond);
}

/// <summary>Get <see cref="ReciprocalLength"/> from <see cref="Length"/> divided by <see cref="Area"/>.</summary>
public static ReciprocalLength operator /(Length length, Area area)
{
return ReciprocalLength.FromInverseMeters(length.Meters / area.SquareMeters);
}

/// <summary>Get <see cref="ReciprocalArea"/> from <see cref="Length"/> divided by <see cref="Volume"/>.</summary>
public static ReciprocalArea operator /(Length length, Volume volume)
{
return ReciprocalArea.FromInverseSquareMeters(length.Meters / volume.CubicMeters);
}

/// <summary>Get <see cref="Area"/> from <see cref="Length"/> times <see cref="Length"/>.</summary>
public static Area operator *(Length length1, Length length2)
{
Expand Down

0 comments on commit 1b87682

Please sign in to comment.