Skip to content

Commit

Permalink
#486 - Add Temperature Unit
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Nov 16, 2021
1 parent d0a7c11 commit 83171a8
Show file tree
Hide file tree
Showing 16 changed files with 880 additions and 8 deletions.
4 changes: 4 additions & 0 deletions xFunc.Maths/Analyzers/Analyzer{TResult,TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public virtual TResult Analyze(Angle exp, TContext context)
public virtual TResult Analyze(Power exp, TContext context)
=> Analyze(exp as IExpression, context);

/// <inheritdoc />
public virtual TResult Analyze(Temperature exp, TContext context)
=> Analyze(exp as IExpression, context);

/// <inheritdoc />
public virtual TResult Analyze(ToDegree exp, TContext context)
=> Analyze(exp as IExpression, context);
Expand Down
4 changes: 4 additions & 0 deletions xFunc.Maths/Analyzers/Analyzer{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public virtual TResult Analyze(Angle exp)
public virtual TResult Analyze(Power exp)
=> Analyze(exp as IExpression);

/// <inheritdoc />
public virtual TResult Analyze(Temperature exp)
=> Analyze(exp as IExpression);

/// <inheritdoc />
public virtual TResult Analyze(ToDegree exp)
=> Analyze(exp as IExpression);
Expand Down
4 changes: 4 additions & 0 deletions xFunc.Maths/Analyzers/Formatters/CommonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public virtual string Analyze(Angle exp)
public virtual string Analyze(Power exp)
=> exp.Value.ToString();

/// <inheritdoc />
public virtual string Analyze(Temperature exp)
=> exp.Value.ToString();

/// <inheritdoc />
public virtual string Analyze(ToDegree exp)
=> ToString(exp, "todegree({0})");
Expand Down
8 changes: 8 additions & 0 deletions xFunc.Maths/Analyzers/IAnalyzer{TResult,TContext}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ public interface IAnalyzer<out TResult, in TContext>
/// <returns>The result of analysis.</returns>
TResult Analyze(Power exp, TContext context);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <param name="context">The context.</param>
/// <returns>The result of analysis.</returns>
TResult Analyze(Temperature exp, TContext context);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions xFunc.Maths/Analyzers/IAnalyzer{TResult}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ public interface IAnalyzer<out TResult>
/// <returns>The result of analysis.</returns>
TResult Analyze(Power exp);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
TResult Analyze(Temperature exp);

/// <summary>
/// Analyzes the specified expression.
/// </summary>
Expand Down
7 changes: 6 additions & 1 deletion xFunc.Maths/Analyzers/TypeAnalyzers/ResultTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public enum ResultTypes
/// </summary>
PowerNumber = 1 << 9,

/// <summary>
/// The expression returns a temperature number.
/// </summary>
TemperatureNumber = 1 << 10,

/// <summary>
/// The expression returns a number or a complex number.
/// </summary>
Expand Down Expand Up @@ -94,5 +99,5 @@ public enum ResultTypes
/// <summary>
/// The expression returns any type of number.
/// </summary>
Numbers = Number | AngleNumber | PowerNumber,
Numbers = Number | AngleNumber | PowerNumber | TemperatureNumber,
}
4 changes: 4 additions & 0 deletions xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ public virtual ResultTypes Analyze(Angle exp)
public virtual ResultTypes Analyze(Power exp)
=> CheckArgument(exp, ResultTypes.PowerNumber);

/// <inheritdoc />
public virtual ResultTypes Analyze(Temperature exp)
=> CheckArgument(exp, ResultTypes.TemperatureNumber);

/// <inheritdoc />
public virtual ResultTypes Analyze(ToDegree exp)
=> AngleConversion(exp);
Expand Down
14 changes: 7 additions & 7 deletions xFunc.Maths/Expressions/Units/PowerUnits/PowerValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ private static PowerUnit GetCommonUnit(PowerUnit left, PowerUnit right)
};

/// <summary>
/// Converts the current object to degrees.
/// Converts the current object to watt.
/// </summary>
/// <returns>The power value which is converted to degrees.</returns>
/// <returns>The power value which is converted to watts.</returns>
public PowerValue ToWatt() => Unit switch
{
PowerUnit.Watt => this,
Expand All @@ -269,9 +269,9 @@ private static PowerUnit GetCommonUnit(PowerUnit left, PowerUnit right)
};

/// <summary>
/// Converts the current object to radians.
/// Converts the current object to kilowatt.
/// </summary>
/// <returns>The power value which is converted to radians.</returns>
/// <returns>The power value which is converted to kilowatts.</returns>
public PowerValue ToKilowatt() => Unit switch
{
PowerUnit.Watt => Kilowatt(Value / 1000.0),
Expand All @@ -281,9 +281,9 @@ private static PowerUnit GetCommonUnit(PowerUnit left, PowerUnit right)
};

/// <summary>
/// Converts the current object to gradians.
/// Converts the current object to horsepower.
/// </summary>
/// <returns>The power value which is converted to gradians.</returns>
/// <returns>The power value which is converted to horsepowers.</returns>
public PowerValue ToHorsepower() => Unit switch
{
PowerUnit.Watt => Horsepower(Value / 745.69987158227022),
Expand Down Expand Up @@ -335,7 +335,7 @@ public static PowerValue Frac(PowerValue value)
/// <summary>
/// Converts <see cref="PowerValue"/> to <see cref="Power"/>.
/// </summary>
/// <returns>The angle number.</returns>
/// <returns>The power number.</returns>
public Power AsExpression()
=> new Power(this);

Expand Down
29 changes: 29 additions & 0 deletions xFunc.Maths/Expressions/Units/TemperatureUnits/Temperature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace xFunc.Maths.Expressions.Units.TemperatureUnits;

/// <summary>
/// Represents an temperature number.
/// </summary>
public class Temperature : Unit<TemperatureValue>
{
/// <summary>
/// Initializes a new instance of the <see cref="Temperature"/> class.
/// </summary>
/// <param name="value">A temperature number.</param>
public Temperature(TemperatureValue value)
: base(value)
{
}

/// <inheritdoc />
protected override TResult AnalyzeInternal<TResult>(IAnalyzer<TResult> analyzer)
=> analyzer.Analyze(this);

/// <inheritdoc />
protected override TResult AnalyzeInternal<TResult, TContext>(
IAnalyzer<TResult, TContext> analyzer,
TContext context)
=> analyzer.Analyze(this, context);
}
25 changes: 25 additions & 0 deletions xFunc.Maths/Expressions/Units/TemperatureUnits/TemperatureUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace xFunc.Maths.Expressions.Units.TemperatureUnits;

/// <summary>
/// Specifies a measurement of temperature.
/// </summary>
public enum TemperatureUnit
{
/// <summary>
/// Celsius (°C).
/// </summary>
Celsius,

/// <summary>
/// Fahrenheit (°F).
/// </summary>
Fahrenheit,

/// <summary>
/// Kelvin (K).
/// </summary>
Kelvin,
}

0 comments on commit 83171a8

Please sign in to comment.