util: add util.fromDouble for convenience.
This commit is contained in:
parent
9f3a040758
commit
fbd99ea1b9
@ -416,7 +416,7 @@ Config.prototype.fixed = function fixed(key, exp, fallback) {
|
||||
return fallback;
|
||||
|
||||
try {
|
||||
return util.fromFixed(value.toString(10), exp || 0);
|
||||
return util.fromDouble(value, exp || 0);
|
||||
} catch (e) {
|
||||
throw new Error(`${fmt(key)} must be a fixed number.`);
|
||||
}
|
||||
|
||||
@ -863,6 +863,20 @@ util.toFixed = function toFixed(num, exp) {
|
||||
return `${sign}${hi}.${lo}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse a double float number and multiply by a
|
||||
* power of ten (uses no floating point arithmetic).
|
||||
* @param {Number} num
|
||||
* @param {Number} exp - Number of decimal places.
|
||||
* @returns {Number}
|
||||
*/
|
||||
|
||||
util.fromDouble = function fromDouble(num, exp) {
|
||||
assert(typeof num === 'number' && isFinite(num));
|
||||
assert(Number.isSafeInteger(exp));
|
||||
return util.fromFixed(num.toFixed(exp), exp);
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse a fixed number string and multiply by a
|
||||
* power of ten (uses no floating point arithmetic).
|
||||
|
||||
@ -273,7 +273,7 @@ Validator.prototype.fixed = function fixed(key, exp, fallback) {
|
||||
return fallback;
|
||||
|
||||
try {
|
||||
return util.fromFixed(value.toString(10), exp || 0);
|
||||
return util.fromDouble(value, exp || 0);
|
||||
} catch (e) {
|
||||
throw new ValidationError(key, 'fixed number');
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ const Script = require('../lib/script/script');
|
||||
const Witness = require('../lib/script/witness');
|
||||
const Stack = require('../lib/script/stack');
|
||||
const TX = require('../lib/primitives/tx');
|
||||
const util = require('../lib/utils/util');
|
||||
const encoding = require('../lib/utils/encoding');
|
||||
const opcodes = Script.opcodes;
|
||||
|
||||
@ -39,9 +40,9 @@ function parseScriptTest(data) {
|
||||
|
||||
comments += ` (${expected})`;
|
||||
|
||||
let amount = 0;
|
||||
if (witArr.length !== 0)
|
||||
amount = witArr.pop() * 1e8;
|
||||
let value = 0;
|
||||
if (witArr.length > 0)
|
||||
value = util.fromDouble(witArr.pop(), 8);
|
||||
|
||||
const witness = Witness.fromString(witArr);
|
||||
const input = Script.fromString(inpHex);
|
||||
@ -58,7 +59,7 @@ function parseScriptTest(data) {
|
||||
witness: witness,
|
||||
input: input,
|
||||
output: output,
|
||||
amount: amount,
|
||||
value: value,
|
||||
flags: flags,
|
||||
expected: expected,
|
||||
comments: comments
|
||||
@ -279,7 +280,7 @@ describe('Script', function() {
|
||||
|
||||
const test = parseScriptTest(data);
|
||||
const {witness, input, output} = test;
|
||||
const {amount, flags} = test;
|
||||
const {value, flags} = test;
|
||||
const {expected, comments} = test;
|
||||
|
||||
for (const noCache of [false, true]) {
|
||||
@ -300,7 +301,7 @@ describe('Script', function() {
|
||||
}],
|
||||
outputs: [{
|
||||
script: output,
|
||||
value: amount
|
||||
value: value
|
||||
}],
|
||||
locktime: 0
|
||||
});
|
||||
@ -319,7 +320,7 @@ describe('Script', function() {
|
||||
}],
|
||||
outputs: [{
|
||||
script: [],
|
||||
value: amount
|
||||
value: value
|
||||
}],
|
||||
locktime: 0
|
||||
});
|
||||
@ -331,7 +332,7 @@ describe('Script', function() {
|
||||
|
||||
let err, res;
|
||||
try {
|
||||
res = Script.verify(input, witness, output, tx, 0, amount, flags);
|
||||
res = Script.verify(input, witness, output, tx, 0, value, flags);
|
||||
} catch (e) {
|
||||
err = e;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user