Compare commits
1 Commits
6c49e96c1b
...
082313fb8d
Author | SHA1 | Date |
---|---|---|
Florine W. Dekker | 082313fb8d |
Binary file not shown.
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "simplify-fractions",
|
||||
"version": "1.2.5",
|
||||
"version": "1.3.0",
|
||||
"description": "Simple web tool for simplifying fractions",
|
||||
"author": "Felix W. Dekker",
|
||||
"browser": "dist/bundle.js",
|
||||
|
@ -16,7 +16,7 @@
|
|||
"deploy": "grunt deploy"
|
||||
},
|
||||
"dependencies": {
|
||||
"katex": "^0.13.3"
|
||||
"katex": "^0.13.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"css-loader": "^5.2.4",
|
||||
|
@ -30,7 +30,7 @@
|
|||
"grunt-text-replace": "^0.4.0",
|
||||
"grunt-webpack": "^4.0.3",
|
||||
"style-loader": "^2.0.0",
|
||||
"webpack": "^5.36.0",
|
||||
"webpack-cli": "^4.6.0"
|
||||
"webpack": "^5.36.2",
|
||||
"webpack-cli": "^4.7.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,67 +4,6 @@ import katex from "katex";
|
|||
import "katex/dist/katex.min.css";
|
||||
|
||||
|
||||
/**
|
||||
* A fraction that can be simplified.
|
||||
*/
|
||||
class Fraction {
|
||||
constructor(numerator, denominator) {
|
||||
if (!isInt(numerator) || !isInt(denominator))
|
||||
throw new Error("Numerator and denominator must be integer-like.");
|
||||
|
||||
this.sign = numerator < 0 !== denominator < 0 ? -1 : 1;
|
||||
this.numerator = Math.abs(+numerator);
|
||||
this.denominator = Math.abs(+denominator);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a new fraction such that the gcd of the numerator and denominator is 1.
|
||||
*
|
||||
* @returns {Fraction} a new fraction such that the gcd of the numerator and denominator is 1
|
||||
*/
|
||||
simplify() {
|
||||
const common = gcd(this.numerator, this.denominator);
|
||||
return new Fraction(this.sign * this.numerator / common, this.denominator / common);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the LaTeX string representation of this fraction.
|
||||
*
|
||||
* @returns {string} the LaTeX string representation of this fraction
|
||||
*/
|
||||
toString() {
|
||||
let frac = `\\frac{${this.numerator}}{${this.denominator}}`;
|
||||
if (this.sign === -1)
|
||||
frac = `-${frac}`;
|
||||
|
||||
return frac;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the LaTeX string representation of this fraction, or of the numerator if the denominator is 1.
|
||||
*
|
||||
* @returns {string} the LaTeX string representation of this fraction, or of the numerator if the denominator
|
||||
* is 1.
|
||||
*/
|
||||
toReducedString() {
|
||||
if (this.numerator === 0)
|
||||
return "0";
|
||||
|
||||
let frac;
|
||||
if (this.denominator === 1)
|
||||
frac = `${this.numerator}`;
|
||||
else
|
||||
frac = `\\frac{${this.numerator}}{${this.denominator}}`;
|
||||
|
||||
if (this.sign === -1)
|
||||
frac = `-${frac}`;
|
||||
|
||||
return frac;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// noinspection EqualityComparisonWithCoercionJS
|
||||
/**
|
||||
* Returns `true` if and only if `n` is an integer.
|
||||
|
@ -98,6 +37,68 @@ const gcd = (a, b) => {
|
|||
};
|
||||
|
||||
|
||||
/**
|
||||
* A fraction that can be simplified.
|
||||
*/
|
||||
class Fraction {
|
||||
constructor(numerator, denominator) {
|
||||
if (!isInt(numerator) || !isInt(denominator))
|
||||
throw new Error("Numerator and denominator must be integer-like.");
|
||||
|
||||
this.sign = numerator < 0 !== denominator < 0 ? -1 : 1;
|
||||
this.numerator = Math.abs(+numerator);
|
||||
this.denominator = Math.abs(+denominator);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a new fraction such that the gcd of the numerator and denominator is 1.
|
||||
*
|
||||
* @returns {Fraction} a new fraction such that the gcd of the numerator and denominator is 1
|
||||
*/
|
||||
simplifyGcd() {
|
||||
const common = gcd(this.numerator, this.denominator);
|
||||
return new Fraction(this.sign * this.numerator / common, this.denominator / common);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the LaTeX string representation of this fraction.
|
||||
*
|
||||
* Unlike `#toReducedString`, this method never simplifies the output to an integer.
|
||||
*
|
||||
* @returns {string} the LaTeX string representation of this fraction
|
||||
*/
|
||||
toString() {
|
||||
let frac = `\\frac{${this.numerator}}{${this.denominator}}`;
|
||||
if (this.sign === -1)
|
||||
frac = `-${frac}`;
|
||||
|
||||
return frac;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the LaTeX string representation of this fraction, or of the numerator if the denominator is 1.
|
||||
*
|
||||
* @returns {string} the string representation of the reduced form of this fraction
|
||||
*/
|
||||
toReducedString() {
|
||||
if (this.numerator === 0)
|
||||
return "0";
|
||||
|
||||
let frac;
|
||||
if (this.denominator === 1)
|
||||
frac = `${this.numerator}`;
|
||||
else
|
||||
frac = `\\frac{${this.numerator}}{${this.denominator}}`;
|
||||
|
||||
if (this.sign === -1)
|
||||
frac = `-${frac}`;
|
||||
|
||||
return frac;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
doAfterLoad(() => {
|
||||
$("#nav").appendChild(nav("/Tools/Simplify Fractions/"));
|
||||
$("#header").appendChild(header({
|
||||
|
@ -108,7 +109,9 @@ doAfterLoad(() => {
|
|||
vcsURL: "https://git.fwdekker.com/FWDekker/simplify-fractions/",
|
||||
version: "v%%VERSION_NUMBER%%"
|
||||
}));
|
||||
|
||||
$("main").classList.remove("hidden");
|
||||
$("[autofocus]").focus()
|
||||
});
|
||||
|
||||
doAfterLoad(() => {
|
||||
|
@ -156,7 +159,7 @@ doAfterLoad(() => {
|
|||
|
||||
const fraction = new Fraction(numeratorInput.value, denominatorInput.value);
|
||||
outputField.innerHTML = katex.renderToString(
|
||||
fraction.toString() + " = " + fraction.simplify().toReducedString(),
|
||||
fraction.toString() + " = " + fraction.simplifyGcd().toReducedString(),
|
||||
{
|
||||
displayMode: true,
|
||||
throwOnError: false
|
||||
|
|
Loading…
Reference in New Issue