Skip to content

Commit

Permalink
[fix] some Carry & Fix Float bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Nov 12, 2021
1 parent 26fafa2 commit ab712ae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-utility",
"version": "2.9.4",
"version": "2.9.5",
"license": "LGPL-3.0",
"author": "[email protected]",
"description": "Web front-end toolkit based on TypeScript",
Expand Down
21 changes: 13 additions & 8 deletions source/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@ export function hypotenuseOf(...data: number[]) {
}

export function carryFloat(raw: number, length: number) {
const text = raw + '';
const text = raw.toFixed(length + 2);
const offset = text.indexOf('.') + length + 1;

if (!text.slice(offset)) return text;
const cut = (text: string) => text.slice(0, offset - (length ? 0 : 1));

const value = `${raw + 10 ** -length}`;
if (!+text[offset]) return cut(text);

return value.slice(0, offset).replace(/\.$/, '');
const result = cut((+cut(text) + 10 ** -length).toFixed(length));

return result.includes('.') ? result.padEnd(offset, '0') : result;
}

export function fixFloat(raw: number, length = 2) {
const text = raw + '';
const text = raw.toFixed(length + 2);
const floatOffset = text.indexOf('.');

if (floatOffset < 0)
return [text, '0'.repeat(length)].filter(Boolean).join('.');
if (floatOffset < 0) return length ? `${text}.${'0'.repeat(length)}` : text;

const offset = floatOffset + length + 1;

Expand All @@ -47,5 +48,9 @@ export function fixFloat(raw: number, length = 2) {

const carry = anchor > 5 || (anchor === 5 && (!!after || !!(before % 2)));

return carry ? carryFloat(raw, length) : text.slice(0, offset);
if (carry) return carryFloat(raw, length);

const result = text.slice(0, offset - (length ? 0 : 1));

return result.includes('.') ? result.padEnd(offset, '0') : result;
}
9 changes: 8 additions & 1 deletion test/math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ describe('Math functions', () => {
it('should carry rest bits of a Float Number based on length', () => {
expect(carryFloat(0.01, 1)).toBe('0.1');
expect(carryFloat(0.01, 2)).toBe('0.01');
expect(carryFloat(1.01, 0)).toBe('2');
expect(carryFloat(1.01, 0)).toBe('1');
expect(carryFloat(0.03001, 3)).toBe('0.030');
expect(carryFloat(0.049999999999999996, 3)).toBe('0.050');
expect(carryFloat(1573.1666666666667, 1)).toBe('1573.2');
expect(carryFloat(7.527726527090811e-7, 7)).toBe('0.0000008');
});

it('should fix a Float Number with Banker Rounding Algorithm', () => {
expect(fixFloat(89, 0)).toBe('89');
expect(fixFloat(89, 1)).toBe('89.0');
expect(fixFloat(89.5, 0)).toBe('89');
expect(fixFloat(89.64, 0)).toBe('90');
expect(fixFloat(89.64, 1)).toBe('89.6');

expect(fixFloat(0.8964, 5)).toBe('0.89640');
expect(fixFloat(0.8964, 3)).toBe('0.896');
expect(fixFloat(0.8966, 3)).toBe('0.897');
expect(fixFloat(0.8965, 3)).toBe('0.896');
Expand Down

0 comments on commit ab712ae

Please sign in to comment.