Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions convex-core/src/main/java/convex/core/cvm/Juice.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import convex.core.data.ADataStructure;
import convex.core.data.Cells;
import convex.core.data.prim.ANumeric;
import convex.core.data.prim.CVMBigDecimal;
import convex.core.data.prim.CVMBigInteger;
import convex.core.data.prim.CVMDouble;

Expand Down Expand Up @@ -517,6 +518,10 @@ public static long costNumeric(ACell a) {
long bl=((CVMBigInteger)a).byteLength();
return Math.max(MIN_NUMERIC_COST,bl);
}
if (a instanceof CVMBigDecimal) {
long bl=((CVMBigDecimal)a).getDecimal().unscaledValue().bitLength()/8+1;
return Math.max(MIN_NUMERIC_COST,bl);
}
return MIN_NUMERIC_COST;
}

Expand Down
3 changes: 3 additions & 0 deletions convex-core/src/main/java/convex/core/cvm/Symbols.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ public class Symbols {
public static final Symbol ACTOR_Q = intern("actor?");

public static final Symbol ZERO_Q = intern("zero?");

public static final Symbol BIGDECIMAL = intern("bigdecimal");
public static final Symbol BIGDECIMAL_Q = intern("bigdecimal?");

public static final Symbol CONTAINS_KEY_Q = intern("contains-key?");

Expand Down
15 changes: 15 additions & 0 deletions convex-core/src/main/java/convex/core/data/CAD3Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,21 @@ private ANumeric readNumeric(byte tag, DecodeState ds) throws BadFormatException
ds.pos+=8;
return CVMDouble.unsafeCreate(Double.longBitsToDouble(bits));
}
// CVMBigDecimal: tag 0x1a, VLQ signed scale + VLQ byte count + raw unscaled bytes
if (tag == Tag.BIG_DECIMAL) {
long scale=Format.readVLQLong(ds.data, ds.pos);
ds.pos+=Format.getVLQLongLength(scale);
long bc=Format.readVLQCount(ds.data, ds.pos);
ds.pos+=Format.getVLQCountLength(bc);
if (bc>Constants.MAX_BIG_INTEGER_LENGTH) throw new BadFormatException("Encoding exceeds max big decimal unscaled length");
Blob blobData=Blob.wrap(ds.data, ds.pos, (int)bc);
ds.pos+=(int)bc;
java.math.BigInteger unscaled=new java.math.BigInteger(blobData.toByteArray());
java.math.BigDecimal bd=new java.math.BigDecimal(unscaled, (int)scale);
CVMBigDecimal result=CVMBigDecimal.create(bd);
if (result==null) throw new BadFormatException("Illegal creation of BigDecimal");
return result;
}
throw new BadFormatException(ErrorMessages.badTagMessage(tag));
}

Expand Down
1 change: 1 addition & 0 deletions convex-core/src/main/java/convex/core/data/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class Tag {

public static final byte INTEGER = (byte) 0x10; // Arbitrary length integer base
public static final byte BIG_INTEGER = (byte) 0x19; // Big integer (greater than long range)
public static final byte BIG_DECIMAL = (byte) 0x1a; // Arbitrary precision decimal
public static final byte DOUBLE = (byte) 0x1d;

// =========================================
Expand Down
Loading