make fractional base a constant

This commit is contained in:
Florian Dold 2016-11-16 10:29:07 +01:00
parent 665c1d8072
commit 82e401a773
3 changed files with 16 additions and 14 deletions

View File

@ -23,7 +23,7 @@
/// <reference path="../decl/urijs/URIjs.d.ts" /> /// <reference path="../decl/urijs/URIjs.d.ts" />
import {AmountJson} from "./types"; import {AmountJson, Amounts} from "./types";
import URI = uri.URI; import URI = uri.URI;
export function substituteFulfillmentUrl(url: string, vars: any) { export function substituteFulfillmentUrl(url: string, vars: any) {
@ -34,7 +34,7 @@ export function substituteFulfillmentUrl(url: string, vars: any) {
export function amountToPretty(amount: AmountJson): string { export function amountToPretty(amount: AmountJson): string {
let x = amount.value + amount.fraction / 1e6; let x = amount.value + amount.fraction / Amounts.fractionalBase;
return `${x} ${amount.currency}`; return `${x} ${amount.currency}`;
} }
@ -63,7 +63,7 @@ export function parsePrettyAmount(pretty: string): AmountJson|undefined {
} }
return { return {
value: parseInt(res[1], 10), value: parseInt(res[1], 10),
fraction: res[2] ? (parseFloat(`0.${res[2]}`) * 1e-6) : 0, fraction: res[2] ? (parseFloat(`0.${res[2]}`) / Amounts.fractionalBase) : 0,
currency: res[3] currency: res[3]
} }
} }

View File

@ -21,10 +21,10 @@
*/ */
import {AmountJson, Contract} from "./types"; import {AmountJson, Contract, Amounts} from "./types";
export function prettyAmount(amount: AmountJson) { export function prettyAmount(amount: AmountJson) {
let v = amount.value + amount.fraction / 1e6; let v = amount.value + amount.fraction / Amounts.fractionalBase;
return `${v.toFixed(2)} ${amount.currency}`; return `${v.toFixed(2)} ${amount.currency}`;
} }

View File

@ -509,6 +509,8 @@ export type PayCoinInfo = Array<{ updatedCoin: CoinRecord, sig: CoinPaySig }>;
export namespace Amounts { export namespace Amounts {
export const fractionalBase = 1e8;
export interface Result { export interface Result {
amount: AmountJson; amount: AmountJson;
// Was there an over-/underflow? // Was there an over-/underflow?
@ -533,18 +535,18 @@ export namespace Amounts {
export function add(first: AmountJson, ...rest: AmountJson[]): Result { export function add(first: AmountJson, ...rest: AmountJson[]): Result {
let currency = first.currency; let currency = first.currency;
let value = first.value + Math.floor(first.fraction / 1e6); let value = first.value + Math.floor(first.fraction / fractionalBase);
if (value > Number.MAX_SAFE_INTEGER) { if (value > Number.MAX_SAFE_INTEGER) {
return { amount: getMaxAmount(currency), saturated: true }; return { amount: getMaxAmount(currency), saturated: true };
} }
let fraction = first.fraction % 1e6; let fraction = first.fraction % fractionalBase;
for (let x of rest) { for (let x of rest) {
if (x.currency !== currency) { if (x.currency !== currency) {
throw Error(`Mismatched currency: ${x.currency} and ${currency}`); throw Error(`Mismatched currency: ${x.currency} and ${currency}`);
} }
value = value + x.value + Math.floor((fraction + x.fraction) / 1e6); value = value + x.value + Math.floor((fraction + x.fraction) / fractionalBase);
fraction = (fraction + x.fraction) % 1e6; fraction = (fraction + x.fraction) % fractionalBase;
if (value > Number.MAX_SAFE_INTEGER) { if (value > Number.MAX_SAFE_INTEGER) {
return { amount: getMaxAmount(currency), saturated: true }; return { amount: getMaxAmount(currency), saturated: true };
} }
@ -567,7 +569,7 @@ export namespace Amounts {
return { amount: { currency, value: 0, fraction: 0 }, saturated: true }; return { amount: { currency, value: 0, fraction: 0 }, saturated: true };
} }
value--; value--;
fraction += 1e6; fraction += fractionalBase;
} }
console.assert(fraction >= b.fraction); console.assert(fraction >= b.fraction);
fraction -= b.fraction; fraction -= b.fraction;
@ -584,10 +586,10 @@ export namespace Amounts {
if (a.currency !== b.currency) { if (a.currency !== b.currency) {
throw Error(`Mismatched currency: ${a.currency} and ${b.currency}`); throw Error(`Mismatched currency: ${a.currency} and ${b.currency}`);
} }
let av = a.value + Math.floor(a.fraction / 1e6); let av = a.value + Math.floor(a.fraction / fractionalBase);
let af = a.fraction % 1e6; let af = a.fraction % fractionalBase;
let bv = b.value + Math.floor(b.fraction / 1e6); let bv = b.value + Math.floor(b.fraction / fractionalBase);
let bf = b.fraction % 1e6; let bf = b.fraction % fractionalBase;
switch (true) { switch (true) {
case av < bv: case av < bv:
return -1; return -1;