Understanding Fraction Arithmetic with Whole Numbers
Fractions represent parts 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. This calculator focuses on performing basic arithmetic operations (addition, subtraction, multiplication, and division) between two fractions, where the inputs are treated as whole numbers for the numerators and denominators.
Operations Explained:
Addition: To add two fractions, a/b + c/d, they must have a common denominator. The formula is (ad + bc) / bd. For example, 1/2 + 1/3 = (1*3 + 1*2) / (2*3) = (3 + 2) / 6 = 5/6.
Subtraction: Similar to addition, to subtract a/b - c/d, you find a common denominator. The formula is (ad - bc) / bd. For example, 1/2 - 1/3 = (1*3 - 1*2) / (2*3) = (3 - 2) / 6 = 1/6.
Multiplication: Multiplying fractions, a/b * c/d, is straightforward. You multiply the numerators together and the denominators together: (a * c) / (b * d). For example, 1/2 * 1/3 = (1 * 1) / (2 * 3) = 1/6.
Division: Dividing by a fraction is equivalent to multiplying by its reciprocal. For a/b / c/d, you multiply the first fraction by the reciprocal of the second: a/b * d/c = (a * d) / (b * c). For example, 1/2 / 1/3 = 1/2 * 3/1 = (1 * 3) / (2 * 1) = 3/2.
Simplifying Fractions (Greatest Common Divisor – GCD):
After performing an operation, the resulting fraction can often be simplified. This is done by finding the Greatest Common Divisor (GCD) of the numerator and the denominator, and then dividing both by it. For example, if the result is 4/6, the GCD of 4 and 6 is 2. Dividing both by 2 gives the simplified fraction 2/3.
This calculator implements these standard arithmetic rules to provide accurate fraction calculations.
function gcd(a, b) {
var a = Math.abs(a);
var b = Math.abs(b);
while (b) {
var temp = b;
b = a % b;
a = temp;
}
return a;
}
function simplifyFraction(num, den) {
if (den === 0) return { num: num, den: den }; // Avoid division by zero
var commonDivisor = gcd(num, den);
num = num / commonDivisor;
den = den / commonDivisor;
// Ensure denominator is positive
if (den < 0) {
num = -num;
den = -den;
}
return { num: num, den: den };
}
function calculateFraction() {
var num1 = parseInt(document.getElementById("num1").value);
var den1 = parseInt(document.getElementById("den1").value);
var num2 = parseInt(document.getElementById("num2").value);
var den2 = parseInt(document.getElementById("den2").value);
var operation = document.getElementById("operation").value;
var resultDiv = document.getElementById("result");
// Input validation
if (isNaN(num1) || isNaN(den1) || isNaN(num2) || isNaN(den2)) {
resultDiv.textContent = "Please enter valid whole numbers.";
resultDiv.style.backgroundColor = "#f8d7da"; // Error color
resultDiv.style.color = "#721c24";
return;
}
if (den1 === 0 || den2 === 0) {
resultDiv.textContent = "Denominator cannot be zero.";
resultDiv.style.backgroundColor = "#f8d7da"; // Error color
resultDiv.style.color = "#721c24";
return;
}
var resultNum, resultDen;
if (operation === "add") {
resultNum = (num1 * den2) + (num2 * den1);
resultDen = den1 * den2;
} else if (operation === "subtract") {
resultNum = (num1 * den2) – (num2 * den1);
resultDen = den1 * den2;
} else if (operation === "multiply") {
resultNum = num1 * num2;
resultDen = den1 * den2;
} else if (operation === "divide") {
if (num2 === 0) {
resultDiv.textContent = "Cannot divide by zero.";
resultDiv.style.backgroundColor = "#f8d7da"; // Error color
resultDiv.style.color = "#721c24";
return;
}
resultNum = num1 * den2;
resultDen = den1 * num2;
}
var simplified = simplifyFraction(resultNum, resultDen);
if (simplified.den === 0) {
resultDiv.textContent = "Division by zero error in calculation.";
resultDiv.style.backgroundColor = "#f8d7da"; // Error color
resultDiv.style.color = "#721c24";
} else if (simplified.den === 1) {
resultDiv.textContent = simplified.num;
resultDiv.style.backgroundColor = "var(–success-green)"; // Success color
resultDiv.style.color = "white";
} else {
resultDiv.textContent = simplified.num + "/" + simplified.den;
resultDiv.style.backgroundColor = "var(–success-green)"; // Success color
resultDiv.style.color = "white";
}
}