Skip to content

Commit

Permalink
[add] Carry Float method
Browse files Browse the repository at this point in the history
[fix] Integer error of Fix Float method
[optimize] update Upstream packages
  • Loading branch information
TechQuery committed Nov 10, 2021
1 parent cf16976 commit 26fafa2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-utility",
"version": "2.9.1",
"version": "2.9.4",
"license": "LGPL-3.0",
"author": "[email protected]",
"description": "Web front-end toolkit based on TypeScript",
Expand Down Expand Up @@ -28,20 +28,20 @@
"regenerator-runtime": "^0.13.9"
},
"devDependencies": {
"@parcel/packager-ts": "^2.0.0",
"@parcel/transformer-typescript-types": "^2.0.0",
"@peculiar/webcrypto": "^1.2.0",
"@parcel/packager-ts": "^2.0.1",
"@parcel/transformer-typescript-types": "^2.0.1",
"@peculiar/webcrypto": "^1.2.2",
"@types/jest": "^27.0.2",
"@types/node": "^14.17.32",
"@types/node": "^14.17.33",
"husky": "^7.0.4",
"intersection-observer": "^0.12.0",
"jest": "^27.3.1",
"lint-staged": "^11.2.6",
"open-cli": "^7.0.1",
"parcel": "^2.0.0",
"parcel": "^2.0.1",
"prettier": "^2.4.1",
"ts-jest": "^27.0.7",
"typedoc": "^0.22.7",
"typedoc": "^0.22.8",
"typedoc-plugin-mdn-links": "^1.0.4",
"typescript": "~4.3.5"
},
Expand Down
24 changes: 21 additions & 3 deletions source/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,31 @@ export function hypotenuseOf(...data: number[]) {
return Math.sqrt(sum(...data.map(item => item ** 2)));
}

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

if (!text.slice(offset)) return text;

const value = `${raw + 10 ** -length}`;

return value.slice(0, offset).replace(/\.$/, '');
}

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

if (floatOffset < 0)
return [text, '0'.repeat(length)].filter(Boolean).join('.');

const offset = floatOffset + length + 1;

const before = +text[offset - 1],
anchor = +text[offset],
after = +text[offset + 1];
const plus = anchor > 5 || (anchor === 5 && (!!after || !!(before % 2)));

return +text.slice(0, offset) + (plus ? 10 ** -length : 0);
const carry = anchor > 5 || (anchor === 5 && (!!after || !!(before % 2)));

return carry ? carryFloat(raw, length) : text.slice(0, offset);
}
23 changes: 17 additions & 6 deletions test/math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
varianceOf,
standardDeviationOf,
hypotenuseOf,
carryFloat,
fixFloat
} from '../source/math';

Expand Down Expand Up @@ -34,12 +35,22 @@ describe('Math functions', () => {
expect(hypotenuseOf(1, 2, 3).toFixed(3)).toBe('3.742');
});

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');
});

it('should fix a Float Number with Banker Rounding Algorithm', () => {
expect(fixFloat(0.8964, 3)).toBe(0.896);
expect(fixFloat(0.8966, 3)).toBe(0.897);
expect(fixFloat(0.8965, 3)).toBe(0.896);
expect(fixFloat(0.8955, 3)).toBe(0.896);
expect(fixFloat(0.89651, 3)).toBe(0.897);
expect(fixFloat(0.89551, 3)).toBe(0.896);
expect(fixFloat(89, 0)).toBe('89');
expect(fixFloat(89, 1)).toBe('89.0');
expect(fixFloat(89.64, 0)).toBe('90');
expect(fixFloat(89.64, 1)).toBe('89.6');
expect(fixFloat(0.8964, 3)).toBe('0.896');
expect(fixFloat(0.8966, 3)).toBe('0.897');
expect(fixFloat(0.8965, 3)).toBe('0.896');
expect(fixFloat(0.8955, 3)).toBe('0.896');
expect(fixFloat(0.89651, 3)).toBe('0.897');
expect(fixFloat(0.89551, 3)).toBe('0.896');
});
});

0 comments on commit 26fafa2

Please sign in to comment.