@gkucmierz/utils 1.12.1 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.12.1",
3
+ "version": "1.13.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@gkucmierz/utils",
9
- "version": "1.12.1",
9
+ "version": "1.13.0",
10
10
  "license": "MIT",
11
11
  "devDependencies": {
12
12
  "husky": "^8.0.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.12.1",
3
+ "version": "1.13.0",
4
4
  "description": "Usefull functions for solving programming tasks",
5
5
  "main": "src/main.mjs",
6
6
  "scripts": {
@@ -0,0 +1,41 @@
1
+
2
+ import {
3
+ heronsFormula,
4
+ heronsFormulaBI,
5
+ } from '../src/herons-formula.mjs';
6
+
7
+ describe('herons-formula', () => {
8
+ it('Integer', () => {
9
+ expect(heronsFormula(5, 12, 13)).toEqual(30);
10
+ expect(heronsFormula(6, 8, 10)).toEqual(24);
11
+ expect(heronsFormula(7, 15, 20)).toEqual(42);
12
+ expect(heronsFormula(17, 17, 30)).toEqual(120);
13
+ expect(heronsFormula(13, 37, 30)).toEqual(180);
14
+ expect(heronsFormula(6, 25, 29)).toEqual(60);
15
+ expect(heronsFormula(73, 9, 80)).toEqual(216);
16
+ expect(heronsFormula(12, 35, 37)).toEqual(210);
17
+ expect(heronsFormula(120, 109, 13)).toEqual(396);
18
+ expect(heronsFormula(9, 10, 17)).toEqual(36);
19
+ });
20
+
21
+ it('Float', () => {
22
+ expect(heronsFormula(2, 3, 4)).toEqual(2.9047375096555625);
23
+ expect(heronsFormula(7, 10, 12)).toEqual(34.977671449083054);
24
+ expect(heronsFormula(6, 11, 12)).toEqual(32.839572165300815);
25
+ expect(heronsFormula(25, 25, 45)).toEqual(245.1880655741629);
26
+ expect(heronsFormula(10, 11, 18)).toEqual(48.59976851796724);
27
+ });
28
+
29
+ it('BigInt', () => {
30
+ expect(heronsFormulaBI(5n, 12n, 13n)).toEqual(30n);
31
+ expect(heronsFormulaBI(6n, 8n, 10n)).toEqual(24n);
32
+ expect(heronsFormulaBI(7n, 15n, 20n)).toEqual(42n);
33
+ expect(heronsFormulaBI(17n, 17n, 30n)).toEqual(120n);
34
+ expect(heronsFormulaBI(13n, 37n, 30n)).toEqual(180n);
35
+ expect(heronsFormulaBI(6n, 25n, 29n)).toEqual(60n);
36
+ expect(heronsFormulaBI(73n, 9n, 80n)).toEqual(216n);
37
+ expect(heronsFormulaBI(12n, 35n, 37n)).toEqual(210n);
38
+ expect(heronsFormulaBI(120n, 109n, 13n)).toEqual(396n);
39
+ expect(heronsFormulaBI(9n, 10n, 17n)).toEqual(36n);
40
+ });
41
+ });
@@ -0,0 +1,13 @@
1
+
2
+ import {
3
+ squareRootBI,
4
+ } from './square-root.mjs';
5
+
6
+ // calculate triangle area given 3 sides
7
+
8
+ const getHeronsFormula = (four, sq) => (a, b, c) => {
9
+ return sq((a + b + c) * (-a + b + c) * (a - b + c) * (a + b - c)) / four;
10
+ };
11
+
12
+ export const heronsFormula = getHeronsFormula(4, n => n ** 0.5);
13
+ export const heronsFormulaBI = getHeronsFormula(4n, squareRootBI);
@@ -0,0 +1,15 @@
1
+
2
+
3
+ export const squareRoot = n => n ** 0.5;
4
+
5
+ // Newrton's formula
6
+ export const squareRootBI = n => {
7
+ let res = BigInt(n.toString().substr(0, n.toString().length / 2));
8
+ let last;
9
+ while (res * res - n !== 0n) {
10
+ last = res;
11
+ res = (res + n / res) / 2n;
12
+ if (res === last) return res;
13
+ }
14
+ return res;
15
+ };