numbers in scarpet better handle long type values.
If possible, numbers that can be represented as longs (seeds, etc) will be handled as longs but they will loose their longinness with a single arithmetic operation.
This commit is contained in:
1 parent
bc9f3e89e0
commit
968ef0cd9f
8 files changed
+87
-24
No files matched your search
@@ -120,7 +120,7 @@ public class FunctionValue extends Value implements Fluff.ILazyFunction
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readNumber()
|
||||
public double readDoubleNumber()
|
||||
{
|
||||
throw new InternalExpressionException("Function value has no numeric value");
|
||||
}
|
||||
|
||||
@@ -352,7 +352,7 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readNumber()
|
||||
public double readDoubleNumber()
|
||||
{
|
||||
return (double)items.size();
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readNumber()
|
||||
public double readDoubleNumber()
|
||||
{
|
||||
return (double)map.size();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import static java.lang.Math.abs;
|
||||
public class NumericValue extends Value
|
||||
{
|
||||
private Double value;
|
||||
private Long longValue;
|
||||
final static double epsilon = 1024*Double.MIN_VALUE;
|
||||
|
||||
public static NumericValue asNumber(Value v1, String id)
|
||||
@@ -35,6 +36,10 @@ public class NumericValue extends Value
|
||||
@Override
|
||||
public String getString()
|
||||
{
|
||||
if (longValue != null)
|
||||
{
|
||||
return Long.toString(getLong());
|
||||
}
|
||||
try
|
||||
{
|
||||
return BigDecimal.valueOf(value).stripTrailingZeros().toPlainString();
|
||||
@@ -48,7 +53,8 @@ public class NumericValue extends Value
|
||||
@Override
|
||||
public String getPrettyString()
|
||||
{
|
||||
if (getDouble() == (double)getLong())
|
||||
|
||||
if (longValue!= null || getDouble() == (double)getLong())
|
||||
{
|
||||
return Long.toString(getLong());
|
||||
}
|
||||
@@ -72,13 +78,14 @@ public class NumericValue extends Value
|
||||
return value.floatValue();
|
||||
}
|
||||
|
||||
public static long floor(double double_1) {
|
||||
private static long floor(double double_1) {
|
||||
long int_1 = (long)double_1;
|
||||
return double_1 < (double)int_1 ? int_1 - 1 : int_1;
|
||||
}
|
||||
|
||||
public long getLong()
|
||||
{
|
||||
if (longValue != null) return longValue;
|
||||
return floor((value+epsilon));
|
||||
}
|
||||
|
||||
@@ -119,12 +126,10 @@ public class NumericValue extends Value
|
||||
return super.divide(v);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Value clone()
|
||||
{
|
||||
return new NumericValue(value);
|
||||
return new NumericValue(value, longValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -158,12 +163,28 @@ public class NumericValue extends Value
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
private NumericValue(double value, Long longValue)
|
||||
{
|
||||
this.value = value;
|
||||
this.longValue = longValue;
|
||||
}
|
||||
|
||||
public NumericValue(String value)
|
||||
{
|
||||
this(new BigDecimal(value).doubleValue());
|
||||
BigDecimal decimal = new BigDecimal(value);
|
||||
if (decimal.stripTrailingZeros().scale() <= 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
longValue = decimal.longValueExact();
|
||||
}
|
||||
catch (ArithmeticException ignored) {}
|
||||
}
|
||||
this.value = decimal.doubleValue();
|
||||
}
|
||||
public NumericValue(long value)
|
||||
{
|
||||
this.longValue = value;
|
||||
this.value = (double)value;
|
||||
}
|
||||
public NumericValue(boolean boolval)
|
||||
@@ -178,7 +199,7 @@ public class NumericValue extends Value
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readNumber()
|
||||
public double readDoubleNumber()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
@@ -212,6 +233,8 @@ public class NumericValue extends Value
|
||||
@Override
|
||||
public Tag toTag(boolean force)
|
||||
{
|
||||
if (longValue != null)
|
||||
return LongTag.of(longValue);
|
||||
long longValue = getLong();
|
||||
if (value == (double)longValue)
|
||||
{
|
||||
|
||||
@@ -161,7 +161,7 @@ public abstract class Value implements Comparable<Value>, Cloneable
|
||||
return new StringValue(value.substring((int)from, (int)to));
|
||||
return new StringValue(value.substring((int)from));
|
||||
}
|
||||
public double readNumber()
|
||||
public double readDoubleNumber()
|
||||
{
|
||||
String s = getString();
|
||||
try
|
||||
@@ -176,7 +176,7 @@ public abstract class Value implements Comparable<Value>, Cloneable
|
||||
|
||||
public long readInteger()
|
||||
{
|
||||
return (long)readNumber();
|
||||
return (long) readDoubleNumber();
|
||||
}
|
||||
|
||||
public String getTypeString()
|
||||
|
||||
Reference in new issue
Block a user