Perform addition, subtraction, multiplication, and division on fractions.
+
–
*
/
Result
Understanding and Using the Fraction Calculator
Fractions are a fundamental concept in mathematics, representing a part of a whole. They are expressed as a ratio of two integers, a numerator and a denominator, separated by a line. For example, 1/2 means one part out of two equal parts. This calculator is designed to simplify operations involving fractions, making complex calculations accessible and straightforward for students, educators, and anyone needing to work with fractional values.
How Fractions Work
A fraction is written as a/b, where:
Numerator (a): The number of parts you have.
Denominator (b): The total number of equal parts the whole is divided into. The denominator cannot be zero.
Arithmetic Operations with Fractions
Our calculator handles the four basic arithmetic operations:
Addition (a/b + c/d):
To add fractions, they must have a common denominator. The formula is (a*d + b*c) / (b*d).
Subtraction (a/b – c/d):
Similar to addition, find a common denominator. The formula is (a*d - b*c) / (b*d).
Multiplication (a/b * c/d):
Multiplying fractions is simpler: multiply the numerators together and the denominators together. The formula is (a*c) / (b*d).
Division (a/b / c/d):
To divide fractions, you multiply the first fraction by the reciprocal of the second fraction. The formula is (a*d) / (b*c).
Simplifying Fractions (Reducing)
After performing an operation, the resulting fraction might not be in its simplest form. For example, 2/4 can be simplified to 1/2. This calculator automatically simplifies the result to its lowest terms by dividing both the numerator and the denominator by their greatest common divisor (GCD).
Greatest Common Divisor (GCD)
The GCD of two numbers is the largest positive integer that divides both numbers without leaving a remainder. For instance, the GCD of 12 and 18 is 6. We use the Euclidean algorithm to efficiently find the GCD.
Use Cases
Education: Helping students understand and practice fraction arithmetic.
Cooking: Adjusting recipes that involve fractional measurements (e.g., 1/2 cup of flour).
DIY Projects: Calculating material needs or dimensions that involve fractions.
General Math Problems: Solving any problem that requires fractional calculations.
Using this calculator can save time and reduce errors, ensuring accuracy in your mathematical tasks.
// Function to find the Greatest Common Divisor (GCD) using Euclidean algorithm
var gcd = function(a, b) {
var temp;
while (b !== 0) {
temp = b;
b = a % b;
a = temp;
}
return Math.abs(a); // Ensure GCD is positive
};
// Function to simplify a fraction
var simplifyFraction = function(numerator, denominator) {
if (denominator === 0) {
return { num: NaN, den: NaN, error: "Denominator cannot be zero." };
}
if (numerator === 0) {
return { num: 0, den: 1, error: null };
}
var commonDivisor = gcd(numerator, denominator);
var simplifiedNum = numerator / commonDivisor;
var simplifiedDen = denominator / commonDivisor;
// Ensure the denominator is positive
if (simplifiedDen 0) {
errorMessageDiv.textContent = errors.join(' ');
resultContainer.style.display = 'block';
return;
}
var resultNum, resultDen;
// — Calculation Logic —
switch (operator) {
case 'add':
resultNum = (num1 * den2) + (num2 * den1);
resultDen = den1 * den2;
break;
case 'subtract':
resultNum = (num1 * den2) – (num2 * den1);
resultDen = den1 * den2;
break;
case 'multiply':
resultNum = num1 * num2;
resultDen = den1 * den2;
break;
case 'divide':
// Division by zero was already checked
resultNum = num1 * den2;
resultDen = den1 * num2;
break;
default:
errorMessageDiv.textContent = "Invalid operation selected.";
resultContainer.style.display = 'block';
return;
}
// — Simplify Result —
var simplified = simplifyFraction(resultNum, resultDen);
if (simplified.error) {
errorMessageDiv.textContent = simplified.error;
} else {
if (simplified.den === 1) {
resultDiv.textContent = simplified.num.toString(); // Display as whole number if denominator is 1
} else {
resultDiv.innerHTML = simplified.num + ' ' + simplified.den;
}
}
resultContainer.style.display = 'block'; // Show the result container
};