math-exercises 1.1.1 → 1.2.1
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.json +1 -1
- package/src/exercises/calcul/addAndSub.js +19 -13
- package/src/exercises/calcul/addAndSub.ts +31 -23
- package/src/exercises/calcul/fractions/fractionAndIntegerDivision.js +35 -0
- package/src/exercises/calcul/fractions/fractionAndIntegerDivision.ts +38 -0
- package/src/exercises/calcul/fractions/fractionAndIntegerProduct.js +31 -0
- package/src/exercises/calcul/fractions/fractionAndIntegerProduct.ts +32 -0
- package/src/exercises/calcul/fractions/fractionAndIntegerSum.js +32 -0
- package/src/exercises/calcul/fractions/fractionAndIntegerSum.ts +31 -0
- package/src/exercises/calcul/fractions/fractionsDivision.js +29 -0
- package/src/exercises/calcul/fractions/fractionsDivision.ts +30 -0
- package/src/exercises/calcul/fractions/fractionsProduct.js +29 -0
- package/src/exercises/calcul/fractions/fractionsProduct.ts +29 -0
- package/src/exercises/calcul/fractions/fractionsSum.js +29 -0
- package/src/exercises/calcul/fractions/fractionsSum.ts +28 -0
- package/src/exercises/calcul/fractions/simplifyFraction.js +25 -0
- package/src/exercises/calcul/fractions/simplifyFraction.ts +24 -0
- package/src/exercises/calcul/operationsPriorities.js +89 -0
- package/src/exercises/calcul/operationsPriorities.ts +118 -0
- package/src/exercises/calcul/rounding/roundToUnit.js +70 -0
- package/src/exercises/calcul/rounding/roundToUnit.ts +68 -0
- package/src/exercises/calculLitteral/distributivity/allIdentities.js +1 -0
- package/src/exercises/calculLitteral/distributivity/allIdentities.ts +1 -0
- package/src/exercises/calculLitteral/distributivity/doubleDistributivity.js +1 -0
- package/src/exercises/calculLitteral/distributivity/doubleDistributivity.ts +2 -1
- package/src/exercises/calculLitteral/distributivity/firstIdentity.js +1 -0
- package/src/exercises/calculLitteral/distributivity/firstIdentity.ts +2 -1
- package/src/exercises/calculLitteral/distributivity/secondIdentity.js +1 -0
- package/src/exercises/calculLitteral/distributivity/secondIdentity.ts +2 -1
- package/src/exercises/calculLitteral/distributivity/simpleDistributivity.js +1 -0
- package/src/exercises/calculLitteral/distributivity/simpleDistributivity.ts +2 -1
- package/src/exercises/calculLitteral/distributivity/thirdIdentity.js +1 -0
- package/src/exercises/calculLitteral/distributivity/thirdIdentity.ts +2 -1
- package/src/exercises/calculLitteral/equation/equationType1Exercise.js +1 -0
- package/src/exercises/calculLitteral/equation/equationType1Exercise.ts +1 -0
- package/src/exercises/calculLitteral/equation/equationType2Exercise.js +1 -0
- package/src/exercises/calculLitteral/equation/equationType2Exercise.ts +1 -0
- package/src/exercises/calculLitteral/equation/equationType3Exercise.js +1 -0
- package/src/exercises/calculLitteral/equation/equationType3Exercise.ts +1 -0
- package/src/exercises/calculLitteral/equation/equationType4Exercise.js +1 -0
- package/src/exercises/calculLitteral/equation/equationType4Exercise.ts +1 -0
- package/src/exercises/calculLitteral/factorisation/factoType1Exercise.js +2 -6
- package/src/exercises/calculLitteral/factorisation/factoType1Exercise.ts +3 -7
- package/src/exercises/calculLitteral/reduction.ts +27 -0
- package/src/exercises/exercise.ts +1 -0
- package/src/exercises/exercises.js +54 -0
- package/src/exercises/exercises.ts +58 -0
- package/src/exercises/powers/powersDivision.js +46 -0
- package/src/exercises/powers/powersDivision.ts +51 -0
- package/src/exercises/powers/powersOfTenToDecimal.js +33 -0
- package/src/exercises/powers/powersOfTenToDecimal.ts +37 -0
- package/src/exercises/powers/powersPower.js +45 -0
- package/src/exercises/powers/powersPower.ts +55 -0
- package/src/exercises/powers/powersProduct.js +46 -0
- package/src/exercises/powers/powersProduct.ts +51 -0
- package/src/exercises/powers/scientificToDecimal.js +38 -0
- package/src/exercises/powers/scientificToDecimal.ts +44 -0
- package/src/exercises/squareRoots/simpifySquareRoot.js +1 -0
- package/src/exercises/squareRoots/simpifySquareRoot.ts +1 -0
- package/src/index.js +5 -0
- package/src/index.ts +5 -16
- package/src/mathutils/arithmetic/coprimesOf.js +13 -0
- package/src/mathutils/arithmetic/coprimesOf.ts +9 -0
- package/src/mathutils/arithmetic/dividersOf.ts +7 -0
- package/src/mathutils/arithmetic/lcd.js +12 -0
- package/src/mathutils/arithmetic/lcd.ts +7 -0
- package/src/mathutils/arithmetic/nonCoprimesOf.ts +9 -0
- package/src/mathutils/arithmetic/nonDividersOf.ts +12 -0
- package/src/mathutils/decimals/decimalPartLengthOf.ts +10 -0
- package/src/mathutils/random/randint.js +11 -2
- package/src/mathutils/random/randint.ts +10 -2
- package/src/numbers/decimals/decimal.js +144 -0
- package/src/numbers/decimals/decimal.ts +140 -0
- package/src/numbers/integer/integer.js +61 -4
- package/src/numbers/integer/integer.ts +58 -7
- package/src/numbers/integer/power.js +53 -0
- package/src/numbers/integer/power.ts +49 -0
- package/src/numbers/nombre.js +3 -2
- package/src/numbers/nombre.ts +2 -0
- package/src/numbers/rationals/rational.js +96 -10
- package/src/numbers/rationals/rational.ts +81 -15
- package/src/numbers/reals/real.js +2 -0
- package/src/numbers/reals/real.ts +3 -1
- package/src/numbers/reals/squareRoot.ts +3 -10
- package/src/polynomials/affine.ts +2 -6
- package/src/polynomials/polynomial.js +4 -13
- package/src/polynomials/polynomial.ts +8 -25
- package/src/tree/latexParser/latexParse.js +81 -34
- package/src/tree/latexParser/latexParse.ts +85 -39
- package/src/tree/nodes/functions/functionNode.js +19 -0
- package/src/tree/nodes/functions/functionNode.ts +18 -0
- package/src/tree/nodes/functions/oppositeNode.js +30 -0
- package/src/tree/nodes/functions/oppositeNode.ts +12 -0
- package/src/tree/nodes/functions/sqrtNode.js +24 -9
- package/src/tree/nodes/functions/sqrtNode.ts +5 -9
- package/src/tree/nodes/node.ts +1 -3
- package/src/tree/nodes/numbers/numberNode.js +3 -6
- package/src/tree/nodes/numbers/numberNode.ts +4 -7
- package/src/tree/nodes/operators/addNode.js +23 -8
- package/src/tree/nodes/operators/addNode.ts +3 -7
- package/src/tree/nodes/operators/divideNode.js +21 -9
- package/src/tree/nodes/operators/divideNode.ts +4 -11
- package/src/tree/nodes/operators/equalNode.js +20 -8
- package/src/tree/nodes/operators/equalNode.ts +3 -9
- package/src/tree/nodes/operators/fractionNode.js +34 -0
- package/src/tree/nodes/operators/fractionNode.ts +16 -0
- package/src/tree/nodes/operators/multiplyNode.js +20 -8
- package/src/tree/nodes/operators/multiplyNode.ts +3 -9
- package/src/tree/nodes/operators/operatorNode.js +35 -0
- package/src/tree/nodes/operators/operatorNode.ts +35 -2
- package/src/tree/nodes/operators/powerNode.js +20 -8
- package/src/tree/nodes/operators/powerNode.ts +3 -8
- package/src/tree/nodes/operators/substractNode.js +20 -8
- package/src/tree/nodes/operators/substractNode.ts +3 -9
- package/src/tree/nodes/variables/variableNode.js +1 -4
- package/src/tree/nodes/variables/variableNode.ts +4 -7
- package/src/utils/coin.js +7 -0
- package/src/utils/coin.ts +3 -0
- package/src/utils/shuffle.js +15 -4
- package/src/utils/shuffle.ts +7 -5
- package/src/exercises/calcul/priority.ts +0 -33
- package/src/expression/expression.js +0 -9
- package/src/expression/expression.ts +0 -13
- package/src/numbers/rationals/division.ts +0 -0
- package/src/tree/nodes/operators/oppositeNode.ts +0 -17
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.coprimesOf = void 0;
|
|
4
|
+
var gcd_1 = require("./gcd");
|
|
5
|
+
var coprimesOf = function (nb) {
|
|
6
|
+
var coprimes = [];
|
|
7
|
+
for (var i = 2; i <= nb; i++) {
|
|
8
|
+
if ((0, gcd_1.gcd)(nb, i) === 1)
|
|
9
|
+
coprimes.push(i);
|
|
10
|
+
}
|
|
11
|
+
return coprimes;
|
|
12
|
+
};
|
|
13
|
+
exports.coprimesOf = coprimesOf;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { dividersOf } from "./dividersOf";
|
|
2
|
+
|
|
3
|
+
export const nonDivisorOf = (nb: number) => {
|
|
4
|
+
let nonDividers = [];
|
|
5
|
+
let dividers = dividersOf(nb);
|
|
6
|
+
|
|
7
|
+
for (let i = 2; i < nb; i++) {
|
|
8
|
+
if (nb % i !== 0) nonDividers.push(i);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return nonDividers;
|
|
12
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const decimalPartLengthOf = (x: number) => {
|
|
2
|
+
function hasFraction(x: number) {
|
|
3
|
+
return Math.abs(Math.round(x) - x) > 1e-10;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
let count = 0;
|
|
7
|
+
// multiply by increasing powers of 10 until the fractional part is ~ 0
|
|
8
|
+
while (hasFraction(x * 10 ** count) && isFinite(10 ** count)) count++;
|
|
9
|
+
return count;
|
|
10
|
+
};
|
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
exports.__esModule = true;
|
|
3
3
|
exports.randint = void 0;
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* @returns random [[a, b[[
|
|
6
|
+
*/
|
|
7
|
+
var randint = function (a, b, excludes) {
|
|
5
8
|
if (b === undefined)
|
|
6
9
|
return Math.floor(Math.random() * a);
|
|
7
|
-
|
|
10
|
+
if (!excludes)
|
|
11
|
+
return a + Math.floor(Math.random() * (b - a));
|
|
12
|
+
var res;
|
|
13
|
+
do {
|
|
14
|
+
res = a + Math.floor(Math.random() * (b - a));
|
|
15
|
+
} while (excludes.includes(res));
|
|
16
|
+
return res;
|
|
8
17
|
};
|
|
9
18
|
exports.randint = randint;
|
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @returns random [[a, b[[
|
|
3
|
+
*/
|
|
4
|
+
export const randint = (a: number, b?: number, excludes?: number[]): number => {
|
|
2
5
|
if (b === undefined) return Math.floor(Math.random() * a);
|
|
3
|
-
return a + Math.floor(Math.random() * (b - a));
|
|
6
|
+
if (!excludes) return a + Math.floor(Math.random() * (b - a));
|
|
7
|
+
let res;
|
|
8
|
+
do {
|
|
9
|
+
res = a + Math.floor(Math.random() * (b - a));
|
|
10
|
+
} while (excludes.includes(res));
|
|
11
|
+
return res;
|
|
4
12
|
};
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.Decimal = exports.DecimalConstructor = void 0;
|
|
4
|
+
var randint_1 = require("../../mathutils/random/randint");
|
|
5
|
+
var numberNode_1 = require("../../tree/nodes/numbers/numberNode");
|
|
6
|
+
var integer_1 = require("../integer/integer");
|
|
7
|
+
var nombre_1 = require("../nombre");
|
|
8
|
+
var DecimalConstructor = /** @class */ (function () {
|
|
9
|
+
function DecimalConstructor() {
|
|
10
|
+
}
|
|
11
|
+
DecimalConstructor.randomFracPart = function (precision) {
|
|
12
|
+
var decimals = "";
|
|
13
|
+
for (var i = 0; i < precision; i++) {
|
|
14
|
+
decimals += (0, randint_1.randint)(i === precision - 1 ? 1 : 0, 10);
|
|
15
|
+
}
|
|
16
|
+
return decimals;
|
|
17
|
+
};
|
|
18
|
+
DecimalConstructor.random = function (min, max, precision) {
|
|
19
|
+
var int = (0, randint_1.randint)(min, max) + "";
|
|
20
|
+
var decimals = DecimalConstructor.randomFracPart(precision);
|
|
21
|
+
return DecimalConstructor.fromParts(int, decimals);
|
|
22
|
+
};
|
|
23
|
+
DecimalConstructor.fromParts = function (intPart, decimalPart) {
|
|
24
|
+
return new Decimal(Number("" + intPart + "." + decimalPart));
|
|
25
|
+
};
|
|
26
|
+
return DecimalConstructor;
|
|
27
|
+
}());
|
|
28
|
+
exports.DecimalConstructor = DecimalConstructor;
|
|
29
|
+
var Decimal = /** @class */ (function () {
|
|
30
|
+
function Decimal(value) {
|
|
31
|
+
this.type = nombre_1.NumberType.Decimal;
|
|
32
|
+
this.value = value;
|
|
33
|
+
this.tex = value + "";
|
|
34
|
+
var _a = (value + "").split("."), intPartString = _a[0], decimalPartString = _a[1];
|
|
35
|
+
this.intPart = Number(intPartString);
|
|
36
|
+
this.decimalPart = decimalPartString || "";
|
|
37
|
+
this.precision = this.decimalPart.length;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
*
|
|
41
|
+
* @param precision 0 = unité, 1 = dizieme, ... , -1 : dizaine
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
Decimal.prototype.round = function (precision) {
|
|
45
|
+
var intPartString = this.intPart + "";
|
|
46
|
+
if (precision < 0) {
|
|
47
|
+
if (precision < -intPartString.length)
|
|
48
|
+
throw Error("can't round to higher precision");
|
|
49
|
+
return new integer_1.Integer(this.intPart).round(-precision);
|
|
50
|
+
}
|
|
51
|
+
if (precision > this.precision)
|
|
52
|
+
throw Error("can't round to higher precision");
|
|
53
|
+
if (precision === this.precision)
|
|
54
|
+
return this;
|
|
55
|
+
var newFracPart = "", newIntPart = "";
|
|
56
|
+
var shouldRoundUp = Number(this.decimalPart[precision]) > 4;
|
|
57
|
+
if (shouldRoundUp) {
|
|
58
|
+
var retenue = true;
|
|
59
|
+
var i = precision - 1;
|
|
60
|
+
while (retenue) {
|
|
61
|
+
if (i > -1) {
|
|
62
|
+
var nb = (Number(this.decimalPart[i]) + 1) % 10;
|
|
63
|
+
if (nb || newFracPart) {
|
|
64
|
+
newFracPart = nb.toString() + newFracPart;
|
|
65
|
+
}
|
|
66
|
+
if (nb !== 0) {
|
|
67
|
+
retenue = false;
|
|
68
|
+
for (var j = i - 1; j > -1; j--) {
|
|
69
|
+
newFracPart = this.decimalPart[j] + newFracPart;
|
|
70
|
+
}
|
|
71
|
+
newIntPart = intPartString;
|
|
72
|
+
}
|
|
73
|
+
else
|
|
74
|
+
i--;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
var nb = (Number(intPartString[i + intPartString.length]) + 1) % 10;
|
|
78
|
+
newIntPart = nb + "" + newIntPart;
|
|
79
|
+
if (nb !== 0) {
|
|
80
|
+
retenue = false;
|
|
81
|
+
for (var j = i + intPartString.length - 1; j > -1; j--) {
|
|
82
|
+
newIntPart = intPartString[j] + newIntPart;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else
|
|
86
|
+
i--;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
var retenue = true;
|
|
92
|
+
var i = precision - 1;
|
|
93
|
+
while (retenue) {
|
|
94
|
+
if (i > -1) {
|
|
95
|
+
var nb = Number(this.decimalPart[i]);
|
|
96
|
+
if (nb || newFracPart) {
|
|
97
|
+
newFracPart = nb.toString() + newFracPart;
|
|
98
|
+
}
|
|
99
|
+
if (nb !== 0) {
|
|
100
|
+
retenue = false;
|
|
101
|
+
for (var j = i - 1; j > -1; j--) {
|
|
102
|
+
newFracPart = this.decimalPart[j] + newFracPart;
|
|
103
|
+
}
|
|
104
|
+
newIntPart = intPartString;
|
|
105
|
+
}
|
|
106
|
+
else
|
|
107
|
+
i--;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
newIntPart = intPartString;
|
|
111
|
+
retenue = false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return DecimalConstructor.fromParts(newIntPart, newFracPart);
|
|
116
|
+
};
|
|
117
|
+
Decimal.prototype.multiplyByPowerOfTen = function (power) {
|
|
118
|
+
var newIntPart = "", newFracPart = "";
|
|
119
|
+
if (power > -1) {
|
|
120
|
+
newIntPart = this.intPart + "";
|
|
121
|
+
for (var i = 0; i < power; i++) {
|
|
122
|
+
newIntPart += i > this.decimalPart.length - 1 ? "0" : this.decimalPart[i];
|
|
123
|
+
}
|
|
124
|
+
newFracPart = this.decimalPart.slice(power);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
var intPartString = this.intPart + "";
|
|
128
|
+
newFracPart = this.decimalPart;
|
|
129
|
+
for (var i = intPartString.length - 1; i > intPartString.length - 1 + power; i--) {
|
|
130
|
+
newFracPart = (i < 0 ? "0" : intPartString[i]) + newFracPart;
|
|
131
|
+
}
|
|
132
|
+
if (power + intPartString.length < 1)
|
|
133
|
+
newIntPart = "0";
|
|
134
|
+
else
|
|
135
|
+
newIntPart = intPartString.slice(0, power + intPartString.length);
|
|
136
|
+
}
|
|
137
|
+
return DecimalConstructor.fromParts(newIntPart, newFracPart);
|
|
138
|
+
};
|
|
139
|
+
Decimal.prototype.toTree = function () {
|
|
140
|
+
return new numberNode_1.NumberNode(this.value);
|
|
141
|
+
};
|
|
142
|
+
return Decimal;
|
|
143
|
+
}());
|
|
144
|
+
exports.Decimal = Decimal;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { randint } from "../../mathutils/random/randint";
|
|
2
|
+
import { Node } from "../../tree/nodes/node";
|
|
3
|
+
import { NumberNode } from "../../tree/nodes/numbers/numberNode";
|
|
4
|
+
import { Integer } from "../integer/integer";
|
|
5
|
+
import { Nombre, NumberType } from "../nombre";
|
|
6
|
+
|
|
7
|
+
export abstract class DecimalConstructor {
|
|
8
|
+
static randomFracPart(precision: number): string {
|
|
9
|
+
let decimals = "";
|
|
10
|
+
for (let i = 0; i < precision; i++) {
|
|
11
|
+
decimals += randint(i === precision - 1 ? 1 : 0, 10);
|
|
12
|
+
}
|
|
13
|
+
return decimals;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
static random(min: number, max: number, precision: number): Decimal {
|
|
17
|
+
const int = randint(min, max) + "";
|
|
18
|
+
const decimals = DecimalConstructor.randomFracPart(precision);
|
|
19
|
+
return DecimalConstructor.fromParts(int, decimals);
|
|
20
|
+
}
|
|
21
|
+
static fromParts(intPart: string, decimalPart: string): Decimal {
|
|
22
|
+
return new Decimal(Number("" + intPart + "." + decimalPart));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class Decimal implements Nombre {
|
|
27
|
+
value: number;
|
|
28
|
+
tex: string;
|
|
29
|
+
type = NumberType.Decimal;
|
|
30
|
+
precision: number;
|
|
31
|
+
intPart: number;
|
|
32
|
+
decimalPart: string;
|
|
33
|
+
constructor(value: number) {
|
|
34
|
+
this.value = value;
|
|
35
|
+
this.tex = value + "";
|
|
36
|
+
let [intPartString, decimalPartString] = (value + "").split(".");
|
|
37
|
+
this.intPart = Number(intPartString);
|
|
38
|
+
this.decimalPart = decimalPartString || "";
|
|
39
|
+
this.precision = this.decimalPart.length;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param precision 0 = unité, 1 = dizieme, ... , -1 : dizaine
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
round(precision: number): Nombre {
|
|
48
|
+
const intPartString = this.intPart + "";
|
|
49
|
+
|
|
50
|
+
if (precision < 0) {
|
|
51
|
+
if (precision < -intPartString.length) throw Error("can't round to higher precision");
|
|
52
|
+
return new Integer(this.intPart).round(-precision);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (precision > this.precision) throw Error("can't round to higher precision");
|
|
56
|
+
if (precision === this.precision) return this;
|
|
57
|
+
|
|
58
|
+
let newFracPart = "",
|
|
59
|
+
newIntPart = "";
|
|
60
|
+
|
|
61
|
+
const shouldRoundUp = Number(this.decimalPart[precision]) > 4;
|
|
62
|
+
|
|
63
|
+
if (shouldRoundUp) {
|
|
64
|
+
let retenue = true;
|
|
65
|
+
let i = precision - 1;
|
|
66
|
+
while (retenue) {
|
|
67
|
+
if (i > -1) {
|
|
68
|
+
const nb = (Number(this.decimalPart[i]) + 1) % 10;
|
|
69
|
+
if (nb || newFracPart) {
|
|
70
|
+
newFracPart = nb.toString() + newFracPart;
|
|
71
|
+
}
|
|
72
|
+
if (nb !== 0) {
|
|
73
|
+
retenue = false;
|
|
74
|
+
for (let j = i - 1; j > -1; j--) {
|
|
75
|
+
newFracPart = this.decimalPart[j] + newFracPart;
|
|
76
|
+
}
|
|
77
|
+
newIntPart = intPartString;
|
|
78
|
+
} else i--;
|
|
79
|
+
} else {
|
|
80
|
+
const nb = (Number(intPartString[i + intPartString.length]) + 1) % 10;
|
|
81
|
+
newIntPart = nb + "" + newIntPart;
|
|
82
|
+
if (nb !== 0) {
|
|
83
|
+
retenue = false;
|
|
84
|
+
for (let j = i + intPartString.length - 1; j > -1; j--) {
|
|
85
|
+
newIntPart = intPartString[j] + newIntPart;
|
|
86
|
+
}
|
|
87
|
+
} else i--;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
let retenue = true;
|
|
92
|
+
let i = precision - 1;
|
|
93
|
+
while (retenue) {
|
|
94
|
+
if (i > -1) {
|
|
95
|
+
const nb = Number(this.decimalPart[i]);
|
|
96
|
+
if (nb || newFracPart) {
|
|
97
|
+
newFracPart = nb.toString() + newFracPart;
|
|
98
|
+
}
|
|
99
|
+
if (nb !== 0) {
|
|
100
|
+
retenue = false;
|
|
101
|
+
for (let j = i - 1; j > -1; j--) {
|
|
102
|
+
newFracPart = this.decimalPart[j] + newFracPart;
|
|
103
|
+
}
|
|
104
|
+
newIntPart = intPartString;
|
|
105
|
+
} else i--;
|
|
106
|
+
} else {
|
|
107
|
+
newIntPart = intPartString;
|
|
108
|
+
retenue = false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return DecimalConstructor.fromParts(newIntPart, newFracPart);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
multiplyByPowerOfTen(power: number) {
|
|
116
|
+
let newIntPart = "",
|
|
117
|
+
newFracPart = "";
|
|
118
|
+
|
|
119
|
+
if (power > -1) {
|
|
120
|
+
newIntPart = this.intPart + "";
|
|
121
|
+
for (let i = 0; i < power; i++) {
|
|
122
|
+
newIntPart += i > this.decimalPart.length - 1 ? "0" : this.decimalPart[i];
|
|
123
|
+
}
|
|
124
|
+
newFracPart = this.decimalPart.slice(power);
|
|
125
|
+
} else {
|
|
126
|
+
const intPartString = this.intPart + "";
|
|
127
|
+
newFracPart = this.decimalPart;
|
|
128
|
+
for (let i = intPartString.length - 1; i > intPartString.length - 1 + power; i--) {
|
|
129
|
+
newFracPart = (i < 0 ? "0" : intPartString[i]) + newFracPart;
|
|
130
|
+
}
|
|
131
|
+
if (power + intPartString.length < 1) newIntPart = "0";
|
|
132
|
+
else newIntPart = intPartString.slice(0, power + intPartString.length);
|
|
133
|
+
}
|
|
134
|
+
return DecimalConstructor.fromParts(newIntPart, newFracPart);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
toTree(): Node {
|
|
138
|
+
return new NumberNode(this.value);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -1,14 +1,71 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
exports.__esModule = true;
|
|
3
|
-
exports.Integer = void 0;
|
|
3
|
+
exports.Integer = exports.IntegerConstructor = void 0;
|
|
4
|
+
var randint_1 = require("../../mathutils/random/randint");
|
|
4
5
|
var numberNode_1 = require("../../tree/nodes/numbers/numberNode");
|
|
6
|
+
var nombre_1 = require("../nombre");
|
|
7
|
+
var rational_1 = require("../rationals/rational");
|
|
8
|
+
var IntegerConstructor = /** @class */ (function () {
|
|
9
|
+
function IntegerConstructor() {
|
|
10
|
+
}
|
|
11
|
+
IntegerConstructor.random = function (nbOfDigits) {
|
|
12
|
+
return (0, randint_1.randint)(0, Math.pow(10, nbOfDigits));
|
|
13
|
+
};
|
|
14
|
+
return IntegerConstructor;
|
|
15
|
+
}());
|
|
16
|
+
exports.IntegerConstructor = IntegerConstructor;
|
|
5
17
|
var Integer = /** @class */ (function () {
|
|
6
|
-
function Integer(value) {
|
|
18
|
+
function Integer(value, tex) {
|
|
7
19
|
this.value = value;
|
|
8
|
-
this.tex = value + "";
|
|
20
|
+
this.tex = tex || value + "";
|
|
21
|
+
this.type = nombre_1.NumberType.Integer;
|
|
9
22
|
}
|
|
10
23
|
Integer.prototype.toTree = function () {
|
|
11
|
-
return new numberNode_1.NumberNode(this.value);
|
|
24
|
+
return new numberNode_1.NumberNode(this.value, this.tex);
|
|
25
|
+
};
|
|
26
|
+
Integer.prototype.round = function (precision) {
|
|
27
|
+
var intString = this.value + "";
|
|
28
|
+
if (precision >= intString.length || precision < 1)
|
|
29
|
+
throw Error("can't round to higher precision");
|
|
30
|
+
var newInt = "";
|
|
31
|
+
var shouldRoundUp = Number(intString[intString.length - precision]) > 4;
|
|
32
|
+
if (shouldRoundUp) {
|
|
33
|
+
for (var i_1 = 0; i_1 < precision; i_1++) {
|
|
34
|
+
newInt += "0";
|
|
35
|
+
}
|
|
36
|
+
var retenue = true;
|
|
37
|
+
var i = intString.length - precision - 1;
|
|
38
|
+
while (retenue) {
|
|
39
|
+
var nb = (Number(intString[i]) + 1) % 10;
|
|
40
|
+
newInt = "" + nb + newInt;
|
|
41
|
+
if (nb === 0) {
|
|
42
|
+
i--;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
retenue = false;
|
|
46
|
+
for (var j = i - 1; j > -1; j--) {
|
|
47
|
+
newInt = intString[j] + newInt;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
for (var i = 0; i < intString.length; i++) {
|
|
54
|
+
newInt += i < intString.length - precision ? intString[i] : "0";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return new Integer(Number(newInt));
|
|
58
|
+
};
|
|
59
|
+
Integer.prototype.divide = function (nb) {
|
|
60
|
+
switch (nb.type) {
|
|
61
|
+
case nombre_1.NumberType.Integer:
|
|
62
|
+
return new rational_1.Rational(this.value, nb.value).simplify();
|
|
63
|
+
case nombre_1.NumberType.Rational:
|
|
64
|
+
var rational = nb;
|
|
65
|
+
return new rational_1.Rational(this.value * rational.denum, rational.num).simplify();
|
|
66
|
+
default:
|
|
67
|
+
throw Error("not implemented");
|
|
68
|
+
}
|
|
12
69
|
};
|
|
13
70
|
return Integer;
|
|
14
71
|
}());
|
|
@@ -1,21 +1,72 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randint } from "../../mathutils/random/randint";
|
|
2
2
|
import { Node } from "../../tree/nodes/node";
|
|
3
3
|
import { NumberNode } from "../../tree/nodes/numbers/numberNode";
|
|
4
|
-
import { Nombre } from "../nombre";
|
|
4
|
+
import { Nombre, NumberType } from "../nombre";
|
|
5
|
+
import { Rational } from "../rationals/rational";
|
|
6
|
+
|
|
7
|
+
export abstract class IntegerConstructor {
|
|
8
|
+
static random(nbOfDigits: number) {
|
|
9
|
+
return randint(0, 10 ** nbOfDigits);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
5
12
|
|
|
6
13
|
export class Integer implements Nombre {
|
|
7
14
|
value: number;
|
|
8
15
|
tex: string;
|
|
16
|
+
type: NumberType;
|
|
9
17
|
|
|
10
|
-
constructor(value: number) {
|
|
18
|
+
constructor(value: number, tex?: string) {
|
|
11
19
|
this.value = value;
|
|
12
|
-
this.tex = value + "";
|
|
20
|
+
this.tex = tex || value + "";
|
|
21
|
+
this.type = NumberType.Integer;
|
|
13
22
|
}
|
|
14
23
|
|
|
15
24
|
toTree(): Node {
|
|
16
|
-
return new NumberNode(this.value);
|
|
25
|
+
return new NumberNode(this.value, this.tex);
|
|
17
26
|
}
|
|
18
|
-
// add(expression: Expression): Expression {
|
|
19
27
|
|
|
20
|
-
|
|
28
|
+
round(precision: number) {
|
|
29
|
+
const intString = this.value + "";
|
|
30
|
+
|
|
31
|
+
if (precision >= intString.length || precision < 1) throw Error("can't round to higher precision");
|
|
32
|
+
|
|
33
|
+
let newInt = "";
|
|
34
|
+
const shouldRoundUp = Number(intString[intString.length - precision]) > 4;
|
|
35
|
+
if (shouldRoundUp) {
|
|
36
|
+
for (let i = 0; i < precision; i++) {
|
|
37
|
+
newInt += "0";
|
|
38
|
+
}
|
|
39
|
+
let retenue = true;
|
|
40
|
+
let i = intString.length - precision - 1;
|
|
41
|
+
|
|
42
|
+
while (retenue) {
|
|
43
|
+
const nb = (Number(intString[i]) + 1) % 10;
|
|
44
|
+
newInt = "" + nb + newInt;
|
|
45
|
+
if (nb === 0) {
|
|
46
|
+
i--;
|
|
47
|
+
} else {
|
|
48
|
+
retenue = false;
|
|
49
|
+
for (let j = i - 1; j > -1; j--) {
|
|
50
|
+
newInt = intString[j] + newInt;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
for (let i = 0; i < intString.length; i++) {
|
|
56
|
+
newInt += i < intString.length - precision ? intString[i] : "0";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return new Integer(Number(newInt));
|
|
60
|
+
}
|
|
61
|
+
divide(nb: Nombre): Nombre {
|
|
62
|
+
switch (nb.type) {
|
|
63
|
+
case NumberType.Integer:
|
|
64
|
+
return new Rational(this.value, nb.value).simplify();
|
|
65
|
+
case NumberType.Rational:
|
|
66
|
+
const rational = nb as Rational;
|
|
67
|
+
return new Rational(this.value * rational.denum, rational.num).simplify();
|
|
68
|
+
default:
|
|
69
|
+
throw Error("not implemented");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
21
72
|
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.Power = void 0;
|
|
4
|
+
var numberNode_1 = require("../../tree/nodes/numbers/numberNode");
|
|
5
|
+
var powerNode_1 = require("../../tree/nodes/operators/powerNode");
|
|
6
|
+
var nombre_1 = require("../nombre");
|
|
7
|
+
var integer_1 = require("./integer");
|
|
8
|
+
var Power = /** @class */ (function () {
|
|
9
|
+
function Power(a, b) {
|
|
10
|
+
this.operand = a;
|
|
11
|
+
this.power = b;
|
|
12
|
+
this.value = Math.pow(a, b);
|
|
13
|
+
this.tex = "".concat(a, "^{").concat(b, "}");
|
|
14
|
+
this.type = b < 0 ? nombre_1.NumberType.Rational : nombre_1.NumberType.Integer;
|
|
15
|
+
}
|
|
16
|
+
Power.prototype.simplify = function () {
|
|
17
|
+
if (this.power === 0)
|
|
18
|
+
return new numberNode_1.NumberNode(1);
|
|
19
|
+
if (this.power === 1)
|
|
20
|
+
return new numberNode_1.NumberNode(this.operand);
|
|
21
|
+
if (this.operand === 1)
|
|
22
|
+
return new numberNode_1.NumberNode(1);
|
|
23
|
+
if (this.operand === 0)
|
|
24
|
+
return new numberNode_1.NumberNode(0);
|
|
25
|
+
if (this.operand === -1)
|
|
26
|
+
return new numberNode_1.NumberNode(this.power % 2 === 0 ? 1 : -1);
|
|
27
|
+
return this.toTree();
|
|
28
|
+
};
|
|
29
|
+
Power.prototype.toDecimalWriting = function () {
|
|
30
|
+
if (this.operand !== 10)
|
|
31
|
+
throw Error("only implemented for powers of ten so far");
|
|
32
|
+
var s = "";
|
|
33
|
+
if (this.power > -1) {
|
|
34
|
+
s += "1";
|
|
35
|
+
for (var i = 0; i < this.power; i++) {
|
|
36
|
+
s += 0;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
s += "1";
|
|
41
|
+
for (var i = 1; i < Math.abs(this.power); i++) {
|
|
42
|
+
s = "0" + s;
|
|
43
|
+
}
|
|
44
|
+
s = "0." + s;
|
|
45
|
+
}
|
|
46
|
+
return new integer_1.Integer(Number(s), s);
|
|
47
|
+
};
|
|
48
|
+
Power.prototype.toTree = function () {
|
|
49
|
+
return new powerNode_1.PowerNode(new numberNode_1.NumberNode(this.operand), new numberNode_1.NumberNode(this.power));
|
|
50
|
+
};
|
|
51
|
+
return Power;
|
|
52
|
+
}());
|
|
53
|
+
exports.Power = Power;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Node } from "../../tree/nodes/node";
|
|
2
|
+
import { NumberNode } from "../../tree/nodes/numbers/numberNode";
|
|
3
|
+
import { PowerNode } from "../../tree/nodes/operators/powerNode";
|
|
4
|
+
import { Nombre, NumberType } from "../nombre";
|
|
5
|
+
import { Integer } from "./integer";
|
|
6
|
+
|
|
7
|
+
export class Power implements Nombre {
|
|
8
|
+
value: number;
|
|
9
|
+
tex: string;
|
|
10
|
+
type: NumberType;
|
|
11
|
+
operand: number;
|
|
12
|
+
power: number;
|
|
13
|
+
constructor(a: number, b: number) {
|
|
14
|
+
this.operand = a;
|
|
15
|
+
this.power = b;
|
|
16
|
+
this.value = Math.pow(a, b);
|
|
17
|
+
this.tex = `${a}^{${b}}`;
|
|
18
|
+
this.type = b < 0 ? NumberType.Rational : NumberType.Integer;
|
|
19
|
+
}
|
|
20
|
+
simplify(): Node {
|
|
21
|
+
if (this.power === 0) return new NumberNode(1);
|
|
22
|
+
if (this.power === 1) return new NumberNode(this.operand);
|
|
23
|
+
if (this.operand === 1) return new NumberNode(1);
|
|
24
|
+
if (this.operand === 0) return new NumberNode(0);
|
|
25
|
+
if (this.operand === -1) return new NumberNode(this.power % 2 === 0 ? 1 : -1);
|
|
26
|
+
return this.toTree();
|
|
27
|
+
}
|
|
28
|
+
toDecimalWriting(): Nombre {
|
|
29
|
+
if (this.operand !== 10) throw Error("only implemented for powers of ten so far");
|
|
30
|
+
let s = "";
|
|
31
|
+
if (this.power > -1) {
|
|
32
|
+
s += "1";
|
|
33
|
+
for (let i = 0; i < this.power; i++) {
|
|
34
|
+
s += 0;
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
s += "1";
|
|
38
|
+
for (let i = 1; i < Math.abs(this.power); i++) {
|
|
39
|
+
s = "0" + s;
|
|
40
|
+
}
|
|
41
|
+
s = "0." + s;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return new Integer(Number(s), s);
|
|
45
|
+
}
|
|
46
|
+
toTree(): Node {
|
|
47
|
+
return new PowerNode(new NumberNode(this.operand), new NumberNode(this.power));
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/numbers/nombre.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.NumberType = void 0;
|
|
|
4
4
|
var NumberType;
|
|
5
5
|
(function (NumberType) {
|
|
6
6
|
NumberType[NumberType["Integer"] = 0] = "Integer";
|
|
7
|
-
NumberType[NumberType["
|
|
8
|
-
NumberType[NumberType["
|
|
7
|
+
NumberType[NumberType["Decimal"] = 1] = "Decimal";
|
|
8
|
+
NumberType[NumberType["Rational"] = 2] = "Rational";
|
|
9
|
+
NumberType[NumberType["Real"] = 3] = "Real";
|
|
9
10
|
})(NumberType = exports.NumberType || (exports.NumberType = {}));
|