public class MathHelper {
public static float AbsoluteValue(float Value) {
if (Value < 0)
Value *= -1;
return Value;
}
public static float Hold(float Value, float Minimum, float Maximum) {
if (Value < Minimum)
Value = Minimum;
if (Value > Maximum)
Value = Maximum;
return Value;
}
public static float ToPower(float Base, int Exponent) {
float b = Base;
if (Exponent == 0) { b = 1; }
else if (Exponent > 0) {
for (int i = 0; i < Exponent; i++)
Base *= b;
}
else if (Exponent < 0) {
Exponent *= -1;
for (int i = 1; i < Exponent; i++)
Base *= b;
Base = 1 / Base;
}
return Base;
}
public static float SquareRoot(float Value) {
if (Value == 0) { return 0; }
float v = (Value / 2) + 1;
float v1 = (v + (Value / 2)) / 2;
while (v1 < v) {
v = v1;
v1 = (v + (Value / v)) / 2;
}
return v;
}
public static float[] QuadraticFormula(float a, float b, float c) {
Complex ca = new Complex(a, 0);
Complex cb = new Complex(b, 0);
Complex cc = new Complex(c, 0);
Complex Δ = (cb * cb) - (4 * ca * cc);
Complex r1 = (-cb + Complex.Sqrt(Δ) / (2 * ca));
Complex r2 = (-cb - Complex.Sqrt(Δ) / (2 * ca));
float[] r = new float[2];
if (r1.Imaginary == 0 && r2.Imaginary == 0)
r = new float[]{Convert.ToSingle(r1.Real), Convert.ToSingle(r2.Real)};
else if (r1.Imaginary == 0)
r = new float[]{Convert.ToSingle(r1.Real), Convert.ToSingle(r2.Imaginary)};
else if (r2.Imaginary == 0)
r = new float[]{Convert.ToSingle(r1.Imaginary), Convert.ToSingle(r2.Real)};
else
r = new float[]{Convert.ToSingle(r1.Imaginary), Convert.ToSingle(r2.Imaginary)};
return r;
}
public static float Square(float Value) { return Value * Value; }
public static float Cube(float Value) { return Square(Value) * Value; }
public static float CubedSquared_Added(float Value) { return Cube(Value) + Square(Value); }
public static float CubedSquared_Subtracted(float Value) { return Cube(Value) - Square(Value); }
public static float CubedSquared_Multiplied(float Value) { return Cube(Value) * Square(Value); }
public static float CubedSquared_Divided(float Value) { return Cube(Value) / Square(Value); }
public static float LowerValue(float Value1, float Value2) {
return Value1 < Value2 ? Value1 : Value2;
}
public static float GreaterValue(float Value1, float Value2) {
return Value1 > Value2 ? Value1 : Value2;
}
public static float PercentIncrease(float OriginalValue, float NewValue) {
return ((NewValue - OriginalValue) / OriginalValue) * 100;
}
public static float PercentDecrease(float OriginalValue, float NewValue) {
return ((OriginalValue - NewValue) / OriginalValue) * 100;
}
}