Fix multiplication in multiplier
This commit is contained in:
@@ -23,6 +23,24 @@ namespace TerraTech {
|
|||||||
throw new ArgumentException(
|
throw new ArgumentException(
|
||||||
string.Format("Field {0} does not exist on {1}", fieldName, parentTraverse));
|
string.Format("Field {0} does not exist on {1}", fieldName, parentTraverse));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify TValue is a numeric type
|
||||||
|
if (!IsNumericType(typeof(TValue))) {
|
||||||
|
throw new ArgumentException(string.Format("Type {0} must be a numeric type", typeof(TValue).Name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsNumericType(Type type) {
|
||||||
|
return type == typeof(byte) || type == typeof(sbyte) || type == typeof(short) || type == typeof(ushort) ||
|
||||||
|
type == typeof(int) || type == typeof(uint) || type == typeof(long) || type == typeof(ulong) ||
|
||||||
|
type == typeof(float) || type == typeof(double) || type == typeof(decimal);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TValue MultiplyValues(TValue a, TValue b) {
|
||||||
|
// Convert to double for the multiplication
|
||||||
|
double result = Convert.ToDouble(a) * Convert.ToDouble(b);
|
||||||
|
// Convert back to TValue
|
||||||
|
return (TValue)Convert.ChangeType(result, typeof(TValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
public TValue GetValue() {
|
public TValue GetValue() {
|
||||||
@@ -48,9 +66,7 @@ namespace TerraTech {
|
|||||||
|
|
||||||
public void Apply() {
|
public void Apply() {
|
||||||
try {
|
try {
|
||||||
dynamic originalDynamic = _originalValue;
|
var newValue = MultiplyValues(_originalValue, _multiplier.Value);
|
||||||
dynamic multiplierDynamic = _multiplier.Value;
|
|
||||||
var newValue = (TValue)(originalDynamic * multiplierDynamic);
|
|
||||||
|
|
||||||
if (Main.debug.Value)
|
if (Main.debug.Value)
|
||||||
Console.WriteLine("Applying to {0}: {1} * {2} = {3}", _fieldName, _originalValue, _multiplier.Value,
|
Console.WriteLine("Applying to {0}: {1} * {2} = {3}", _fieldName, _originalValue, _multiplier.Value,
|
||||||
|
Reference in New Issue
Block a user