-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
425 additions
and
503 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,78 @@ | ||
// Copyright (c) Dmytro Kyshchenko. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace xFunc.Maths.Expressions.Units.PowerUnits; | ||
|
||
/// <summary> | ||
/// Specifies a measurement of power. | ||
/// Represents a power unit. | ||
/// </summary> | ||
public enum PowerUnit | ||
public readonly struct PowerUnit : IEquatable<PowerUnit> | ||
{ | ||
/// <summary> | ||
/// Watt (W). | ||
/// </summary> | ||
Watt, | ||
public static readonly PowerUnit Watt = new PowerUnit(1.0, "W"); | ||
|
||
/// <summary> | ||
/// Kilowatt (kW). | ||
/// </summary> | ||
Kilowatt, | ||
public static readonly PowerUnit Kilowatt = new PowerUnit(1000.0, "kW"); | ||
|
||
/// <summary> | ||
/// Horsepower (hp). | ||
/// </summary> | ||
Horsepower, | ||
public static readonly PowerUnit Horsepower = new PowerUnit(745.69987158227022, "hp"); | ||
|
||
private PowerUnit(double factor, string unitName) | ||
{ | ||
Factor = factor; | ||
UnitName = unitName; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether two specified instances of <see cref="PowerUnit"/> are equal. | ||
/// </summary> | ||
/// <param name="left">The first object to compare.</param> | ||
/// <param name="right">The second object to compare.</param> | ||
/// <returns><c>true</c> if <paramref name="left"/> is equal to <paramref name="right"/>; otherwise, <c>false</c>.</returns> | ||
public static bool operator ==(PowerUnit left, PowerUnit right) | ||
=> left.Equals(right); | ||
|
||
/// <summary> | ||
/// Determines whether two specified instances of <see cref="PowerUnit"/> are equal. | ||
/// </summary> | ||
/// <param name="left">The first object to compare.</param> | ||
/// <param name="right">The second object to compare.</param> | ||
/// <returns><c>true</c> if <paramref name="left"/> is equal to <paramref name="right"/>; otherwise, <c>false</c>.</returns> | ||
public static bool operator !=(PowerUnit left, PowerUnit right) | ||
=> !left.Equals(right); | ||
|
||
/// <inheritdoc /> | ||
public bool Equals(PowerUnit other) | ||
=> Factor.Equals(other.Factor) && UnitName == other.UnitName; | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals(object? obj) | ||
=> obj is PowerUnit other && Equals(other); | ||
|
||
/// <inheritdoc /> | ||
[ExcludeFromCodeCoverage] | ||
public override int GetHashCode() | ||
=> HashCode.Combine(Factor, UnitName); | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() | ||
=> UnitName; | ||
|
||
/// <summary> | ||
/// Gets a factor of conversion from this unit to base unit. | ||
/// </summary> | ||
public double Factor { get; } | ||
|
||
/// <summary> | ||
/// Gets a short name of the unit. | ||
/// </summary> | ||
public string UnitName { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 68 additions & 5 deletions
73
xFunc.Maths/Expressions/Units/TemperatureUnits/TemperatureUnit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,88 @@ | ||
// Copyright (c) Dmytro Kyshchenko. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace xFunc.Maths.Expressions.Units.TemperatureUnits; | ||
|
||
/// <summary> | ||
/// Specifies a measurement of temperature. | ||
/// Represents a temperature unit. | ||
/// </summary> | ||
public enum TemperatureUnit | ||
public readonly struct TemperatureUnit : IEquatable<TemperatureUnit> | ||
{ | ||
/// <summary> | ||
/// Celsius (°C). | ||
/// </summary> | ||
Celsius, | ||
public static readonly TemperatureUnit Celsius = new TemperatureUnit(x => x, x => x, "°C"); | ||
|
||
/// <summary> | ||
/// Fahrenheit (°F). | ||
/// </summary> | ||
Fahrenheit, | ||
public static readonly TemperatureUnit Fahrenheit = new TemperatureUnit( | ||
x => (x - 32) * 5.0 / 9.0, x => x * 9.0 / 5.0 + 32, "°F"); | ||
|
||
/// <summary> | ||
/// Kelvin (K). | ||
/// </summary> | ||
Kelvin, | ||
public static readonly TemperatureUnit Kelvin = new TemperatureUnit( | ||
x => x - 273.15, x => x + 273.15, "K"); | ||
|
||
private TemperatureUnit(Func<double, double> toBase, Func<double, double> fromBase, string unitName) | ||
{ | ||
ToBase = toBase; | ||
FromBase = fromBase; | ||
UnitName = unitName; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether two specified instances of <see cref="TemperatureUnit"/> are equal. | ||
/// </summary> | ||
/// <param name="left">The first object to compare.</param> | ||
/// <param name="right">The second object to compare.</param> | ||
/// <returns><c>true</c> if <paramref name="left"/> is equal to <paramref name="right"/>; otherwise, <c>false</c>.</returns> | ||
public static bool operator ==(TemperatureUnit left, TemperatureUnit right) | ||
=> left.Equals(right); | ||
|
||
/// <summary> | ||
/// Determines whether two specified instances of <see cref="TemperatureUnit"/> are equal. | ||
/// </summary> | ||
/// <param name="left">The first object to compare.</param> | ||
/// <param name="right">The second object to compare.</param> | ||
/// <returns><c>true</c> if <paramref name="left"/> is equal to <paramref name="right"/>; otherwise, <c>false</c>.</returns> | ||
public static bool operator !=(TemperatureUnit left, TemperatureUnit right) | ||
=> !left.Equals(right); | ||
|
||
/// <inheritdoc /> | ||
public bool Equals(TemperatureUnit other) | ||
=> ToBase.Equals(other.ToBase) && | ||
FromBase.Equals(other.FromBase) && | ||
UnitName == other.UnitName; | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals(object? obj) | ||
=> obj is TemperatureUnit other && Equals(other); | ||
|
||
/// <inheritdoc /> | ||
[ExcludeFromCodeCoverage] | ||
public override int GetHashCode() | ||
=> HashCode.Combine(ToBase, FromBase, UnitName); | ||
|
||
/// <inheritdoc /> | ||
public override string ToString() | ||
=> UnitName; | ||
|
||
/// <summary> | ||
/// Gets the conversion function from the current unit to the base unit. | ||
/// </summary> | ||
public Func<double, double> ToBase { get; } | ||
|
||
/// <summary> | ||
/// Gets the conversion function from the base unit to the current unit. | ||
/// </summary> | ||
public Func<double, double> FromBase { get; } | ||
|
||
/// <summary> | ||
/// Gets a short name of the unit. | ||
/// </summary> | ||
public string UnitName { get; } | ||
} |
Oops, something went wrong.