Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
CMCDragonkai committed Oct 21, 2021
1 parent ef0c2da commit 4c1c8e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,17 @@ function toFixedPoint(
} else {
fractional = roundPrecise(floating % 1, precision);
}
// if the fractional is rounded to 1
// If the fractional is rounded to 1
// then it should be added to the integer
if (fractional === 1) {
integer += fractional;
fractional = 0;
}
// floor is used to round down to a number that can be represented by the bit size
// Floor is used to round down to a number that can be represented by the bit size
// if ceil or round was used, it's possible to return a number that would overflow the bit size
// for example if 12 bits is used, then 4096 would overflow to all zeros
// the maximum for 12 bit is 4095
const fractionalFixed = Math.floor(fractional * (2 ** size));
const fractionalFixed = Math.floor(fractional * 2 ** size);
return [integer, fractionalFixed];
}

Expand All @@ -272,7 +272,7 @@ function fromFixedPoint(
if (precision == null) {
fractional = fractionalFixed / 2 ** size;
} else {
fractional = roundPrecise(fractionalFixed / (2 ** size), precision);
fractional = roundPrecise(fractionalFixed / 2 ** size, precision);
}
return integer + fractional;
}
Expand Down
14 changes: 9 additions & 5 deletions tests/IdSortable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ describe('IdSortable', () => {
}
});
test('encoded multibase strings may be lexically sortable (base58btc)', () => {
// base58btc's alphabet preserves sort order
// Base58btc's alphabet preserves sort order
const idGen = new IdSortable();
// This generating over 100,000 ids and checks that they maintain
// sort order for each 100 chunk of ids
let count = 1000;
while (count > 0) {
const idStrings = [...utils.take(idGen, 100)].map((id) => utils.toMultibase(id, 'base58btc'));
const idStrings = [...utils.take(idGen, 100)].map((id) =>
utils.toMultibase(id, 'base58btc'),
);
const idStringsShuffled = idStrings.slice();
shuffle(idStringsShuffled);
idStringsShuffled.sort();
Expand All @@ -117,13 +119,15 @@ describe('IdSortable', () => {
}
});
test('encoded multibase strings may not be lexically sortable (base64)', async () => {
// base64's alphabet does not preserve sort order
// Base64's alphabet does not preserve sort order
const idGen = new IdSortable();
const idStrings = [...utils.take(idGen, 100)].map((id) => utils.toMultibase(id, 'base64'));
const idStrings = [...utils.take(idGen, 100)].map((id) =>
utils.toMultibase(id, 'base64'),
);
const idStringsShuffled = idStrings.slice();
shuffle(idStringsShuffled);
idStringsShuffled.sort();
// it will not equal
// It will not equal
expect(idStringsShuffled).not.toStrictEqual(idStrings);
});
test('ids are monotonic within the same timestamp', () => {
Expand Down
28 changes: 14 additions & 14 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,39 +100,39 @@ describe('utils', () => {
expect(utils.fromFixedPoint([0, 0], 12, 3)).toBe(0.0);
});
test('fixed point conversion when close to 1', () => {
// highest number 12 digits can represent is 4095
// Highest number 12 digits can represent is 4095
// a number of 4096 would result in overflow
// here we test when we get close to 4096
// the conversion from toFixedPoint and fromFixedPoint will be lossy
// this is because toFixedPoint will round off precision and floor any number getting close to 4096
let closeTo: number;
let fp: [number, number];
// exactly at 3 decimal points
// Exactly at 3 decimal points
closeTo = 0.999;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([0, 4091]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.999);
// will round below
// Will round below
closeTo = 0.9994;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([0, 4091]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.999);
// will round above to 1
// Will round above to 1
closeTo = 0.9995;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([1, 0]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(1);
// will round above to 1
// Will round above to 1
closeTo = 0.9999;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([1, 0]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(1);
// will round above to 1
// Will round above to 1
closeTo = 0.99999;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([1, 0]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(1);
// exactly at 5 decimal points
// Exactly at 5 decimal points
closeTo = 0.99999;
fp = utils.toFixedPoint(closeTo, 12, 5);
expect(fp).toStrictEqual([0, 4095]);
Expand All @@ -141,37 +141,37 @@ describe('utils', () => {
test('fixed point conversion when close to 0', () => {
let closeTo: number;
let fp: [number, number];
// exactly 3 decimal places
// Exactly 3 decimal places
closeTo = 0.001;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([0, 4]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.001);
// will round to 0
// Will round to 0
closeTo = 0.0001;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([0, 0]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0);
// will round to 0
// Will round to 0
closeTo = 0.0004;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([0, 0]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0);
// will round to 0.001
// Will round to 0.001
closeTo = 0.0005;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([0, 4]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.001);
// will round to 0.001
// Will round to 0.001
closeTo = 0.00055;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([0, 4]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.001);
// will round to 0.001
// Will round to 0.001
closeTo = 0.0009;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([0, 4]);
expect(utils.fromFixedPoint(fp, 12, 3)).toBe(0.001);
// will round to 0.002
// Will round to 0.002
closeTo = 0.0015;
fp = utils.toFixedPoint(closeTo, 12, 3);
expect(fp).toStrictEqual([0, 8]);
Expand Down

0 comments on commit 4c1c8e6

Please sign in to comment.