Fractions are a fundamental part of 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 line. For example, in the fraction 1/2, 1 is the numerator and 2 is the denominator.
Basic Fraction Operations
This calculator performs the four basic arithmetic operations on fractions:
Addition (a/b + c/d): To add fractions, find a common denominator. The least common denominator (LCD) is often preferred. Multiply the numerator and denominator of each fraction by the appropriate factor to achieve the LCD. Then, add the numerators and keep the common denominator.
Example: 1/2 + 1/4. LCD is 4. Convert 1/2 to 2/4. Then, 2/4 + 1/4 = 3/4.
Subtraction (a/b - c/d): Similar to addition, find a common denominator, convert the fractions, and then subtract the numerators while keeping the common denominator.
Example: 3/4 - 1/2. LCD is 4. Convert 1/2 to 2/4. Then, 3/4 - 2/4 = 1/4.
Multiplication (a/b * c/d): Multiply the numerators together and the denominators together. Simplification can be done before or after multiplication.
Example: 1/2 * 3/4 = (1*3) / (2*4) = 3/8.
Division (a/b ÷ c/d): To divide by a fraction, multiply by its reciprocal. The reciprocal of c/d is d/c. So, a/b ÷ c/d = a/b * d/c.
Example: 1/2 ÷ 1/4 = 1/2 * 4/1 = (1*4) / (2*1) = 4/2. This simplifies to 2/1 or 2.
Simplification (Reducing Fractions)
After performing an operation, the resulting fraction is often simplified to its lowest terms. This is done by dividing both the numerator and the denominator by their greatest common divisor (GCD). For example, 4/8 simplifies to 1/2 because the GCD of 4 and 8 is 4.
Use Cases
Fraction calculators are useful in various contexts:
Education: Helping students understand and practice fraction arithmetic.
Cooking: Adjusting recipe quantities (e.g., doubling or halving a recipe that uses fractions of cups).
DIY and Construction: Measuring and calculating lengths and materials when standard units involve fractions (e.g., woodworking, sewing).
Engineering and Science: Performing calculations where fractional values are common.
function gcd(a, b) {
var a = Math.abs(a);
var b = Math.abs(b);
while(b) {
var t = b;
b = a % b;
a = t;
}
return a;
}
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 resultDisplay = document.getElementById("result");
resultDisplay.innerHTML = ""; // Clear previous result
if (isNaN(num1) || isNaN(den1) || isNaN(num2) || isNaN(den2)) {
resultDisplay.innerHTML = "Error: Please enter valid numbers.";
return;
}
if (den1 === 0 || den2 === 0) {
resultDisplay.innerHTML = "Error: Denominator cannot be zero.";
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) {
resultDisplay.innerHTML = "Error: Division by zero.";
return;
}
resultNum = num1 * den2;
resultDen = den1 * num2;
}
if (resultDen === 0) {
resultDisplay.innerHTML = "Error: Resulting denominator is zero.";
return;
}
// Simplify the fraction
var commonDivisor = gcd(resultNum, resultDen);
resultNum = resultNum / commonDivisor;
resultDen = resultDen / commonDivisor;
// Handle negative denominator
if (resultDen < 0) {
resultNum = -resultNum;
resultDen = -resultDen;
}
var resultHTML = '
';
if (resultDen === 1) {
resultHTML += resultNum; // Display as whole number if denominator is 1
} else {
resultHTML += '