function getGCD(a, b) {
a = Math.abs(a);
b = Math.abs(b);
while (b) {
var temp = b;
b = a % b;
a = temp;
}
return a;
}
function simplifyFraction(numerator, denominator) {
if (denominator === 0) {
return { num: numerator, den: denominator }; // Cannot simplify if denominator is 0
}
if (numerator === 0) {
return { num: 0, den: 1 };
}
var commonDivisor = getGCD(numerator, denominator);
var simplifiedNum = numerator / commonDivisor;
var simplifiedDen = denominator / commonDivisor;
// Ensure denominator is positive
if (simplifiedDen 0 ? "Positive Infinity" : "Negative Infinity");
}
if (numerator === 0) {
return "0";
}
var simplified = simplifyFraction(numerator, denominator);
if (simplified.den === 1) {
return simplified.num.toString();
}
return simplified.num + "/" + simplified.den;
}
function calculateFractions() {
var n1 = parseFloat(document.getElementById("numerator1").value);
var d1 = parseFloat(document.getElementById("denominator1").value);
var n2 = parseFloat(document.getElementById("numerator2").value);
var d2 = parseFloat(document.getElementById("denominator2").value);
if (isNaN(n1) || isNaN(d1) || isNaN(n2) || isNaN(d2)) {
document.getElementById("additionResult").innerHTML = "Addition: Please enter valid numbers for all fields.";
document.getElementById("subtractionResult").innerHTML = "Subtraction:";
document.getElementById("multiplicationResult").innerHTML = "Multiplication:";
document.getElementById("divisionResult").innerHTML = "Division:";
return;
}
if (d1 === 0 || d2 === 0) {
document.getElementById("additionResult").innerHTML = "Addition: Denominators cannot be zero.";
document.getElementById("subtractionResult").innerHTML = "Subtraction:";
document.getElementById("multiplicationResult").innerHTML = "Multiplication:";
document.getElementById("divisionResult").innerHTML = "Division:";
return;
}
// Addition
var addNum = (n1 * d2) + (n2 * d1);
var addDen = d1 * d2;
document.getElementById("additionResult").innerHTML = "Addition: " + n1 + "/" + d1 + " + " + n2 + "/" + d2 + " = " + addNum + "/" + addDen + " = " + formatFraction(addNum, addDen);
// Subtraction
var subNum = (n1 * d2) – (n2 * d1);
var subDen = d1 * d2;
document.getElementById("subtractionResult").innerHTML = "Subtraction: " + n1 + "/" + d1 + " – " + n2 + "/" + d2 + " = " + subNum + "/" + subDen + " = " + formatFraction(subNum, subDen);
// Multiplication
var mulNum = n1 * n2;
var mulDen = d1 * d2;
document.getElementById("multiplicationResult").innerHTML = "Multiplication: " + n1 + "/" + d1 + " × " + n2 + "/" + d2 + " = " + mulNum + "/" + mulDen + " = " + formatFraction(mulNum, mulDen);
// Division
if (n2 === 0) {
document.getElementById("divisionResult").innerHTML = "Division: Cannot divide by zero (second fraction's numerator is zero).";
} else {
var divNum = n1 * d2;
var divDen = d1 * n2;
document.getElementById("divisionResult").innerHTML = "Division: " + n1 + "/" + d1 + " ÷ " + n2 + "/" + d2 + " = " + divNum + "/" + divDen + " = " + formatFraction(divNum, divDen);
}
}
// Initial calculation on load
window.onload = calculateFractions;
Understanding Fraction Arithmetic
Fractions are a fundamental concept in mathematics, representing a part of a whole. They consist of a numerator (the top number) and a denominator (the bottom number), separated by a fraction bar. The denominator indicates how many equal parts the whole is divided into, and the numerator indicates how many of those parts are being considered.
Adding Fractions
To add fractions, they must have a common denominator. If they don't, you need to find the least common multiple (LCM) of the denominators and convert the fractions to equivalent fractions with that common denominator. Once the denominators are the same, you simply add the numerators and keep the common denominator.
Similar to addition, subtracting fractions also requires a common denominator. Find the LCM of the denominators, convert the fractions, and then subtract the numerators, keeping the common denominator.
Multiplying fractions is straightforward: multiply the numerators together to get the new numerator, and multiply the denominators together to get the new denominator. Simplification can be done before or after multiplication.
To divide fractions, you "invert and multiply." This means you flip the second fraction (the divisor) to find its reciprocal, and then multiply the first fraction by this reciprocal.
After performing any arithmetic operation, it's good practice to simplify the resulting fraction to its lowest terms. This involves dividing both the numerator and the denominator by their greatest common divisor (GCD). For example, \(\frac{4}{8}\) simplifies to \(\frac{1}{2}\) because the GCD of 4 and 8 is 4.
How to Use the Calculator
Our Fraction Arithmetic Calculator makes these operations simple. Just enter the numerator and denominator for your first fraction, and then do the same for your second fraction. Click the "Calculate" button, and the tool will instantly display the results for addition, subtraction, multiplication, and division, including both the unsimplified and simplified forms of the answers.