Fixed twos-complement issues.
This commit is contained in:
parent
7c0b5020f6
commit
e1c8b99307
@ -42,16 +42,11 @@ export class NumberCoder extends Coder {
|
|||||||
if (value > bounds || value < -(bounds + BN_1)) {
|
if (value > bounds || value < -(bounds + BN_1)) {
|
||||||
this._throwError("value out-of-bounds", _value);
|
this._throwError("value out-of-bounds", _value);
|
||||||
}
|
}
|
||||||
|
value = toTwos(value, 8 * WordSize);
|
||||||
} else if (value < BN_0 || value > mask(maxUintValue, this.size * 8)) {
|
} else if (value < BN_0 || value > mask(maxUintValue, this.size * 8)) {
|
||||||
this._throwError("value out-of-bounds", _value);
|
this._throwError("value out-of-bounds", _value);
|
||||||
}
|
}
|
||||||
|
|
||||||
value = mask(toTwos(value, this.size * 8), this.size * 8);
|
|
||||||
|
|
||||||
if (this.signed) {
|
|
||||||
value = toTwos(fromTwos(value, this.size * 8), 8 * WordSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
return writer.writeValue(value);
|
return writer.writeValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,13 +29,14 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
|
|||||||
|
|
||||||
let match = type.match(regexNumber);
|
let match = type.match(regexNumber);
|
||||||
if (match) {
|
if (match) {
|
||||||
|
let signed = (match[1] === "int");
|
||||||
let size = parseInt(match[2] || "256")
|
let size = parseInt(match[2] || "256")
|
||||||
|
|
||||||
assertArgument((!match[2] || match[2] === String(size)) && (size % 8 === 0) && size !== 0 && size <= 256, "invalid number type", "type", type);
|
assertArgument((!match[2] || match[2] === String(size)) && (size % 8 === 0) && size !== 0 && size <= 256, "invalid number type", "type", type);
|
||||||
|
|
||||||
if (isArray) { size = 256; }
|
if (isArray) { size = 256; }
|
||||||
|
|
||||||
value = toTwos(value, size);
|
if (signed) { value = toTwos(value, size); }
|
||||||
|
|
||||||
return getBytes(zeroPadValue(toArray(value), size / 8));
|
return getBytes(zeroPadValue(toArray(value), size / 8));
|
||||||
}
|
}
|
||||||
|
@ -162,10 +162,10 @@ function toString(val: bigint, decimals: number) {
|
|||||||
let str = val.toString();
|
let str = val.toString();
|
||||||
|
|
||||||
// No decimal point for whole values
|
// No decimal point for whole values
|
||||||
if (decimals === 0) { return str; }
|
if (decimals === 0) { return (negative + str); }
|
||||||
|
|
||||||
// Pad out to the whole component
|
// Pad out to the whole component (including a whole digit)
|
||||||
while (str.length < decimals) { str = Zeros + str; }
|
while (str.length <= decimals) { str = Zeros + str; }
|
||||||
|
|
||||||
// Insert the decimal point
|
// Insert the decimal point
|
||||||
const index = str.length - decimals;
|
const index = str.length - decimals;
|
||||||
|
@ -52,7 +52,7 @@ export function formatUnits(value: BigNumberish, unit?: string | Numeric): strin
|
|||||||
decimals = getNumber(unit, "unit");
|
decimals = getNumber(unit, "unit");
|
||||||
}
|
}
|
||||||
|
|
||||||
return FixedNumber.fromValue(value, decimals, "fixed256x80").toString();
|
return FixedNumber.fromValue(value, decimals, { decimals }).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user