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