Skip to content

Commit

Permalink
#306 - Part of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Oct 4, 2020
1 parent d0a6e65 commit deb6e5a
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 87 deletions.
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Angles/AngleValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,6 @@ public Angle AsExpression()
/// <summary>
/// Gets an integer that indicates the sign of a double-precision floating-point number.
/// </summary>
public int Sign => Math.Sign(Value);
public double Sign => Math.Sign(Value);
}
}
1 change: 0 additions & 1 deletion xFunc.Maths/Expressions/Floor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using xFunc.Maths.Analyzers;
Expand Down
1 change: 0 additions & 1 deletion xFunc.Maths/Expressions/Lg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Immutable;
using System.Numerics;
using xFunc.Maths.Analyzers;
Expand Down
1 change: 0 additions & 1 deletion xFunc.Maths/Expressions/Ln.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Immutable;
using System.Numerics;
using xFunc.Maths.Analyzers;
Expand Down
1 change: 0 additions & 1 deletion xFunc.Maths/Expressions/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Immutable;
using System.Numerics;
using xFunc.Maths.Analyzers;
Expand Down
127 changes: 117 additions & 10 deletions xFunc.Maths/Expressions/NumberValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using xFunc.Maths.Expressions.Angles;
using xFunc.Maths.Resources;

namespace xFunc.Maths.Expressions
{
Expand Down Expand Up @@ -609,21 +610,19 @@ public static NumberValue Frac(NumberValue numberValue)
/// <summary>
/// Returns the polynomial greatest common divisor.
/// </summary>
/// <param name="a">The first numbers.</param>
/// <param name="b">The second numbers.</param>
/// <param name="left">The first numbers.</param>
/// <param name="right">The second numbers.</param>
/// <returns>The greatest common divisor.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NumberValue GCD(NumberValue a, NumberValue b)
public static NumberValue GCD(NumberValue left, NumberValue right)
{
return new NumberValue(GCD(a.Value, b.Value));
var a = left.Value;
var b = right.Value;

static double GCD(double a, double b)
{
while (!Equals(b, 0.0))
b = a % (a = b);
while (!Equals(b, 0.0))
b = a % (a = b);

return a;
}
return new NumberValue(a);
}

/// <summary>
Expand Down Expand Up @@ -717,6 +716,109 @@ public static object Pow(NumberValue number, NumberValue power)
public static Complex Pow(Complex number, NumberValue power)
=> Complex.Pow(number, power.Value);

/// <summary>
/// Rounds a double-precision floating-point value to a specified number of fractional digits,
/// and uses the specified rounding convention for midpoint values.
/// </summary>
/// <param name="number">A double-precision floating-point number to be rounded.</param>
/// <param name="digits">The number of fractional digits in the return value.</param>
/// <returns>The number nearest to <paramref name="number"/> that has a number of fractional digits equal to <paramref name="digits"/>. If value has fewer fractional digits than <paramref name="digits"/>, <paramref name="number"/> is returned unchanged.</returns>
/// <exception cref="InvalidOperationException">The value is not an integer.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NumberValue Round(NumberValue number, NumberValue digits)
{
if (!digits.Value.IsInt())
throw new InvalidOperationException(Resource.ValueIsNotInteger);

var rounded = Math.Round(number.Value, (int)digits.Value, MidpointRounding.AwayFromZero);

return new NumberValue(rounded);
}

/// <summary>
/// Returns the square root of a specified number.
/// </summary>
/// <param name="numberValue">The number whose square root is to be found.</param>
/// <returns>The square root.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Sqrt(NumberValue numberValue)
{
if (numberValue < 0)
return Complex.Sqrt(numberValue.Value);

return new NumberValue(Math.Sqrt(numberValue.Value));
}

private static string PadNumber(string number, int padding)
{
var padLength = number.Length % padding;
if (padLength > 0)
padLength = number.Length + (padding - padLength);

return number.PadLeft(padLength, '0');
}

/// <summary>
/// Converts <paramref name="numberValue"/> to the binary number.
/// </summary>
/// <param name="numberValue">The number.</param>
/// <returns>String that contains the number in the new numeral system.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToBin(NumberValue numberValue)
{
// TODO:
var number = numberValue.Value;
if (!number.IsInt())
throw new ArgumentException(Resource.ValueIsNotInteger, nameof(numberValue));

var result = Convert.ToString((int)number, 2);
result = PadNumber(result, 8);

return $"0b{result}";
}

/// <summary>
/// Converts <paramref name="numberValue"/> to the octal number.
/// </summary>
/// <param name="numberValue">The number.</param>
/// <returns>String that contains the number in the new numeral system.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToOct(NumberValue numberValue)
{
var number = numberValue.Value;
if (!number.IsInt())
throw new ArgumentException(Resource.ValueIsNotInteger, nameof(numberValue));

return $"0{Convert.ToString((int)number, 8)}";
}

/// <summary>
/// Converts <paramref name="numberValue"/> to the hexadecimal number.
/// </summary>
/// <param name="numberValue">The number.</param>
/// <returns>String that contains the number in the new numeral system.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToHex(NumberValue numberValue)
{
var number = numberValue.Value;
if (!number.IsInt())
throw new ArgumentException(Resource.ValueIsNotInteger, nameof(numberValue));

var result = Convert.ToString((int)number, 16).ToUpperInvariant();
result = PadNumber(result, 2);

return $"0x{result}";
}

/// <summary>
/// Calculates the integral part of a specified double-precision floating-point number.
/// </summary>
/// <param name="number">A number to truncate.</param>
/// <returns>The integral part.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NumberValue Truncate(NumberValue number)
=> new NumberValue(Math.Truncate(number.Value));

/// <summary>
/// Gets a value indicating whether the current value is not a number (NaN).
/// </summary>
Expand All @@ -737,6 +839,11 @@ public static Complex Pow(Complex number, NumberValue power)
/// </summary>
public bool IsNegativeInfinity => double.IsNegativeInfinity(Value);

/// <summary>
/// Gets a sign of number.
/// </summary>
public double Sign => Math.Sign(Value);

/// <summary>
/// Gets a value.
/// </summary>
Expand Down
13 changes: 4 additions & 9 deletions xFunc.Maths/Expressions/Round.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using xFunc.Maths.Analyzers;
using xFunc.Maths.Resources;

namespace xFunc.Maths.Expressions
{
Expand Down Expand Up @@ -61,15 +60,11 @@ public override object Execute(ExpressionParameters? parameters)
var result = Argument.Execute(parameters);
var digits = Digits?.Execute(parameters) ?? 0.0;

if (result is double arg && digits is double digitsDouble)
return (result, digits) switch
{
if (!digitsDouble.IsInt())
throw new InvalidOperationException(Resource.ValueIsNotInteger);

return Math.Round(arg, (int)digitsDouble, MidpointRounding.AwayFromZero);
}

throw new ResultIsNotSupportedException(this, result);
(NumberValue left, NumberValue right) => NumberValue.Round(left, right),
_ => throw new ResultIsNotSupportedException(this, result, digits),
};
}

