This commit is contained in:
38
node_modules/fraction.js/CHANGELOG.md
generated
vendored
Normal file
38
node_modules/fraction.js/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
# CHANGELOG
|
||||
|
||||
v5.2.2
|
||||
- Improved documentation and removed unecessary check
|
||||
|
||||
v5.2.1:
|
||||
- 2bb7b05: Added negative sign check
|
||||
|
||||
v5.2:
|
||||
- 6f9d124: Implemented log and improved simplify
|
||||
- b773e7a: Added named export to TS definition
|
||||
- 70304f9: Fixed merge conflict
|
||||
- 3b940d3: Implemented other comparing functions
|
||||
- 10acdfc: Update README.md
|
||||
- ba41d00: Update README.md
|
||||
- 73ded97: Update README.md
|
||||
- acabc39: Fixed param parsing
|
||||
|
||||
v5.0.5:
|
||||
- 2c9d4c2: Improved roundTo() and param parser
|
||||
|
||||
v5.0.4:
|
||||
- 39e61e7: Fixed bignum param passing
|
||||
|
||||
v5.0.3:
|
||||
- 7d9a3ec: Upgraded bundler for code quality
|
||||
|
||||
v5.0.2:
|
||||
- c64b1d6: fixed esm export
|
||||
|
||||
v5.0.1:
|
||||
- e440f9c: Fixed CJS export
|
||||
- 9bbdd29: Fixed CJS export
|
||||
|
||||
v5.0.0:
|
||||
- ac7cd06: Fixed readme
|
||||
- 33cc9e5: Added crude build
|
||||
- 1adcc76: Release breaking v5.0. Fraction.js now builds on BigInt. The API stays the same as v4, except that the object attributes `n`, `d`, and `s`, are not Number but BigInt and may break code that directly accesses these attributes.
|
||||
21
node_modules/fraction.js/LICENSE
generated
vendored
Normal file
21
node_modules/fraction.js/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Robert Eisele
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
520
node_modules/fraction.js/README.md
generated
vendored
Normal file
520
node_modules/fraction.js/README.md
generated
vendored
Normal file
@@ -0,0 +1,520 @@
|
||||
# Fraction.js - ℚ in JavaScript
|
||||
|
||||
[](https://npmjs.org/package/fraction.js "View this project on npm")
|
||||
[](http://opensource.org/licenses/MIT)
|
||||
|
||||
Do you find the limitations of floating-point arithmetic frustrating, especially when rational and irrational numbers like π or √2 are stored within the same finite precision? This can lead to avoidable inaccuracies such as:
|
||||
|
||||
```javascript
|
||||
1 / 98 * 98 // Results in 0.9999999999999999
|
||||
```
|
||||
|
||||
For applications requiring higher precision or where working with fractions is preferable, consider incorporating *Fraction.js* into your project.
|
||||
|
||||
The library effectively addresses precision issues, as demonstrated below:
|
||||
|
||||
```javascript
|
||||
Fraction(1).div(98).mul(98) // Returns 1
|
||||
```
|
||||
|
||||
*Fraction.js* uses a `BigInt` representation for both the numerator and denominator, ensuring minimal performance overhead while maximizing accuracy. Its design is optimized for precision, making it an ideal choice as a foundational library for other math tools, such as [Polynomial.js](https://github.com/rawify/Polynomial.js) and [Math.js](https://github.com/josdejong/mathjs).
|
||||
|
||||
## Convert Decimal to Fraction
|
||||
|
||||
One of the core features of *Fraction.js* is its ability to seamlessly convert decimal numbers into fractions.
|
||||
|
||||
```javascript
|
||||
let x = new Fraction(1.88);
|
||||
let res = x.toFraction(true); // Returns "1 22/25" as a string
|
||||
```
|
||||
|
||||
This is particularly useful when you need precise fraction representations instead of dealing with the limitations of floating-point arithmetic. What if you allow some error tolerance?
|
||||
|
||||
```javascript
|
||||
let x = new Fraction(0.33333);
|
||||
let res = x.simplify(0.001) // Error < 0.001
|
||||
.toFraction(); // Returns "1/3" as a string
|
||||
```
|
||||
|
||||
## Precision
|
||||
|
||||
As native `BigInt` support in JavaScript becomes more common, libraries like *Fraction.js* use it to handle calculations with higher precision. This improves the speed and accuracy of math operations with large numbers, providing a better solution for tasks that need more precision than floating-point numbers can offer.
|
||||
|
||||
## Examples / Motivation
|
||||
|
||||
A simple example of using *Fraction.js* might look like this:
|
||||
|
||||
```javascript
|
||||
var f = new Fraction("9.4'31'"); // 9.4313131313131...
|
||||
f.mul([-4, 3]).mod("4.'8'"); // 4.88888888888888...
|
||||
```
|
||||
|
||||
The result can then be displayed as:
|
||||
|
||||
```javascript
|
||||
console.log(f.toFraction()); // -4154 / 1485
|
||||
```
|
||||
|
||||
Additionally, you can access the internal attributes of the fraction, such as the sign (s), numerator (n), and denominator (d). Keep in mind that these values are stored as `BigInt`:
|
||||
|
||||
```javascript
|
||||
Number(f.s) * Number(f.n) / Number(f.d) = -1 * 4154 / 1485 = -2.797306...
|
||||
```
|
||||
|
||||
If you attempted to calculate this manually using floating-point arithmetic, you'd get something like:
|
||||
|
||||
```javascript
|
||||
(9.4313131 * (-4 / 3)) % 4.888888 = -2.797308133...
|
||||
```
|
||||
|
||||
While the result is reasonably close, it’s not as accurate as the fraction-based approach that *Fraction.js* provides, especially when dealing with repeating decimals or complex operations. This highlights the value of precision that the library brings.
|
||||
|
||||
### Laplace Probability
|
||||
|
||||
Here's a straightforward example of using *Fraction.js* to calculate probabilities. Let's determine the probability of rolling a specific outcome on a fair die:
|
||||
|
||||
- **P({3})**: The probability of rolling a 3.
|
||||
- **P({1, 4})**: The probability of rolling either 1 or 4.
|
||||
- **P({2, 4, 6})**: The probability of rolling 2, 4, or 6.
|
||||
|
||||
#### P({3}):
|
||||
|
||||
```javascript
|
||||
var p = new Fraction([3].length, 6).toString(); // "0.1(6)"
|
||||
```
|
||||
|
||||
#### P({1, 4}):
|
||||
|
||||
```javascript
|
||||
var p = new Fraction([1, 4].length, 6).toString(); // "0.(3)"
|
||||
```
|
||||
|
||||
#### P({2, 4, 6}):
|
||||
|
||||
```javascript
|
||||
var p = new Fraction([2, 4, 6].length, 6).toString(); // "0.5"
|
||||
```
|
||||
|
||||
### Convert degrees/minutes/seconds to precise rational representation:
|
||||
|
||||
57+45/60+17/3600
|
||||
|
||||
```javascript
|
||||
var deg = 57; // 57°
|
||||
var min = 45; // 45 Minutes
|
||||
var sec = 17; // 17 Seconds
|
||||
|
||||
new Fraction(deg).add(min, 60).add(sec, 3600).toString() // -> 57.7547(2)
|
||||
```
|
||||
|
||||
|
||||
### Rational approximation of irrational numbers
|
||||
|
||||
To approximate a number like *sqrt(5) - 2* with a numerator and denominator, you can reformat the equation as follows: *pow(n / d + 2, 2) = 5*.
|
||||
|
||||
Then the following algorithm will generate the rational number besides the binary representation.
|
||||
|
||||
```javascript
|
||||
var x = "/", s = "";
|
||||
|
||||
var a = new Fraction(0),
|
||||
b = new Fraction(1);
|
||||
for (var n = 0; n <= 10; n++) {
|
||||
|
||||
var c = a.add(b).div(2);
|
||||
|
||||
console.log(n + "\t" + a + "\t" + b + "\t" + c + "\t" + x);
|
||||
|
||||
if (c.add(2).pow(2).valueOf() < 5) {
|
||||
a = c;
|
||||
x = "1";
|
||||
} else {
|
||||
b = c;
|
||||
x = "0";
|
||||
}
|
||||
s+= x;
|
||||
}
|
||||
console.log(s)
|
||||
```
|
||||
|
||||
The result is
|
||||
|
||||
```
|
||||
n a[n] b[n] c[n] x[n]
|
||||
0 0/1 1/1 1/2 /
|
||||
1 0/1 1/2 1/4 0
|
||||
2 0/1 1/4 1/8 0
|
||||
3 1/8 1/4 3/16 1
|
||||
4 3/16 1/4 7/32 1
|
||||
5 7/32 1/4 15/64 1
|
||||
6 15/64 1/4 31/128 1
|
||||
7 15/64 31/128 61/256 0
|
||||
8 15/64 61/256 121/512 0
|
||||
9 15/64 121/512 241/1024 0
|
||||
10 241/1024 121/512 483/2048 1
|
||||
```
|
||||
|
||||
Thus the approximation after 11 iterations of the bisection method is *483 / 2048* and the binary representation is 0.00111100011 (see [WolframAlpha](http://www.wolframalpha.com/input/?i=sqrt%285%29-2+binary))
|
||||
|
||||
I published another example on how to approximate PI with fraction.js on my [blog](https://raw.org/article/rational-numbers-in-javascript/) (Still not the best idea to approximate irrational numbers, but it illustrates the capabilities of Fraction.js perfectly).
|
||||
|
||||
|
||||
### Get the exact fractional part of a number
|
||||
|
||||
```javascript
|
||||
var f = new Fraction("-6.(3416)");
|
||||
console.log(f.mod(1).abs().toFraction()); // = 3416/9999
|
||||
```
|
||||
|
||||
### Mathematical correct modulo
|
||||
|
||||
The behaviour on negative congruences is different to most modulo implementations in computer science. Even the *mod()* function of Fraction.js behaves in the typical way. To solve the problem of having the mathematical correct modulo with Fraction.js you could come up with this:
|
||||
|
||||
```javascript
|
||||
var a = -1;
|
||||
var b = 10.99;
|
||||
|
||||
console.log(new Fraction(a)
|
||||
.mod(b)); // Not correct, usual Modulo
|
||||
|
||||
console.log(new Fraction(a)
|
||||
.mod(b).add(b).mod(b)); // Correct! Mathematical Modulo
|
||||
```
|
||||
|
||||
fmod() imprecision circumvented
|
||||
---
|
||||
It turns out that Fraction.js outperforms almost any fmod() implementation, including JavaScript itself, [php.js](http://phpjs.org/functions/fmod/), C++, Python, Java and even Wolframalpha due to the fact that numbers like 0.05, 0.1, ... are infinite decimal in base 2.
|
||||
|
||||
The equation *fmod(4.55, 0.05)* gives *0.04999999999999957*, wolframalpha says *1/20*. The correct answer should be **zero**, as 0.05 divides 4.55 without any remainder.
|
||||
|
||||
|
||||
## Parser
|
||||
|
||||
Any function (see below) as well as the constructor of the *Fraction* class parses its input and reduce it to the smallest term.
|
||||
|
||||
You can pass either Arrays, Objects, Integers, Doubles or Strings.
|
||||
|
||||
### Arrays / Objects
|
||||
|
||||
```javascript
|
||||
new Fraction(numerator, denominator);
|
||||
new Fraction([numerator, denominator]);
|
||||
new Fraction({n: numerator, d: denominator});
|
||||
```
|
||||
|
||||
### Integers
|
||||
|
||||
```javascript
|
||||
new Fraction(123);
|
||||
```
|
||||
|
||||
### Doubles
|
||||
|
||||
```javascript
|
||||
new Fraction(55.4);
|
||||
```
|
||||
|
||||
**Note:** If you pass a double as it is, Fraction.js will perform a number analysis based on Farey Sequences. If you concern performance, cache Fraction.js objects and pass arrays/objects.
|
||||
|
||||
The method is really precise, but too large exact numbers, like 1234567.9991829 will result in a wrong approximation. If you want to keep the number as it is, convert it to a string, as the string parser will not perform any further observations. If you have problems with the approximation, in the file `examples/approx.js` is a different approximation algorithm, which might work better in some more specific use-cases.
|
||||
|
||||
|
||||
### Strings
|
||||
|
||||
```javascript
|
||||
new Fraction("123.45");
|
||||
new Fraction("123/45"); // A rational number represented as two decimals, separated by a slash
|
||||
new Fraction("123:45"); // A rational number represented as two decimals, separated by a colon
|
||||
new Fraction("4 123/45"); // A rational number represented as a whole number and a fraction
|
||||
new Fraction("123.'456'"); // Note the quotes, see below!
|
||||
new Fraction("123.(456)"); // Note the brackets, see below!
|
||||
new Fraction("123.45'6'"); // Note the quotes, see below!
|
||||
new Fraction("123.45(6)"); // Note the brackets, see below!
|
||||
```
|
||||
|
||||
### Two arguments
|
||||
|
||||
```javascript
|
||||
new Fraction(3, 2); // 3/2 = 1.5
|
||||
```
|
||||
|
||||
### Repeating decimal places
|
||||
|
||||
*Fraction.js* can easily handle repeating decimal places. For example *1/3* is *0.3333...*. There is only one repeating digit. As you can see in the examples above, you can pass a number like *1/3* as "0.'3'" or "0.(3)", which are synonym. There are no tests to parse something like 0.166666666 to 1/6! If you really want to handle this number, wrap around brackets on your own with the function below for example: 0.1(66666666)
|
||||
|
||||
Assume you want to divide 123.32 / 33.6(567). [WolframAlpha](http://www.wolframalpha.com/input/?i=123.32+%2F+%2812453%2F370%29) states that you'll get a period of 1776 digits. *Fraction.js* comes to the same result. Give it a try:
|
||||
|
||||
```javascript
|
||||
var f = new Fraction("123.32");
|
||||
console.log("Bam: " + f.div("33.6(567)"));
|
||||
```
|
||||
|
||||
To automatically make a number like "0.123123123" to something more Fraction.js friendly like "0.(123)", I hacked this little brute force algorithm in a 10 minutes. Improvements are welcome...
|
||||
|
||||
```javascript
|
||||
function formatDecimal(str) {
|
||||
|
||||
var comma, pre, offset, pad, times, repeat;
|
||||
|
||||
if (-1 === (comma = str.indexOf(".")))
|
||||
return str;
|
||||
|
||||
pre = str.substr(0, comma + 1);
|
||||
str = str.substr(comma + 1);
|
||||
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
|
||||
offset = str.substr(0, i);
|
||||
|
||||
for (var j = 0; j < 5; j++) {
|
||||
|
||||
pad = str.substr(i, j + 1);
|
||||
|
||||
times = Math.ceil((str.length - offset.length) / pad.length);
|
||||
|
||||
repeat = new Array(times + 1).join(pad); // Silly String.repeat hack
|
||||
|
||||
if (0 === (offset + repeat).indexOf(str)) {
|
||||
return pre + offset + "(" + pad + ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
var f, x = formatDecimal("13.0123123123"); // = 13.0(123)
|
||||
if (x !== null) {
|
||||
f = new Fraction(x);
|
||||
}
|
||||
```
|
||||
|
||||
## Attributes
|
||||
|
||||
|
||||
The Fraction object allows direct access to the numerator, denominator and sign attributes. It is ensured that only the sign-attribute holds sign information so that a sign comparison is only necessary against this attribute.
|
||||
|
||||
```javascript
|
||||
var f = new Fraction('-1/2');
|
||||
console.log(f.n); // Numerator: 1
|
||||
console.log(f.d); // Denominator: 2
|
||||
console.log(f.s); // Sign: -1
|
||||
```
|
||||
|
||||
|
||||
## Functions
|
||||
|
||||
### Fraction abs()
|
||||
|
||||
Returns the actual number without any sign information
|
||||
|
||||
### Fraction neg()
|
||||
|
||||
Returns the actual number with flipped sign in order to get the additive inverse
|
||||
|
||||
### Fraction add(n)
|
||||
|
||||
Returns the sum of the actual number and the parameter n
|
||||
|
||||
### Fraction sub(n)
|
||||
|
||||
Returns the difference of the actual number and the parameter n
|
||||
|
||||
### Fraction mul(n)
|
||||
|
||||
Returns the product of the actual number and the parameter n
|
||||
|
||||
### Fraction div(n)
|
||||
|
||||
Returns the quotient of the actual number and the parameter n
|
||||
|
||||
### Fraction pow(exp)
|
||||
|
||||
Returns the power of the actual number, raised to an possible rational exponent. If the result becomes non-rational the function returns `null`.
|
||||
|
||||
### Fraction log(base)
|
||||
|
||||
Returns the logarithm of the actual number to a given rational base. If the result becomes non-rational the function returns `null`.
|
||||
|
||||
### Fraction mod(n)
|
||||
|
||||
Returns the modulus (rest of the division) of the actual object and n (this % n). It's a much more precise [fmod()](#fmod-impreciseness-circumvented) if you like. Please note that *mod()* is just like the modulo operator of most programming languages. If you want a mathematical correct modulo, see [here](#mathematical-correct-modulo).
|
||||
|
||||
### Fraction mod()
|
||||
|
||||
Returns the modulus (rest of the division) of the actual object (numerator mod denominator)
|
||||
|
||||
### Fraction gcd(n)
|
||||
|
||||
Returns the fractional greatest common divisor
|
||||
|
||||
### Fraction lcm(n)
|
||||
|
||||
Returns the fractional least common multiple
|
||||
|
||||
### Fraction ceil([places=0-16])
|
||||
|
||||
Returns the ceiling of a rational number with Math.ceil
|
||||
|
||||
### Fraction floor([places=0-16])
|
||||
|
||||
Returns the floor of a rational number with Math.floor
|
||||
|
||||
### Fraction round([places=0-16])
|
||||
|
||||
Returns the rational number rounded with Math.round
|
||||
|
||||
### Fraction roundTo(multiple)
|
||||
|
||||
Rounds a fraction to the closest multiple of another fraction.
|
||||
|
||||
### Fraction inverse()
|
||||
|
||||
Returns the multiplicative inverse of the actual number (n / d becomes d / n) in order to get the reciprocal
|
||||
|
||||
### Fraction simplify([eps=0.001])
|
||||
|
||||
Simplifies the rational number under a certain error threshold. Ex. `0.333` will be `1/3` with `eps=0.001`
|
||||
|
||||
### boolean equals(n)
|
||||
|
||||
Check if two rational numbers are equal
|
||||
|
||||
### boolean lt(n)
|
||||
|
||||
Check if this rational number is less than another
|
||||
|
||||
### boolean lte(n)
|
||||
|
||||
Check if this rational number is less than or equal another
|
||||
|
||||
### boolean gt(n)
|
||||
|
||||
Check if this rational number is greater than another
|
||||
|
||||
### boolean gte(n)
|
||||
|
||||
Check if this rational number is greater than or equal another
|
||||
|
||||
### int compare(n)
|
||||
|
||||
Compare two numbers.
|
||||
```
|
||||
result < 0: n is greater than actual number
|
||||
result > 0: n is smaller than actual number
|
||||
result = 0: n is equal to the actual number
|
||||
```
|
||||
|
||||
### boolean divisible(n)
|
||||
|
||||
Check if two numbers are divisible (n divides this)
|
||||
|
||||
### double valueOf()
|
||||
|
||||
Returns a decimal representation of the fraction
|
||||
|
||||
### String toString([decimalPlaces=15])
|
||||
|
||||
Generates an exact string representation of the given object. For repeating decimal places, digits within repeating cycles are enclosed in parentheses, e.g., `1/3 = "0.(3)"`. For other numbers, the string will include up to the specified `decimalPlaces` significant digits, including any trailing zeros if truncation occurs. For example, `1/2` will be represented as `"0.5"`, without additional trailing zeros.
|
||||
|
||||
**Note:** Since both `valueOf()` and `toString()` are provided, `toString()` will only be invoked implicitly when the object is used in a string context. For instance, when using the plus operator like `"123" + new Fraction`, `valueOf()` will be called first, as JavaScript attempts to combine primitives before concatenating them, with the string type taking precedence. However, `alert(new Fraction)` or `String(new Fraction)` will behave as expected. To ensure specific behavior, explicitly call either `toString()` or `valueOf()`.
|
||||
|
||||
### String toLatex(showMixed=false)
|
||||
|
||||
Generates an exact LaTeX representation of the actual object. You can see a [live demo](https://raw.org/article/rational-numbers-in-javascript/) on my blog.
|
||||
|
||||
The optional boolean parameter indicates if you want to show the a mixed fraction. "1 1/3" instead of "4/3"
|
||||
|
||||
### String toFraction(showMixed=false)
|
||||
|
||||
Gets a string representation of the fraction
|
||||
|
||||
The optional boolean parameter indicates if you want to showa mixed fraction. "1 1/3" instead of "4/3"
|
||||
|
||||
### Array toContinued()
|
||||
|
||||
Gets an array of the fraction represented as a continued fraction. The first element always contains the whole part.
|
||||
|
||||
```javascript
|
||||
var f = new Fraction('88/33');
|
||||
var c = f.toContinued(); // [2, 1, 2]
|
||||
```
|
||||
|
||||
### Fraction clone()
|
||||
|
||||
Creates a copy of the actual Fraction object
|
||||
|
||||
|
||||
## Exceptions
|
||||
|
||||
If a really hard error occurs (parsing error, division by zero), *Fraction.js* throws exceptions! Please make sure you handle them correctly.
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
You can install `Fraction.js` via npm:
|
||||
|
||||
```bash
|
||||
npm install fraction.js
|
||||
```
|
||||
|
||||
Or with yarn:
|
||||
|
||||
```bash
|
||||
yarn add fraction.js
|
||||
```
|
||||
|
||||
Alternatively, download or clone the repository:
|
||||
|
||||
```bash
|
||||
git clone https://github.com/rawify/Fraction.js
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Include the `fraction.min.js` file in your project:
|
||||
|
||||
```html
|
||||
<script src="path/to/fraction.min.js"></script>
|
||||
<script>
|
||||
var x = new Fraction("13/4");
|
||||
</script>
|
||||
```
|
||||
|
||||
Or in a Node.js project:
|
||||
|
||||
```javascript
|
||||
const Fraction = require('fraction.js');
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```javascript
|
||||
import Fraction from 'fraction.js';
|
||||
```
|
||||
|
||||
|
||||
## Coding Style
|
||||
|
||||
As every library I publish, Fraction.js is also built to be as small as possible after compressing it with Google Closure Compiler in advanced mode. Thus the coding style orientates a little on maxing-out the compression rate. Please make sure you keep this style if you plan to extend the library.
|
||||
|
||||
## Building the library
|
||||
|
||||
After cloning the Git repository run:
|
||||
|
||||
```bash
|
||||
npm install
|
||||
npm run build
|
||||
```
|
||||
|
||||
## Run a test
|
||||
|
||||
Testing the source against the shipped test suite is as easy as
|
||||
|
||||
```bash
|
||||
npm run test
|
||||
```
|
||||
|
||||
## Copyright and Licensing
|
||||
|
||||
Copyright (c) 2025, [Robert Eisele](https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
1045
node_modules/fraction.js/dist/fraction.js
generated
vendored
Normal file
1045
node_modules/fraction.js/dist/fraction.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
21
node_modules/fraction.js/dist/fraction.min.js
generated
vendored
Normal file
21
node_modules/fraction.js/dist/fraction.min.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
Fraction.js v5.3.4 8/22/2025
|
||||
https://raw.org/article/rational-numbers-in-javascript/
|
||||
|
||||
Copyright (c) 2025, Robert Eisele (https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
*/
|
||||
'use strict';(function(F){function D(){return Error("Parameters must be integer")}function x(){return Error("Invalid argument")}function C(){return Error("Division by Zero")}function q(a,b){var d=g,c=h;let f=h;if(void 0!==a&&null!==a)if(void 0!==b){if("bigint"===typeof a)d=a;else{if(isNaN(a))throw x();if(0!==a%1)throw D();d=BigInt(a)}if("bigint"===typeof b)c=b;else{if(isNaN(b))throw x();if(0!==b%1)throw D();c=BigInt(b)}f=d*c}else if("object"===typeof a){if("d"in a&&"n"in a)d=BigInt(a.n),c=BigInt(a.d),
|
||||
"s"in a&&(d*=BigInt(a.s));else if(0 in a)d=BigInt(a[0]),1 in a&&(c=BigInt(a[1]));else if("bigint"===typeof a)d=a;else throw x();f=d*c}else if("number"===typeof a){if(isNaN(a))throw x();0>a&&(f=-h,a=-a);if(0===a%1)d=BigInt(a);else{b=1;var k=0,l=1,m=1;let r=1;1<=a&&(b=10**Math.floor(1+Math.log10(a)),a/=b);for(;1E7>=l&&1E7>=r;)if(c=(k+m)/(l+r),a===c){1E7>=l+r?(d=k+m,c=l+r):r>l?(d=m,c=r):(d=k,c=l);break}else a>c?(k+=m,l+=r):(m+=k,r+=l),1E7<l?(d=m,c=r):(d=k,c=l);d=BigInt(d)*BigInt(b);c=BigInt(c)}}else if("string"===
|
||||
typeof a){c=0;k=b=d=g;l=m=h;a=a.replace(/_/g,"").match(/\d+|./g);if(null===a)throw x();"-"===a[c]?(f=-h,c++):"+"===a[c]&&c++;if(a.length===c+1)b=w(a[c++],f);else if("."===a[c+1]||"."===a[c]){"."!==a[c]&&(d=w(a[c++],f));c++;if(c+1===a.length||"("===a[c+1]&&")"===a[c+3]||"'"===a[c+1]&&"'"===a[c+3])b=w(a[c],f),m=t**BigInt(a[c].length),c++;if("("===a[c]&&")"===a[c+2]||"'"===a[c]&&"'"===a[c+2])k=w(a[c+1],f),l=t**BigInt(a[c+1].length)-h,c+=3}else"/"===a[c+1]||":"===a[c+1]?(b=w(a[c],f),m=w(a[c+2],h),c+=
|
||||
3):"/"===a[c+3]&&" "===a[c+1]&&(d=w(a[c],f),b=w(a[c+2],f),m=w(a[c+4],h),c+=5);if(a.length<=c)c=m*l,f=d=k+c*d+l*b;else throw x();}else if("bigint"===typeof a)f=d=a,c=h;else throw x();if(c===g)throw C();e.s=f<g?-h:h;e.n=d<g?-d:d;e.d=c<g?-c:c}function w(a,b){try{a=BigInt(a)}catch(d){throw x();}return a*b}function u(a){return"bigint"===typeof a?a:Math.floor(a)}function n(a,b){if(b===g)throw C();const d=Object.create(v.prototype);d.s=a<g?-h:h;a=a<g?-a:a;const c=y(a,b);d.n=a/c;d.d=b/c;return d}function A(a){const b=
|
||||
Object.create(null);if(a<=h)return b[a]=h,b;for(;a%p===g;)b[p]=(b[p]||g)+h,a/=p;for(;a%B===g;)b[B]=(b[B]||g)+h,a/=B;for(;a%z===g;)b[z]=(b[z]||g)+h,a/=z;for(let d=0,c=p+z;c*c<=a;){for(;a%c===g;)b[c]=(b[c]||g)+h,a/=c;c+=G[d];d=d+1&7}a>h&&(b[a]=(b[a]||g)+h);return b}function y(a,b){if(!a)return b;if(!b)return a;for(;;){a%=b;if(!a)return b;b%=a;if(!b)return a}}function v(a,b){q(a,b);if(this instanceof v)a=y(e.d,e.n),this.s=e.s,this.n=e.n/a,this.d=e.d/a;else return n(e.s*e.n,e.d)}"undefined"===typeof BigInt&&
|
||||
(BigInt=function(a){if(isNaN(a))throw Error("");return a});const g=BigInt(0),h=BigInt(1),p=BigInt(2),B=BigInt(3),z=BigInt(5),t=BigInt(10),e={s:h,n:g,d:h},G=[p*p,p,p*p,p,p*p,p*B,p,p*B];v.prototype={s:h,n:g,d:h,abs:function(){return n(this.n,this.d)},neg:function(){return n(-this.s*this.n,this.d)},add:function(a,b){q(a,b);return n(this.s*this.n*e.d+e.s*this.d*e.n,this.d*e.d)},sub:function(a,b){q(a,b);return n(this.s*this.n*e.d-e.s*this.d*e.n,this.d*e.d)},mul:function(a,b){q(a,b);return n(this.s*e.s*
|
||||
this.n*e.n,this.d*e.d)},div:function(a,b){q(a,b);return n(this.s*e.s*this.n*e.d,this.d*e.n)},clone:function(){return n(this.s*this.n,this.d)},mod:function(a,b){if(void 0===a)return n(this.s*this.n%this.d,h);q(a,b);if(g===e.n*this.d)throw C();return n(this.s*e.d*this.n%(e.n*this.d),e.d*this.d)},gcd:function(a,b){q(a,b);return n(y(e.n,this.n)*y(e.d,this.d),e.d*this.d)},lcm:function(a,b){q(a,b);return e.n===g&&this.n===g?n(g,h):n(e.n*this.n,y(e.n,this.n)*y(e.d,this.d))},inverse:function(){return n(this.s*
|
||||
this.d,this.n)},pow:function(a,b){q(a,b);if(e.d===h)return e.s<g?n((this.s*this.d)**e.n,this.n**e.n):n((this.s*this.n)**e.n,this.d**e.n);if(this.s<g)return null;a=A(this.n);b=A(this.d);let d=h,c=h;for(let f in a)if("1"!==f){if("0"===f){d=g;break}a[f]*=e.n;if(a[f]%e.d===g)a[f]/=e.d;else return null;d*=BigInt(f)**a[f]}for(let f in b)if("1"!==f){b[f]*=e.n;if(b[f]%e.d===g)b[f]/=e.d;else return null;c*=BigInt(f)**b[f]}return e.s<g?n(c,d):n(d,c)},log:function(a,b){q(a,b);if(this.s<=g||e.s<=g)return null;
|
||||
var d=Object.create(null);a=A(e.n);const c=A(e.d);b=A(this.n);const f=A(this.d);for(var k in c)a[k]=(a[k]||g)-c[k];for(var l in f)b[l]=(b[l]||g)-f[l];for(var m in a)"1"!==m&&(d[m]=!0);for(var r in b)"1"!==r&&(d[r]=!0);l=k=null;for(const E in d)if(m=a[E]||g,d=b[E]||g,m===g){if(d!==g)return null}else if(r=y(d,m),d/=r,m/=r,null===k&&null===l)k=d,l=m;else if(d*l!==k*m)return null;return null!==k&&null!==l?n(k,l):null},equals:function(a,b){q(a,b);return this.s*this.n*e.d===e.s*e.n*this.d},lt:function(a,
|
||||
b){q(a,b);return this.s*this.n*e.d<e.s*e.n*this.d},lte:function(a,b){q(a,b);return this.s*this.n*e.d<=e.s*e.n*this.d},gt:function(a,b){q(a,b);return this.s*this.n*e.d>e.s*e.n*this.d},gte:function(a,b){q(a,b);return this.s*this.n*e.d>=e.s*e.n*this.d},compare:function(a,b){q(a,b);a=this.s*this.n*e.d-e.s*e.n*this.d;return(g<a)-(a<g)},ceil:function(a){a=t**BigInt(a||0);return n(u(this.s*a*this.n/this.d)+(a*this.n%this.d>g&&this.s>=g?h:g),a)},floor:function(a){a=t**BigInt(a||0);return n(u(this.s*a*this.n/
|
||||
this.d)-(a*this.n%this.d>g&&this.s<g?h:g),a)},round:function(a){a=t**BigInt(a||0);return n(u(this.s*a*this.n/this.d)+this.s*((this.s>=g?h:g)+a*this.n%this.d*p>this.d?h:g),a)},roundTo:function(a,b){q(a,b);var d=this.n*e.d;a=this.d*e.n;b=d%a;d=u(d/a);b+b>=a&&d++;return n(this.s*d*e.n,e.d)},divisible:function(a,b){q(a,b);return e.n===g?!1:this.n*e.d%(e.n*this.d)===g},valueOf:function(){return Number(this.s*this.n)/Number(this.d)},toString:function(a=15){let b=this.n,d=this.d;var c;a:{for(c=d;c%p===g;c/=
|
||||
p);for(;c%z===g;c/=z);if(c===h)c=g;else{for(var f=t%c,k=1;f!==h;k++)if(f=f*t%c,2E3<k){c=g;break a}c=BigInt(k)}}a:{f=h;k=t;var l=c;let m=h;for(;l>g;k=k*k%d,l>>=h)l&h&&(m=m*k%d);k=m;for(l=0;300>l;l++){if(f===k){f=BigInt(l);break a}f=f*t%d;k=k*t%d}f=0}k=f;f=this.s<g?"-":"";f+=u(b/d);(b=b%d*t)&&(f+=".");if(c){for(a=k;a--;)f+=u(b/d),b%=d,b*=t;f+="(";for(a=c;a--;)f+=u(b/d),b%=d,b*=t;f+=")"}else for(;b&&a--;)f+=u(b/d),b%=d,b*=t;return f},toFraction:function(a=!1){let b=this.n,d=this.d,c=this.s<g?"-":"";
|
||||
if(d===h)c+=b;else{const f=u(b/d);a&&f>g&&(c+=f,c+=" ",b%=d);c=c+b+"/"+d}return c},toLatex:function(a=!1){let b=this.n,d=this.d,c=this.s<g?"-":"";if(d===h)c+=b;else{const f=u(b/d);a&&f>g&&(c+=f,b%=d);c=c+"\\frac{"+b+"}{"+d;c+="}"}return c},toContinued:function(){let a=this.n,b=this.d;const d=[];for(;b;){d.push(u(a/b));const c=a%b;a=b;b=c}return d},simplify:function(a=.001){a=BigInt(Math.ceil(1/a));const b=this.abs(),d=b.toContinued();for(let f=1;f<d.length;f++){let k=n(d[f-1],h);for(var c=f-2;0<=
|
||||
c;c--)k=k.inverse().add(d[c]);c=k.sub(b);if(c.n*a<c.d)return k.mul(this.s)}return this}};"function"===typeof define&&define.amd?define([],function(){return v}):"object"===typeof exports?(Object.defineProperty(v,"__esModule",{value:!0}),v["default"]=v,v.Fraction=v,module.exports=v):F.Fraction=v})(this);
|
||||
1043
node_modules/fraction.js/dist/fraction.mjs
generated
vendored
Normal file
1043
node_modules/fraction.js/dist/fraction.mjs
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
26
node_modules/fraction.js/examples/angles.js
generated
vendored
Normal file
26
node_modules/fraction.js/examples/angles.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
Fraction.js v5.0.0 10/1/2024
|
||||
https://raw.org/article/rational-numbers-in-javascript/
|
||||
|
||||
Copyright (c) 2024, Robert Eisele (https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
*/
|
||||
|
||||
// This example generates a list of angles with human readable radians
|
||||
|
||||
var Fraction = require('fraction.js');
|
||||
|
||||
var tab = [];
|
||||
for (var d = 1; d <= 360; d++) {
|
||||
|
||||
var pi = Fraction(2, 360).mul(d);
|
||||
var tau = Fraction(1, 360).mul(d);
|
||||
|
||||
if (pi.d <= 6n && pi.d != 5n)
|
||||
tab.push([
|
||||
d,
|
||||
pi.toFraction() + "pi",
|
||||
tau.toFraction() + "tau"]);
|
||||
}
|
||||
|
||||
console.table(tab);
|
||||
54
node_modules/fraction.js/examples/approx.js
generated
vendored
Normal file
54
node_modules/fraction.js/examples/approx.js
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Fraction.js v5.0.0 10/1/2024
|
||||
https://raw.org/article/rational-numbers-in-javascript/
|
||||
|
||||
Copyright (c) 2024, Robert Eisele (https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
*/
|
||||
const Fraction = require('fraction.js');
|
||||
|
||||
// Another rational approximation, not using Farey Sequences but Binary Search using the mediant
|
||||
function approximate(p, precision) {
|
||||
|
||||
var num1 = Math.floor(p);
|
||||
var den1 = 1;
|
||||
|
||||
var num2 = num1 + 1;
|
||||
var den2 = 1;
|
||||
|
||||
if (p !== num1) {
|
||||
|
||||
while (den1 <= precision && den2 <= precision) {
|
||||
|
||||
var m = (num1 + num2) / (den1 + den2);
|
||||
|
||||
if (p === m) {
|
||||
|
||||
if (den1 + den2 <= precision) {
|
||||
den1 += den2;
|
||||
num1 += num2;
|
||||
den2 = precision + 1;
|
||||
} else if (den1 > den2) {
|
||||
den2 = precision + 1;
|
||||
} else {
|
||||
den1 = precision + 1;
|
||||
}
|
||||
break;
|
||||
|
||||
} else if (p < m) {
|
||||
num2 += num1;
|
||||
den2 += den1;
|
||||
} else {
|
||||
num1 += num2;
|
||||
den1 += den2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (den1 > precision) {
|
||||
den1 = den2;
|
||||
num1 = num2;
|
||||
}
|
||||
return new Fraction(num1, den1);
|
||||
}
|
||||
|
||||
24
node_modules/fraction.js/examples/egyptian.js
generated
vendored
Normal file
24
node_modules/fraction.js/examples/egyptian.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
Fraction.js v5.0.0 10/1/2024
|
||||
https://raw.org/article/rational-numbers-in-javascript/
|
||||
|
||||
Copyright (c) 2024, Robert Eisele (https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
*/
|
||||
const Fraction = require('fraction.js');
|
||||
|
||||
// Based on http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fractions/egyptian.html
|
||||
function egyptian(a, b) {
|
||||
|
||||
var res = [];
|
||||
|
||||
do {
|
||||
var t = Math.ceil(b / a);
|
||||
var x = new Fraction(a, b).sub(1, t);
|
||||
res.push(t);
|
||||
a = Number(x.n);
|
||||
b = Number(x.d);
|
||||
} while (a !== 0n);
|
||||
return res;
|
||||
}
|
||||
console.log("1 / " + egyptian(521, 1050).join(" + 1 / "));
|
||||
111
node_modules/fraction.js/examples/hesse-convergence.js
generated
vendored
Normal file
111
node_modules/fraction.js/examples/hesse-convergence.js
generated
vendored
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
Fraction.js v5.0.0 10/1/2024
|
||||
https://raw.org/article/rational-numbers-in-javascript/
|
||||
|
||||
Copyright (c) 2024, Robert Eisele (https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
*/
|
||||
const Fraction = require('fraction.js');
|
||||
|
||||
/*
|
||||
We have the polynom f(x) = 1/3x_1^2 + x_2^2 + x_1 * x_2 + 3
|
||||
|
||||
The gradient of f(x):
|
||||
|
||||
grad(x) = | x_1^2+x_2 |
|
||||
| 2x_2+x_1 |
|
||||
|
||||
And thus the Hesse-Matrix H:
|
||||
| 2x_1 1 |
|
||||
| 1 2 |
|
||||
|
||||
The inverse Hesse-Matrix H^-1 is
|
||||
| -2 / (1-4x_1) 1 / (1 - 4x_1) |
|
||||
| 1 / (1 - 4x_1) -2x_1 / (1 - 4x_1) |
|
||||
|
||||
We now want to find lim ->oo x[n], with the starting element of (3 2)^T
|
||||
|
||||
*/
|
||||
|
||||
// Get the Hesse Matrix
|
||||
function H(x) {
|
||||
|
||||
var z = Fraction(1).sub(Fraction(4).mul(x[0]));
|
||||
|
||||
return [
|
||||
Fraction(-2).div(z),
|
||||
Fraction(1).div(z),
|
||||
Fraction(1).div(z),
|
||||
Fraction(-2).mul(x[0]).div(z),
|
||||
];
|
||||
}
|
||||
|
||||
// Get the gradient of f(x)
|
||||
function grad(x) {
|
||||
|
||||
return [
|
||||
Fraction(x[0]).mul(x[0]).add(x[1]),
|
||||
Fraction(2).mul(x[1]).add(x[0])
|
||||
];
|
||||
}
|
||||
|
||||
// A simple matrix multiplication helper
|
||||
function matrMult(m, v) {
|
||||
|
||||
return [
|
||||
Fraction(m[0]).mul(v[0]).add(Fraction(m[1]).mul(v[1])),
|
||||
Fraction(m[2]).mul(v[0]).add(Fraction(m[3]).mul(v[1]))
|
||||
];
|
||||
}
|
||||
|
||||
// A simple vector subtraction helper
|
||||
function vecSub(a, b) {
|
||||
|
||||
return [
|
||||
Fraction(a[0]).sub(b[0]),
|
||||
Fraction(a[1]).sub(b[1])
|
||||
];
|
||||
}
|
||||
|
||||
// Main function, gets a vector and the actual index
|
||||
function run(V, j) {
|
||||
|
||||
var t = H(V);
|
||||
//console.log("H(X)");
|
||||
for (var i in t) {
|
||||
|
||||
// console.log(t[i].toFraction());
|
||||
}
|
||||
|
||||
var s = grad(V);
|
||||
//console.log("vf(X)");
|
||||
for (var i in s) {
|
||||
|
||||
// console.log(s[i].toFraction());
|
||||
}
|
||||
|
||||
//console.log("multiplication");
|
||||
var r = matrMult(t, s);
|
||||
for (var i in r) {
|
||||
|
||||
// console.log(r[i].toFraction());
|
||||
}
|
||||
|
||||
var R = (vecSub(V, r));
|
||||
|
||||
console.log("X" + j);
|
||||
console.log(R[0].toFraction(), "= " + R[0].valueOf());
|
||||
console.log(R[1].toFraction(), "= " + R[1].valueOf());
|
||||
console.log("\n");
|
||||
|
||||
return R;
|
||||
}
|
||||
|
||||
|
||||
// Set the starting vector
|
||||
var v = [3, 2];
|
||||
|
||||
for (var i = 0; i < 15; i++) {
|
||||
|
||||
v = run(v, i);
|
||||
}
|
||||
67
node_modules/fraction.js/examples/integrate.js
generated
vendored
Normal file
67
node_modules/fraction.js/examples/integrate.js
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
Fraction.js v5.0.0 10/1/2024
|
||||
https://raw.org/article/rational-numbers-in-javascript/
|
||||
|
||||
Copyright (c) 2024, Robert Eisele (https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
*/
|
||||
const Fraction = require('fraction.js');
|
||||
|
||||
// NOTE: This is a nice example, but a stable version of this is served with Polynomial.js:
|
||||
// https://github.com/rawify/Polynomial.js
|
||||
|
||||
function integrate(poly) {
|
||||
|
||||
poly = poly.replace(/\s+/g, "");
|
||||
|
||||
var regex = /(\([+-]?[0-9/]+\)|[+-]?[0-9/]+)x(?:\^(\([+-]?[0-9/]+\)|[+-]?[0-9]+))?/g;
|
||||
var arr;
|
||||
var res = {};
|
||||
while (null !== (arr = regex.exec(poly))) {
|
||||
|
||||
var a = (arr[1] || "1").replace("(", "").replace(")", "").split("/");
|
||||
var b = (arr[2] || "1").replace("(", "").replace(")", "").split("/");
|
||||
|
||||
var exp = new Fraction(b).add(1);
|
||||
var key = "" + exp;
|
||||
|
||||
if (res[key] !== undefined) {
|
||||
res[key] = { x: new Fraction(a).div(exp).add(res[key].x), e: exp };
|
||||
} else {
|
||||
res[key] = { x: new Fraction(a).div(exp), e: exp };
|
||||
}
|
||||
}
|
||||
|
||||
var str = "";
|
||||
var c = 0;
|
||||
for (var i in res) {
|
||||
if (res[i].x.s !== -1n && c > 0) {
|
||||
str += "+";
|
||||
} else if (res[i].x.s === -1n) {
|
||||
str += "-";
|
||||
}
|
||||
if (res[i].x.n !== res[i].x.d) {
|
||||
if (res[i].x.d !== 1n) {
|
||||
str += res[i].x.n + "/" + res[i].x.d;
|
||||
} else {
|
||||
str += res[i].x.n;
|
||||
}
|
||||
}
|
||||
str += "x";
|
||||
if (res[i].e.n !== res[i].e.d) {
|
||||
str += "^";
|
||||
if (res[i].e.d !== 1n) {
|
||||
str += "(" + res[i].e.n + "/" + res[i].e.d + ")";
|
||||
} else {
|
||||
str += res[i].e.n;
|
||||
}
|
||||
}
|
||||
c++;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
var poly = "-2/3x^3-2x^2+3x+8x^3-1/3x^(4/8)";
|
||||
|
||||
console.log("f(x): " + poly);
|
||||
console.log("F(x): " + integrate(poly));
|
||||
24
node_modules/fraction.js/examples/ratio-chain.js
generated
vendored
Normal file
24
node_modules/fraction.js/examples/ratio-chain.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
Given the ratio a : b : c = 2 : 3 : 4
|
||||
What is c, given a = 40?
|
||||
|
||||
A general ratio chain is a_1 : a_2 : a_3 : ... : a_n = r_1 : r2 : r_3 : ... : r_n.
|
||||
Now each term can be expressed as a_i = r_i * x for some unknown proportional constant x.
|
||||
If a_k is known it follows that x = a_k / r_k. Substituting x into the first equation yields
|
||||
a_i = r_i / r_k * a_k.
|
||||
|
||||
Given an array r and a given value a_k, the following function calculates all a_i:
|
||||
*/
|
||||
|
||||
function calculateRatios(r, a_k, k) {
|
||||
const x = Fraction(a_k).div(r[k]);
|
||||
return r.map(r_i => x.mul(r_i));
|
||||
}
|
||||
|
||||
// Example usage:
|
||||
const r = [2, 3, 4]; // Ratio array representing a : b : c = 2 : 3 : 4
|
||||
const a_k = 40; // Given value of a (corresponding to r[0])
|
||||
const k = 0; // Index of the known value (a corresponds to r[0])
|
||||
|
||||
const result = calculateRatios(r, a_k, k);
|
||||
console.log(result); // Output: [40, 60, 80]
|
||||
29
node_modules/fraction.js/examples/rational-pow.js
generated
vendored
Normal file
29
node_modules/fraction.js/examples/rational-pow.js
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
Fraction.js v5.0.0 10/1/2024
|
||||
https://raw.org/article/rational-numbers-in-javascript/
|
||||
|
||||
Copyright (c) 2024, Robert Eisele (https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
*/
|
||||
const Fraction = require('fraction.js');
|
||||
|
||||
// Calculates (a/b)^(c/d) if result is rational
|
||||
// Derivation: https://raw.org/book/analysis/rational-numbers/
|
||||
function root(a, b, c, d) {
|
||||
|
||||
// Initial estimate
|
||||
let x = Fraction(100 * (Math.floor(Math.pow(a / b, c / d)) || 1), 100);
|
||||
const abc = Fraction(a, b).pow(c);
|
||||
|
||||
for (let i = 0; i < 30; i++) {
|
||||
const n = abc.mul(x.pow(1 - d)).sub(x).div(d).add(x)
|
||||
|
||||
if (x.n === n.n && x.d === n.d) {
|
||||
return n;
|
||||
}
|
||||
x = n;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
root(18, 2, 1, 2); // 3/1
|
||||
16
node_modules/fraction.js/examples/tape-measure.js
generated
vendored
Normal file
16
node_modules/fraction.js/examples/tape-measure.js
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
Fraction.js v5.0.0 10/1/2024
|
||||
https://raw.org/article/rational-numbers-in-javascript/
|
||||
|
||||
Copyright (c) 2024, Robert Eisele (https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
*/
|
||||
const Fraction = require('fraction.js');
|
||||
|
||||
function closestTapeMeasure(frac) {
|
||||
|
||||
// A tape measure is usually divided in parts of 1/16
|
||||
|
||||
return Fraction(frac).roundTo("1/16");
|
||||
}
|
||||
console.log(closestTapeMeasure("1/3")); // 5/16
|
||||
35
node_modules/fraction.js/examples/toFraction.js
generated
vendored
Normal file
35
node_modules/fraction.js/examples/toFraction.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
Fraction.js v5.0.0 10/1/2024
|
||||
https://raw.org/article/rational-numbers-in-javascript/
|
||||
|
||||
Copyright (c) 2024, Robert Eisele (https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
*/
|
||||
|
||||
const Fraction = require('fraction.js');
|
||||
|
||||
function toFraction(frac) {
|
||||
|
||||
var map = {
|
||||
'1:4': "¼",
|
||||
'1:2': "½",
|
||||
'3:4': "¾",
|
||||
'1:7': "⅐",
|
||||
'1:9': "⅑",
|
||||
'1:10': "⅒",
|
||||
'1:3': "⅓",
|
||||
'2:3': "⅔",
|
||||
'1:5': "⅕",
|
||||
'2:5': "⅖",
|
||||
'3:5': "⅗",
|
||||
'4:5': "⅘",
|
||||
'1:6': "⅙",
|
||||
'5:6': "⅚",
|
||||
'1:8': "⅛",
|
||||
'3:8': "⅜",
|
||||
'5:8': "⅝",
|
||||
'7:8': "⅞"
|
||||
};
|
||||
return map[frac.n + ":" + frac.d] || frac.toFraction(false);
|
||||
}
|
||||
console.log(toFraction(Fraction(0.25))); // ¼
|
||||
42
node_modules/fraction.js/examples/valueOfPi.js
generated
vendored
Normal file
42
node_modules/fraction.js/examples/valueOfPi.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
Fraction.js v5.0.0 10/1/2024
|
||||
https://raw.org/article/rational-numbers-in-javascript/
|
||||
|
||||
Copyright (c) 2024, Robert Eisele (https://raw.org/)
|
||||
Licensed under the MIT license.
|
||||
*/
|
||||
|
||||
var Fraction = require("fraction.js")
|
||||
|
||||
function valueOfPi(val) {
|
||||
|
||||
let minLen = Infinity, minI = 0, min = null;
|
||||
const choose = [val, val * Math.PI, val / Math.PI];
|
||||
for (let i = 0; i < choose.length; i++) {
|
||||
let el = new Fraction(choose[i]).simplify(1e-13);
|
||||
let len = Math.log(Number(el.n) + 1) + Math.log(Number(el.d));
|
||||
if (len < minLen) {
|
||||
minLen = len;
|
||||
minI = i;
|
||||
min = el;
|
||||
}
|
||||
}
|
||||
|
||||
if (minI == 2) {
|
||||
return min.toFraction().replace(/(\d+)(\/\d+)?/, (_, p, q) =>
|
||||
(p == "1" ? "" : p) + "π" + (q || ""));
|
||||
}
|
||||
|
||||
if (minI == 1) {
|
||||
return min.toFraction().replace(/(\d+)(\/\d+)?/, (_, p, q) =>
|
||||
p + (!q ? "/π" : "/(" + q.slice(1) + "π)"));
|
||||
}
|
||||
return min.toFraction();
|
||||
}
|
||||
|
||||
console.log(valueOfPi(-3)); // -3
|
||||
console.log(valueOfPi(4 * Math.PI)); // 4π
|
||||
console.log(valueOfPi(3.14)); // 157/50
|
||||
console.log(valueOfPi(3 / 2 * Math.PI)); // 3π/2
|
||||
console.log(valueOfPi(Math.PI / 2)); // π/2
|
||||
console.log(valueOfPi(-1 / (2 * Math.PI))); // -1/(2π)
|
||||
79
node_modules/fraction.js/fraction.d.mts
generated
vendored
Normal file
79
node_modules/fraction.js/fraction.d.mts
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Interface representing a fraction with numerator and denominator.
|
||||
*/
|
||||
export interface NumeratorDenominator {
|
||||
n: number | bigint;
|
||||
d: number | bigint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Type for handling multiple types of input for Fraction operations.
|
||||
*/
|
||||
export type FractionInput =
|
||||
| Fraction
|
||||
| number
|
||||
| bigint
|
||||
| string
|
||||
| [number | bigint | string, number | bigint | string]
|
||||
| NumeratorDenominator;
|
||||
|
||||
/**
|
||||
* Function signature for Fraction operations like add, sub, mul, etc.
|
||||
*/
|
||||
export type FractionParam = {
|
||||
(numerator: number | bigint, denominator: number | bigint): Fraction;
|
||||
(num: FractionInput): Fraction;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fraction class representing a rational number with numerator and denominator.
|
||||
*/
|
||||
declare class Fraction {
|
||||
constructor();
|
||||
constructor(num: FractionInput);
|
||||
constructor(numerator: number | bigint, denominator: number | bigint);
|
||||
|
||||
s: bigint;
|
||||
n: bigint;
|
||||
d: bigint;
|
||||
|
||||
abs(): Fraction;
|
||||
neg(): Fraction;
|
||||
|
||||
add: FractionParam;
|
||||
sub: FractionParam;
|
||||
mul: FractionParam;
|
||||
div: FractionParam;
|
||||
pow: FractionParam;
|
||||
log: FractionParam;
|
||||
gcd: FractionParam;
|
||||
lcm: FractionParam;
|
||||
|
||||
mod(): Fraction;
|
||||
mod(num: FractionInput): Fraction;
|
||||
|
||||
ceil(places?: number): Fraction;
|
||||
floor(places?: number): Fraction;
|
||||
round(places?: number): Fraction;
|
||||
roundTo: FractionParam;
|
||||
|
||||
inverse(): Fraction;
|
||||
simplify(eps?: number): Fraction;
|
||||
|
||||
equals(num: FractionInput): boolean;
|
||||
lt(num: FractionInput): boolean;
|
||||
lte(num: FractionInput): boolean;
|
||||
gt(num: FractionInput): boolean;
|
||||
gte(num: FractionInput): boolean;
|
||||
compare(num: FractionInput): number;
|
||||
divisible(num: FractionInput): boolean;
|
||||
|
||||
valueOf(): number;
|
||||
toString(decimalPlaces?: number): string;
|
||||
toLatex(showMixed?: boolean): string;
|
||||
toFraction(showMixed?: boolean): string;
|
||||
toContinued(): bigint[];
|
||||
clone(): Fraction;
|
||||
}
|
||||
|
||||
export { Fraction as default, Fraction };
|
||||
79
node_modules/fraction.js/fraction.d.ts
generated
vendored
Normal file
79
node_modules/fraction.js/fraction.d.ts
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
declare class Fraction {
|
||||
constructor();
|
||||
constructor(num: Fraction.FractionInput);
|
||||
constructor(numerator: number | bigint, denominator: number | bigint);
|
||||
|
||||
s: bigint;
|
||||
n: bigint;
|
||||
d: bigint;
|
||||
|
||||
abs(): Fraction;
|
||||
neg(): Fraction;
|
||||
|
||||
add: Fraction.FractionParam;
|
||||
sub: Fraction.FractionParam;
|
||||
mul: Fraction.FractionParam;
|
||||
div: Fraction.FractionParam;
|
||||
pow: Fraction.FractionParam;
|
||||
log: Fraction.FractionParam;
|
||||
gcd: Fraction.FractionParam;
|
||||
lcm: Fraction.FractionParam;
|
||||
|
||||
mod(): Fraction;
|
||||
mod(num: Fraction.FractionInput): Fraction;
|
||||
|
||||
ceil(places?: number): Fraction;
|
||||
floor(places?: number): Fraction;
|
||||
round(places?: number): Fraction;
|
||||
roundTo: Fraction.FractionParam;
|
||||
|
||||
inverse(): Fraction;
|
||||
simplify(eps?: number): Fraction;
|
||||
|
||||
equals(num: Fraction.FractionInput): boolean;
|
||||
lt(num: Fraction.FractionInput): boolean;
|
||||
lte(num: Fraction.FractionInput): boolean;
|
||||
gt(num: Fraction.FractionInput): boolean;
|
||||
gte(num: Fraction.FractionInput): boolean;
|
||||
compare(num: Fraction.FractionInput): number;
|
||||
divisible(num: Fraction.FractionInput): boolean;
|
||||
|
||||
valueOf(): number;
|
||||
toString(decimalPlaces?: number): string;
|
||||
toLatex(showMixed?: boolean): string;
|
||||
toFraction(showMixed?: boolean): string;
|
||||
toContinued(): bigint[];
|
||||
clone(): Fraction;
|
||||
|
||||
static default: typeof Fraction;
|
||||
static Fraction: typeof Fraction;
|
||||
}
|
||||
|
||||
declare namespace Fraction {
|
||||
interface NumeratorDenominator { n: number | bigint; d: number | bigint; }
|
||||
type FractionInput =
|
||||
| Fraction
|
||||
| number
|
||||
| bigint
|
||||
| string
|
||||
| [number | bigint | string, number | bigint | string]
|
||||
| NumeratorDenominator;
|
||||
|
||||
type FractionParam = {
|
||||
(numerator: number | bigint, denominator: number | bigint): Fraction;
|
||||
(num: FractionInput): Fraction;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Export matches CJS runtime:
|
||||
* module.exports = Fraction;
|
||||
* module.exports.default = Fraction;
|
||||
* module.exports.Fraction = Fraction;
|
||||
*/
|
||||
declare const FractionExport: typeof Fraction & {
|
||||
default: typeof Fraction;
|
||||
Fraction: typeof Fraction;
|
||||
};
|
||||
|
||||
export = FractionExport;
|
||||
81
node_modules/fraction.js/package.json
generated
vendored
Normal file
81
node_modules/fraction.js/package.json
generated
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
{
|
||||
"name": "fraction.js",
|
||||
"title": "Fraction.js",
|
||||
"version": "5.3.4",
|
||||
"description": "The RAW rational numbers library",
|
||||
"homepage": "https://raw.org/article/rational-numbers-in-javascript/",
|
||||
"bugs": "https://github.com/rawify/Fraction.js/issues",
|
||||
"keywords": [
|
||||
"math",
|
||||
"numbers",
|
||||
"parser",
|
||||
"ratio",
|
||||
"fraction",
|
||||
"fractions",
|
||||
"rational",
|
||||
"rationals",
|
||||
"rational numbers",
|
||||
"bigint",
|
||||
"arbitrary precision",
|
||||
"mixed numbers",
|
||||
"decimal",
|
||||
"numerator",
|
||||
"denominator",
|
||||
"simplification"
|
||||
],
|
||||
"private": false,
|
||||
"main": "./dist/fraction.js",
|
||||
"module": "./dist/fraction.mjs",
|
||||
"browser": "./dist/fraction.min.js",
|
||||
"unpkg": "./dist/fraction.min.js",
|
||||
"types": "./fraction.d.mts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": {
|
||||
"import": "./fraction.d.mts",
|
||||
"require": "./fraction.d.ts"
|
||||
},
|
||||
"import": "./dist/fraction.mjs",
|
||||
"require": "./dist/fraction.js",
|
||||
"browser": "./dist/fraction.min.js"
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"typesVersions": {
|
||||
"<4.7": {
|
||||
"*": [
|
||||
"fraction.d.ts"
|
||||
]
|
||||
}
|
||||
},
|
||||
"sideEffects": false,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+ssh://git@github.com/rawify/Fraction.js.git"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/rawify"
|
||||
},
|
||||
"author": {
|
||||
"name": "Robert Eisele",
|
||||
"email": "robert@raw.org",
|
||||
"url": "https://raw.org/"
|
||||
},
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": "*"
|
||||
},
|
||||
"directories": {
|
||||
"example": "examples",
|
||||
"test": "tests"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "crude-build Fraction",
|
||||
"test": "mocha tests/*.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"crude-build": "^0.1.2",
|
||||
"mocha": "*"
|
||||
}
|
||||
}
|
||||
1046
node_modules/fraction.js/src/fraction.js
generated
vendored
Normal file
1046
node_modules/fraction.js/src/fraction.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1806
node_modules/fraction.js/tests/fraction.test.js
generated
vendored
Normal file
1806
node_modules/fraction.js/tests/fraction.test.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user