/// <inheritdoc />
Expand Down
4 changes: 2 additions & 2 deletions xFunc.Maths/Expressions/Sign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public override object Execute(ExpressionParameters? parameters)

return result switch
{
double number => (double)Math.Sign(number),
AngleValue angle => (double)angle.Sign,
NumberValue number => number.Sign,
AngleValue angle => angle.Sign,
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
3 changes: 1 addition & 2 deletions xFunc.Maths/Expressions/Sqrt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public override object Execute(ExpressionParameters? parameters)

return result switch
{
double number when number < 0 => Complex.Sqrt(number),
double number => Math.Sqrt(number),
NumberValue number => NumberValue.Sqrt(number),
Complex complex => (object)Complex.Sqrt(complex),
_ => throw new ResultIsNotSupportedException(this, result),
};
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/ToBin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override object Execute(ExpressionParameters? parameters)

return result switch
{
double number => number.ToBin(),
NumberValue number => NumberValue.ToBin(number),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/ToHex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override object Execute(ExpressionParameters? parameters)

return result switch
{
double number => number.ToHex(),
NumberValue number => NumberValue.ToHex(number),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/ToOct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override object Execute(ExpressionParameters? parameters)

return result switch
{
double number => number.ToOct(),
NumberValue number => NumberValue.ToOct(number),
_ => throw new ResultIsNotSupportedException(this, result),
};
}
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/Trunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public override object Execute(ExpressionParameters? parameters)

return result switch
{
double number => Math.Truncate(number),
NumberValue number => NumberValue.Truncate(number),
AngleValue angle => AngleValue.Truncate(angle),
_ => throw new ResultIsNotSupportedException(this, result),
};
Expand Down
2 changes: 1 addition & 1 deletion xFunc.Maths/Expressions/UnaryMinus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override object Execute(ExpressionParameters? parameters)

return result switch
{
double number => -number,
NumberValue number => -number,
AngleValue angle => -angle,
Complex complex => (object)Complex.Negate(complex),
_ => throw new ResultIsNotSupportedException(this, result),
Expand Down
54 changes: 0 additions & 54 deletions xFunc.Maths/MathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,60 +203,6 @@ public static double Acsc(double d)
public static double Acsch(double d)
=> Math.Log(1 / d + Math.Sqrt(1 / d * d + 1));

private static string PadNumber(string number, int padding)
{
var padLength = number.Length % padding;
if (padLength > 0)
padLength = number.Length + (padding - padLength);

return number.PadLeft(padLength, '0');
}

/// <summary>
/// Converts <paramref name="number"/> to the binary number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>String that contains the number in the new numeral system.</returns>
public static string ToBin(this double number)
{
if (!number.IsInt())
throw new ArgumentException(Resource.ValueIsNotInteger, nameof(number));

var result = Convert.ToString((int)number, 2);
result = PadNumber(result, 8);

return $"0b{result}";
}

/// <summary>
/// Converts <paramref name="number"/> to the octal number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>String that contains the number in the new numeral system.</returns>
public static string ToOct(this double number)
{
if (!number.IsInt())
throw new ArgumentException(Resource.ValueIsNotInteger, nameof(number));

return $"0{Convert.ToString((int)number, 8)}";
}

/// <summary>
/// Converts <paramref name="number"/> to the hexadecimal number.
/// </summary>
/// <param name="number">The number.</param>
/// <returns>String that contains the number in the new numeral system.</returns>
public static string ToHex(this double number)
{
if (!number.IsInt())
throw new ArgumentException(Resource.ValueIsNotInteger, nameof(number));

var result = Convert.ToString((int)number, 16).ToUpperInvariant();
result = PadNumber(result, 2);

return $"0x{result}";
}

/// <summary>
/// Formats a complex number.
/// </summary>
Expand Down

0 comments on commit deb6e5a

Please sign in to comment.